Expand description
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
Suspendusing eitherSuspend::listenorSuspend::try_listen. It may be used to wait for a notification with.awaitor by parking the current thread. - Next
- A
Futurerepresenting the next item in an Iter stream. - Notifier
- An instance of a notifier for a
Suspendinstance. When notified, the associated thread orFuturewill be woken if currently suspended. - Recv
Error - An error returned if the associated
oneshot::Senderis dropped. Requires theoneshotfeature. An error returned from the indefinitely blocking recv functions on a [Receiver]. - Sender
- A
oneshot::Senderused to provide a single asynchronous result. Requires theoneshotfeature. - Suspend
- A structure which may be used to suspend a thread or
Futurepending a notification. - Task
- An asynchronous result which may be evaluated with
.await, or by using blocking operations with an optional timeout. - Timeout
Error - A timeout error which may be returned when waiting for a
SuspendorIterwith a given timeout.
Functions§
- block_
on - A convenience method to evaluate a
Future, blocking the current thread until it is resolved. - iter_
stream - A convenience method to turn a
Streaminto anIteratorwhich parks the current thread until items are available. - notify_
once - Create a new single-use
Notifierand a correspondingTask. Once notified, theTaskwill resolve to(). - ready
- A convenience method to create a new
Taskfrom a result value. - sender_
task - Create a new oneshot message pair consisting of a
Senderand aTask<T>. OnceSender::sendis called or theSenderis dropped, theTaskwill resolve to the sent message or aRecvError. Requires theoneshotfeature.
Type Aliases§
- PollFn
- A polling function equivalent to
Future::poll. - Poll
Next Fn - A polling function equivalent to
Stream::poll_next.