gpt4 book ai didi

Rust 生命周期,数据流入其他引用

转载 作者:行者123 更新时间:2023-11-29 08:28:58 25 4
gpt4 key购买 nike

我编写了以下代码来过滤数据流,在我从解析简单数字更改为还具有绑定(bind)到生命周期的类型(如 &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>绑定(bind)到 SCHEME这是延迟生成并绑定(bind)到 'static不允许 filter.execute 引用 &self.name.as_str() 是有道理的因为它会过时但不是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:这可以通过编译 as inside filter_matches 来解决。方法,但这有点糟糕,因为用户只会在尝试过滤时收到解析错误,而且速度可能会变慢。

最佳答案

我看到有两种方法可以解决这个问题:
1) 延长 self.name 的生命周期.这可以通过收集 expensive_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按 block ,将每个 block 收集到向量中。

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 倍

关于Rust 生命周期,数据流入其他引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55966092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com