rust - 在地图中存储异步回调
问题描述
我试图将多个异步回调存储在一个哈希映射中,稍后我会对其进行迭代。这是我最接近让它工作的尝试,但是,由于回调的生命周期,我收到了编译错误。
use std::boxed::Box;
use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
type AsyncCallback = Box<dyn Fn() -> Pin<Box<dyn Future<Output = ()>>>>;
#[derive(Default)]
pub struct CallbackMap {
callbacks: HashMap<&'static str, AsyncCallback>,
}
impl CallbackMap {
pub fn add<C, F>(&mut self, name: &'static str, callback: C)
where
C: Fn() -> F,
C: 'static,
F: Future<Output = ()> + 'static,
{
self.callbacks
.insert(name, Box::new(|| Box::pin(callback())));
}
pub async fn execute(&self) {
for (_key, value) in &self.callbacks {
value().await;
}
}
}
async fn async_callback() {
println!("Callback 2");
}
fn main() {
let mut callbacks = CallbackMap::default();
callbacks.add("test1", || async {
println!("Callback 1");
});
callbacks.add("test2", async_callback);
callbacks.execute();
}
这是我得到的错误:
error[E0597]: `callback` does not live long enough
--> src/main.rs:21:48
|
21 | .insert(name, Box::new(|| Box::pin(callback())));
| ---------------------^^^^^^^^----
| | | |
| | | borrowed value does not live long enough
| | value captured here
| cast requires that `callback` is borrowed for `'static`
22 | }
| - `callback` dropped here while still borrowed
解决方案
使用move
关键字移动 callback
到闭包中:
self.callbacks
.insert(name, Box::new(move || Box::pin(callback())));
// ^^^^
推荐阅读
- web-scraping - 网络抓取被阻止 - 使用 Googlesheets IMPORTXML()
- node.js - 我在使用 node.js 安装或创建东西时遇到问题
- google-cloud-platform - 如何将开始和结束日期传递给 Data Studio 中的报表?
- r - 使用 Stringr 简化清理推文文本
- dax - 根据使用 DAX 的工作日数比较 MTD 与上个月
- google-sheets - 谷歌表格中的自动序列号和文本:“数字 - 文本”通过拖动
- python - 修改列表中的值
- obs - 如何解决我的 obs 插件的断断续续的声音?
- python - 在特定文件夹中时python导入错误
- javascript - Discord JS 客户端仅在公会成员缓存中拥有自己