[−][src]Crate suspend
This crate provides various utilities for moving between async and synchronous contexts.
Examples
The Task structure allows a Future
or a polling function to be
evaluated in a blocking manner:
use suspend::Task; use std::time::Duration; let task = Task::from_future(async { 100 }).map(|val| val * 2); assert_eq!(task.wait_timeout(Duration::from_secs(1)), Ok(200));
Similarly, the Iter structure allows a Stream
instance to be consumed
in an async or blocking manner:
use suspend::{Iter, block_on}; let mut values = Iter::from_iterator(1..); assert_eq!(block_on(async { values.next().await }), Some(1)); assert_eq!(values.take(3).collect::<Vec<_>>(), vec![2, 3, 4]);
The Suspend structure may be used to coordinate between threads and
Futures
, allowing either to act as a waiter or notifier:
use std::time::Duration; use suspend::{Suspend, block_on}; let mut susp = Suspend::new(); let notifier = susp.notifier(); // start listening for notifications let mut listener = susp.listen(); // send a notification (satisfies the current listener) notifier.notify(); // wait for notification (already sent) with a timeout assert_eq!(listener.wait_timeout(Duration::from_secs(1)), true); drop(listener); let mut listener = susp.listen(); notifier.notify(); // the listener is also a Future block_on(async { listener.await });
Re-exports
pub use oneshot_rs as oneshot; |
Structs
Iter | A stream which may be polled asynchronously, or by using blocking operations with an optional timeout. |
Listener | The result of acquiring a |
Next | A |
Notifier | An instance of a notifier for a |
RecvError | An error returned if the associated |
Sender | A |
Suspend | A structure which may be used to suspend a thread or |
Task | An asynchronous result which may be evaluated with |
TimeoutError | A timeout error which may be returned when waiting for a |
Functions
block_on | A convenience method to evaluate a |
iter_stream | A convenience method to turn a |
notify_once | Create a new single-use |
ready | A convenience method to create a new |
sender_task | Create a new oneshot message pair consisting of a |
Type Definitions
PollFn | A polling function equivalent to |
PollNextFn | A polling function equivalent to |