#future #callback #async

callback-future

回调和futures之间的适配器

1 个不稳定版本

0.1.0 2020年5月25日

#1221 in 异步

MIT/Apache

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!");
}

许可证

本项目许可证为以下之一:

任选其一。

依赖项

~1–1.7MB
~35K SLoC