1 个不稳定版本
0.1.0 | 2020年5月25日 |
---|
#1221 in 异步
8KB
111 行
callback-future-rs
回调和futures之间简单的适配器。
示例
依赖项
[dependencies]
callback-future="0.1"
代码
use callback_future::CallbackFuture;
use futures::executor::block_on;
use std::thread;
use std::time::Duration;
async fn get_result() -> String {
CallbackFuture::new(
|complete| {
// simulate async callback from another thread
thread::spawn(move || {
thread::sleep(Duration::from_secs(1));
complete("Hello, world!".to_string());
});
}
).await
}
fn main() {
assert_eq!(block_on(get_result()), "Hello, world!");
}
许可证
本项目许可证为以下之一:
- Apache License, Version 2.0, (LICENSE-APACHE 或 https://apache.ac.cn/licenses/LICENSE-2.0)
- MIT 许可证 (LICENSE-MIT 或 http://opensource.org/licenses/MIT)
任选其一。
依赖项
~1–1.7MB
~35K SLoC