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
Suspend
using eitherSuspend::listen
orSuspend::try_listen
. It may be used to wait for a notification with.await
or by parking the current thread. - Next
- A
Future
representing the next item in an Iter stream. - Notifier
- An instance of a notifier for a
Suspend
instance. When notified, the associated thread orFuture
will be woken if currently suspended. - Recv
Error - An error returned if the associated
oneshot::Sender
is dropped. Requires theoneshot
feature. An error returned from the indefinitely blocking recv functions on a [Receiver
]. - Sender
- A
oneshot::Sender
used to provide a single asynchronous result. Requires theoneshot
feature. - Suspend
- A structure which may be used to suspend a thread or
Future
pending 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
Suspend
orIter
with 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
Stream
into anIterator
which parks the current thread until items are available. - notify_
once - Create a new single-use
Notifier
and a correspondingTask
. Once notified, theTask
will resolve to()
. - ready
- A convenience method to create a new
Task
from a result value. - sender_
task - Create a new oneshot message pair consisting of a
Sender
and aTask<T>
. OnceSender::send
is called or theSender
is dropped, theTask
will resolve to the sent message or aRecvError
. Requires theoneshot
feature.
Type Aliases§
- PollFn
- A polling function equivalent to
Future::poll
. - Poll
Next Fn - A polling function equivalent to
Stream::poll_next
.