[英]Rust lifetimes, data flows into other references
I wrote the following code that filters a stream of data which worked fine until I changed from parsing simple numbers to also have types that are bound to lifetimes like &str
and &[u8]
. 我编写了以下代码,该代码过滤了可以正常工作的数据流,直到我从解析简单数字更改为也具有绑定到生存期的类型(如
&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),
})
}
If I try to compile it the compiler will fail with this: 如果我尝试对其进行编译,则编译器将因以下原因而失败:
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
my first thought was that self and filter should have the same lifetime in fn filter_matches<'s>(&self, filter: &Filter<'s>) -> bool
but if I change the signature to fn filter_matches<'s>(&'s self, filter: &Filter<'s>) -> bool
I will start getting this error: 我首先想到的是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
I am failing to understand the reason, Filter<'s>
is bound to SCHEME
which is lazily generated and is bound to 'static
which makes sense not allowing filter.execute to take reference to &self.name.as_str()
because it would be outlived but, isn't filter.execute(&ctx)
which the signature is pub fn execute(&self, ctx: &ExecutionContext<'s>) -> Result<bool, SchemeMismatchError>
supposed to drop the references as soon as it finishes as the result of it has not other lifetimes? 我无法理解原因,
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>
应该在引用完成后立即删除引用结果没有其他生命?
In order to try and compile the code above, you can use this Cargo.toml
: 为了尝试编译上面的代码,您可以使用以下
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: That could be solved by compiling the as inside filter_matches
method but that would be sort of bad because the user would only get the parse error when trying to filter and it could potentially be slower. PS:可以通过在
filter_matches
方法中编译as来解决此问题,但这会很不好,因为用户在尝试过滤时只会得到parse错误,并且可能会更慢。
I see 2 ways to solve this problem: 我看到两种解决此问题的方法:
1) extend lifetime of self.name
. 1)延长
self.name
寿命。 This can be achieved by collecting expensive_data_iterator
into, say, Vec. 这可以通过将
expensive_data_iterator
data_iterator收集到Vec中来实现。
--- let data = expensive_data_iterator();
+++ let data: Vec<_> = expensive_data_iterator().collect();
2) reduce lifetime of filter
. 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()))
I omitted some other minor changes. 我省略了一些其他小的更改。 And yes,
filter_matches<'s>(&'s self, ...)
is mandatory in either case. 是的,无论哪种情况,
filter_matches<'s>(&'s self, ...)
都是强制性的。
PS yes, 2nd option works because my_struct
outlives filter
. PS是的,第二个选项有效,因为
my_struct
寿命超过filter
。 Well, if both approaches are somewhat bad, then you can combine them! 好吧,如果两种方法都不好,那么您可以将它们组合起来! Process
data
by chunks, collecting each one into vector. 按块处理
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
}
it uses only O(N) memory and compiles filter N times less 它仅使用O(N)内存,并且编译过滤器少N倍
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.