diff options
author | Carl Lerche <me@carllerche.com> | 2020-01-22 18:59:22 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-22 18:59:22 -0800 |
commit | 8cf98d694663e8397bfc337e7a4676567287bfae (patch) | |
tree | 50b214c2e29a257633494a64eed35c5b6f568eba /tokio/src/future | |
parent | f9ea576ccae5beffeaa2f2c48c2c0d2f9449673b (diff) |
Provide `select!` macro (#2152)
Provides a `select!` macro for concurrently waiting on multiple async
expressions. The macro has similar goals and syntax as the one provided
by the `futures` crate, but differs significantly in implementation.
First, this implementation does not require special traits to be
implemented on futures or streams (i.e., no `FuseFuture`). A design goal
is to be able to pass a "plain" async fn result into the select! macro.
Even without `FuseFuture`, this `select!` implementation is able to
handle all cases the `futures::select!` macro can handle. It does this
by supporting pre-poll conditions on branches and result pattern
matching. For pre-conditions, each branch is able to include a condition
that disables the branch if it evaluates to false. This allows the user
to guard futures that have already been polled, preventing double
polling. Pattern matching can be used to disable streams that complete.
A second big difference is the macro is implemented almost entirely as a
declarative macro. The biggest advantage to using this strategy is that
the user will not need to alter the rustc recursion limit except in the
most extreme cases.
The resulting future also tends to be smaller in many cases.
Diffstat (limited to 'tokio/src/future')
-rw-r--r-- | tokio/src/future/mod.rs | 2 | ||||
-rw-r--r-- | tokio/src/future/poll_fn.rs | 4 |
2 files changed, 3 insertions, 3 deletions
diff --git a/tokio/src/future/mod.rs b/tokio/src/future/mod.rs index 9a155bf7..c5225600 100644 --- a/tokio/src/future/mod.rs +++ b/tokio/src/future/mod.rs @@ -6,7 +6,7 @@ mod maybe_done; pub(crate) use maybe_done::{maybe_done, MaybeDone}; mod poll_fn; -pub(crate) use poll_fn::poll_fn; +pub use poll_fn::poll_fn; mod ready; pub(crate) use ready::{ok, Ready}; diff --git a/tokio/src/future/poll_fn.rs b/tokio/src/future/poll_fn.rs index ce2a5524..9b3d1370 100644 --- a/tokio/src/future/poll_fn.rs +++ b/tokio/src/future/poll_fn.rs @@ -6,14 +6,14 @@ use std::pin::Pin; use std::task::{Context, Poll}; /// Future for the [`poll_fn`] function. -pub(crate) struct PollFn<F> { +pub struct PollFn<F> { f: F, } impl<F> Unpin for PollFn<F> {} /// Creates a new future wrapping around a function returning [`Poll`]. -pub(crate) fn poll_fn<T, F>(f: F) -> PollFn<F> +pub fn poll_fn<T, F>(f: F) -> PollFn<F> where F: FnMut(&mut Context<'_>) -> Poll<T>, { |