我是 rust 和 tokio async 的新手,我正在尝试编译以下看似简单的代码:
async fn network_handler(network_config: &config::NetworkConfig) -> Result<(), Error> {
Ok(())
}
pub async fn run(network_config: &config::NetworkConfig) -> Result<(), Error> {
let network_config_copy = network_config.clone();
tokio::spawn(async move {
network_handler(&network_config_copy).await
}).await?
}
但是编译器抱怨:
error: cannot infer an appropriate lifetime
--> src/network.rs:43:18
|
43 | pub async fn run(network_config: &config::NetworkConfig) -> Result<(), Error> {
| ^^^^^^^^^^^^^^ ...but this borrow...
44 | let network_config_copy = network_config.clone();
45 | tokio::spawn(async move {
| ------------ this return type evaluates to the `'static` lifetime...
|
note: ...can't outlive the lifetime `'_` as defined on the function body at 43:34
--> src/network.rs:43:34
|
43 | pub async fn run(network_config: &config::NetworkConfig) -> Result<(), Error> {
| ^
help: you can add a constraint to the return type to make it last less than `'static` and match the lifetime `'_` as defined on the function body at 43:34
|
45 | tokio::spawn + '_(async move {
| ^^^^^^^^^^^^^^^^^
从我在该主题上找到的先前讨论和示例中,我了解到将对 network_config 的引用传递给 spawn-ed 闭包会导致生命周期问题,因为单独的线程可能会超过 network_config。这就是为什么我将 network_config 的克隆移动到生成的线程,但似乎仍然存在终生歧义。
是否有任何额外的提示我可以给编译器以便它正确地获得生命周期?还是我做错了整件事?
注意:NetworkConfig 类定义为:
#[derive(Debug, Deserialize)]
pub struct NetworkConfig {
pub bind: String,
pub node_key_file: String,
}