Serde 反序列化生命周期干扰局部变量生命周期
Serde Deserialize lifetime interfering with local variable lifetime
我正在学习 Rust 并使用 Tokio、channels 和 Serde。我在编写一些非常基本的代码时遇到困难:
use serde::{Deserialize, Serialize};
use tokio::sync::mpsc::{channel, Receiver};
struct WorkflowProcess {
rx: Receiver<String>,
}
#[derive(Deserialize, Serialize)]
struct Msg {
version: i32,
}
impl WorkflowProcess {
async fn process<'a, T: Deserialize<'a>>(&mut self, callback: impl Fn(T)) {
let r = self.rx.recv().await;
if let Some(v) = r {
let s: &str = v.as_str();
let deserialized: T = serde_json::from_str(s).unwrap();
callback(deserialized);
}
}
}
#[tokio::main]
async fn main() {
let (tx, rx) = channel::<String>(100);
let mut workflow = WorkflowProcess { rx };
let worker = tokio::spawn(async move {
let x = |msg: Msg| {
assert_eq!(
msg.version,
32
);
};
workflow.process(x).await;
});
let serialized = serde_json::to_string(&Msg { version: 32 }).unwrap();
tx.send(serialized).await;
worker.await;
}
尝试编译时出现此错误:
error[E0597]: `v` does not live long enough
--> src/main.rs:17:27
|
14 | async fn process<'a, T: Deserialize<'a>>(&mut self, callback: impl Fn(T)) {
| -- lifetime `'a` defined here
...
17 | let s: &str = v.as_str();
| ^^^^^^^^^^ borrowed value does not live long enough
18 | let deserialized: T = serde_json::from_str(s).unwrap();
| ----------------------- argument requires that `v` is borrowed for `'a`
19 | callback(deserialized);
20 | }
| - `v` dropped here while still borrowed
在我看来 deserialized
的生命周期在 v
和 s
的生命周期之内,但我猜 'a
不是我想的那样。我希望 Rust 有生命周期 debugger/visualizer。在这种情况下,'a
是否与 'static
相同?
我知道我可以使用 DeserializeOwned
,但我真的很想弄清我的误解。
您可以按照@Jmb 在评论中的建议使用HRTB:
impl WorkflowProcess {
async fn process<T>(&mut self, callback: impl Fn(T))
where
for<'a> T: Deserialize<'a>,
{
let r = self.rx.recv().await;
if let Some(v) = r {
let deserialized: T = {
let s: &str = v.as_str();
serde_json::from_str(s).unwrap()
};
callback(deserialized);
}
}
}
我正在学习 Rust 并使用 Tokio、channels 和 Serde。我在编写一些非常基本的代码时遇到困难:
use serde::{Deserialize, Serialize};
use tokio::sync::mpsc::{channel, Receiver};
struct WorkflowProcess {
rx: Receiver<String>,
}
#[derive(Deserialize, Serialize)]
struct Msg {
version: i32,
}
impl WorkflowProcess {
async fn process<'a, T: Deserialize<'a>>(&mut self, callback: impl Fn(T)) {
let r = self.rx.recv().await;
if let Some(v) = r {
let s: &str = v.as_str();
let deserialized: T = serde_json::from_str(s).unwrap();
callback(deserialized);
}
}
}
#[tokio::main]
async fn main() {
let (tx, rx) = channel::<String>(100);
let mut workflow = WorkflowProcess { rx };
let worker = tokio::spawn(async move {
let x = |msg: Msg| {
assert_eq!(
msg.version,
32
);
};
workflow.process(x).await;
});
let serialized = serde_json::to_string(&Msg { version: 32 }).unwrap();
tx.send(serialized).await;
worker.await;
}
尝试编译时出现此错误:
error[E0597]: `v` does not live long enough
--> src/main.rs:17:27
|
14 | async fn process<'a, T: Deserialize<'a>>(&mut self, callback: impl Fn(T)) {
| -- lifetime `'a` defined here
...
17 | let s: &str = v.as_str();
| ^^^^^^^^^^ borrowed value does not live long enough
18 | let deserialized: T = serde_json::from_str(s).unwrap();
| ----------------------- argument requires that `v` is borrowed for `'a`
19 | callback(deserialized);
20 | }
| - `v` dropped here while still borrowed
在我看来 deserialized
的生命周期在 v
和 s
的生命周期之内,但我猜 'a
不是我想的那样。我希望 Rust 有生命周期 debugger/visualizer。在这种情况下,'a
是否与 'static
相同?
我知道我可以使用 DeserializeOwned
,但我真的很想弄清我的误解。
您可以按照@Jmb 在评论中的建议使用HRTB:
impl WorkflowProcess {
async fn process<T>(&mut self, callback: impl Fn(T))
where
for<'a> T: Deserialize<'a>,
{
let r = self.rx.recv().await;
if let Some(v) = r {
let deserialized: T = {
let s: &str = v.as_str();
serde_json::from_str(s).unwrap()
};
callback(deserialized);
}
}
}