Crates.io | callback-future |
lib.rs | callback-future |
version | 0.1.0 |
source | src |
created_at | 2020-05-25 15:21:26.922102 |
updated_at | 2020-05-25 15:21:26.922102 |
description | Adapter between callbacks and futures |
homepage | |
repository | https://github.com/syrykh/callback-future-rs.git |
max_upload_size | |
id | 245621 |
size | 20,102 |
A simple adapter between callbacks and futures.
Dependencies:
[dependencies]
callback-future="0.1"
Code:
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!");
}
This project is licensed under either of
at your option.