#future #callback #async

callback-future

Adapter between callbacks and futures

1 unstable release

0.1.0 May 25, 2020

#992 in Asynchronous

MIT/Apache

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

at your option.

Dependencies

~1–1.7MB
~36K SLoC