[英]Rust lifetimes, data flows into other references
我編寫了以下代碼,該代碼過濾了可以正常工作的數據流,直到我從解析簡單數字更改為也具有綁定到生存期的類型(如&str
和&[u8]
為止。
use wirefilter::{ExecutionContext, Filter, Scheme};
lazy_static::lazy_static! {
static ref SCHEME: Scheme = Scheme! {
port: Int,
name: Bytes,
};
}
#[derive(Debug)]
struct MyStruct {
port: i32,
name: String,
}
impl MyStruct {
fn scheme() -> &'static Scheme {
&SCHEME
}
fn filter_matches<'s>(&self, filter: &Filter<'s>) -> bool {
let mut ctx = ExecutionContext::new(Self::scheme());
ctx.set_field_value("port", self.port).unwrap();
ctx.set_field_value("name", self.name.as_str()).unwrap();
filter.execute(&ctx).unwrap()
}
}
fn main() -> Result<(), failure::Error> {
let data = expensive_data_iterator();
let scheme = MyStruct::scheme();
let filter = scheme
.parse("port in {2 5} && name matches \"http.*\"")?
.compile();
for my_struct in data
.filter(|my_struct| my_struct.filter_matches(&filter))
.take(2)
{
println!("{:?}", my_struct);
}
Ok(())
}
fn expensive_data_iterator() -> impl Iterator<Item = MyStruct> {
(0..).map(|port| MyStruct {
port,
name: format!("http {}", port % 2),
})
}
如果我嘗試對其進行編譯,則編譯器將因以下原因而失敗:
error[E0623]: lifetime mismatch
--> src/main.rs:26:16
|
21 | fn filter_matches<'s>(&self, filter: &Filter<'s>) -> bool {
| ----- ----------
| |
| these two types are declared with different lifetimes...
...
26 | filter.execute(&ctx).unwrap()
| ^^^^^^^ ...but data from `self` flows into `filter` here
error: aborting due to previous error
error: Could not compile `wirefilter_playground`.
To learn more, run the command again with --verbose.
Process finished with exit code 101
我首先想到的是self和filter在fn filter_matches<'s>(&self, filter: &Filter<'s>) -> bool
應該有相同的壽命,但是如果我將簽名更改為fn filter_matches<'s>(&'s self, filter: &Filter<'s>) -> bool
我將開始遇到此錯誤:
error: borrowed data cannot be stored outside of its closure
--> src/main.rs:38:29
|
33 | let filter = scheme
| ------ ...so that variable is valid at time of its declaration
...
38 | .filter(|my_struct| my_struct.filter_matches(&filter))
| ----------- ^^^^^^^^^ -------------- cannot infer an appropriate lifetime...
| | |
| | cannot be stored outside of its closure
| borrowed data cannot outlive this closure
error: aborting due to previous error
error: Could not compile `wirefilter_playground`.
To learn more, run the command again with --verbose.
Process finished with exit code 101
我無法理解原因, Filter<'s>
綁定到延遲生成的SCHEME
,並綁定到'static
,這意味着不允許filter.execute引用&self.name.as_str()
是&self.name.as_str()
因為這樣已filter.execute(&ctx)
,但不是filter.execute(&ctx)
,簽名是pub fn execute(&self, ctx: &ExecutionContext<'s>) -> Result<bool, SchemeMismatchError>
應該在引用完成后立即刪除引用結果沒有其他生命?
為了嘗試編譯上面的代碼,您可以使用以下Cargo.toml
:
[package]
name = "wirefilter_playground"
version = "0.1.0"
edition = "2018"
[dependencies]
wirefilter-engine = "0.6.1"
failure = "0.1.5"
lazy_static = "1.3.0"
PS:可以通過在filter_matches
方法中編譯as來解決此問題,但這會很不好,因為用戶在嘗試過濾時只會得到parse錯誤,並且可能會更慢。
我看到兩種解決此問題的方法:
1)延長self.name
壽命。 這可以通過將expensive_data_iterator
data_iterator收集到Vec中來實現。
--- let data = expensive_data_iterator();
+++ let data: Vec<_> = expensive_data_iterator().collect();
2)減少filter
使用壽命。
--- let filter = scheme.parse("...")?.compile();
+++ let filter = scheme.parse("...")?;
--- .filter(|my_struct| my_struct.filter_matches(&filter))
+++ .filter(|my_struct| my_struct.filter_matches(&filter.clone().compile()))
我省略了一些其他小的更改。 是的,無論哪種情況, filter_matches<'s>(&'s self, ...)
都是強制性的。
PS是的,第二個選項有效,因為my_struct
壽命超過filter
。 好吧,如果兩種方法都不好,那么您可以將它們組合起來! 按塊處理data
,將每個data
收集到向量中。
const N: usize = 10; // or any other size
loop {
let cur_chunk: Vec<_> = data.by_ref().take(N).collect();
if cur_chunk.is_empty() {
break;
}
let cur_filter = filter.clone().compile();
// etc
}
它僅使用O(N)內存,並且編譯過濾器少N倍
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.