mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-29 00:00:11 +02:00
This patch adds experimental async/await support to Tokio. It does this by adding feature flags to existing libs only where necessary in order to add nightly specific code (mostly `Unpin` implementations). It then provides a new crate: `tokio-async-await` which is a shim layer on top of `tokio`. The `tokio-async-await` crate is expected to look exactly like `tokio` does, but with async / await support. This strategy reduces the amount of cfg guarding in the main libraries. This patch also adds `tokio-channel`, which is copied from futures-rs 0.1 and adds the necessary `Unpin` implementations. In general, futures 0.1 is mostly unmaintained, so it will make sense for Tokio to take over maintainership of key components regardless of async / await support.
41 lines
1.0 KiB
Rust
41 lines
1.0 KiB
Rust
//! Use streams with `async` / `await`.
|
|
|
|
mod next;
|
|
|
|
pub use self::next::Next;
|
|
|
|
use futures::Stream;
|
|
|
|
use std::marker::Unpin;
|
|
|
|
/// An extension trait which adds utility methods to `Stream` types.
|
|
pub trait StreamExt: Stream {
|
|
/// Creates a future that resolves to the next item in the stream.
|
|
///
|
|
/// # Examples
|
|
///
|
|
/// ```
|
|
/// #![feature(await_macro, async_await)]
|
|
/// tokio::run_async(async {
|
|
/// // The extension trait can also be imported with
|
|
/// // `use tokio::prelude::*`.
|
|
/// use tokio::prelude::{stream, StreamExt};
|
|
///
|
|
/// let mut stream = stream::iter_ok::<_, ()>(1..3);
|
|
///
|
|
/// assert_eq!(await!(stream.next()), Some(Ok(1)));
|
|
/// assert_eq!(await!(stream.next()), Some(Ok(2)));
|
|
/// assert_eq!(await!(stream.next()), Some(Ok(3)));
|
|
/// assert_eq!(await!(stream.next()), None);
|
|
/// });
|
|
/// ```
|
|
fn next(&mut self) -> Next<Self>
|
|
where
|
|
Self: Sized + Unpin,
|
|
{
|
|
Next::new(self)
|
|
}
|
|
}
|
|
|
|
impl<T: Stream> StreamExt for T {}
|