我正在编写一个程序,使用wirefilter来过滤来自无限流的数据。
但是似乎由于生命周期我不能在循环中使用已编译的ast,而当我尝试进行编译时,这是输出:
error: borrowed data cannot be stored outside of its closure
--> src/main.rs:34:33
|
31 | let filter = ast.compile();
| ------ ...so that variable is valid at time of its declaration
32 |
33 | for my_struct in data.filter(|my_struct| {
| ----------- borrowed data cannot outlive this closure
34 | let execution_context = my_struct.execution_context();
| ^^^^^^^^^ ----------------- cannot infer an appropriate lifetime...
| |
| cannot be stored outside of its closure
error: aborting due to previous error
error: Could not compile `wirefilter_playground`.
To learn more, run the command again with --verbose.
main.rs
use wirefilter::{ExecutionContext, Scheme};
lazy_static::lazy_static! {
static ref SCHEME: Scheme = Scheme! {
port: Int
};
}
#[derive(Debug)]
struct MyStruct {
port: i32,
}
impl MyStruct {
fn scheme() -> &'static Scheme {
&SCHEME
}
fn execution_context(&self) -> ExecutionContext {
let mut ctx = ExecutionContext::new(Self::scheme());
ctx.set_field_value("port", self.port).unwrap();
ctx
}
}
fn main() -> Result<(), failure::Error> {
let data = expensive_data_iterator();
let scheme = MyStruct::scheme();
let ast = scheme.parse("port in {2 5}")?;
let filter = ast.compile();
for my_struct in data.filter(|my_struct| {
let execution_context = my_struct.execution_context();
filter.execute(&execution_context).unwrap()
}).take(10) {
println!("{:?}", my_struct);
}
Ok(())
}
fn expensive_data_iterator() -> impl Iterator<Item=MyStruct> {
(0..).map(|port| MyStruct { port })
}
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"
有可能使其工作吗?我只想为最终用户生成经过过滤的数据,否则数据量将在内存中巨大。 预先谢谢你!
答案 0 :(得分:2)
问题似乎在于返回结构中的生存期省略。特别是这段代码:
fn execution_context(&self) -> ExecutionContext {
//...
}
与此等效:
fn execution_context<'s>(&'s self) -> ExecutionContext<'s> {
//...
}
一旦您意识到ExecutionContext
具有关联的生存期,这将变得显而易见。
ExecutionContext
的生存期不必与MyStruct
的生存期匹配,因此您可能要编写:
fn execution_context<'e>(&self) -> ExecutionContext<'e> {
//...
}
或者也许:
fn execution_context<'s, 'e>(&'s self) -> ExecutionContext<'e>
where 'e: 's {
//...
}
取决于您的上下文最终是否会引用MyStruct
的任何内容。