1 unstable release
0.1.0 | May 25, 2020 |
---|
#1245 in Asynchronous
8KB
111 lines
callback-future-rs
A simple adapter between callbacks and futures.
Example
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!");
}
License
This project is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Dependencies
~1–1.6MB
~33K SLoC