diff --git a/futures-test/src/future/mod.rs b/futures-test/src/future/mod.rs index 73b2326e5e..5238e7dbbb 100644 --- a/futures-test/src/future/mod.rs +++ b/futures-test/src/future/mod.rs @@ -61,15 +61,16 @@ pub trait FutureTestExt: Future { /// /// ``` /// #![feature(async_await)] + /// # futures::executor::block_on(async { /// use futures::channel::oneshot; - /// use futures::executor::block_on; /// use futures_test::future::FutureTestExt; /// /// let (tx, rx) = oneshot::channel::(); /// /// (async { tx.send(5).unwrap() }).run_in_background(); /// - /// assert_eq!(block_on(rx), Ok(5)); + /// assert_eq!(rx.await, Ok(5)); + /// # }); /// ``` fn run_in_background(self) where diff --git a/futures-util/src/future/abortable.rs b/futures-util/src/future/abortable.rs index ab176f4b6b..813736b8fd 100644 --- a/futures-util/src/future/abortable.rs +++ b/futures-util/src/future/abortable.rs @@ -29,13 +29,15 @@ impl Abortable where Fut: Future { /// Example: /// /// ``` + /// #![feature(async_await)] + /// # futures::executor::block_on(async { /// use futures::future::{ready, Abortable, AbortHandle, Aborted}; - /// use futures::executor::block_on; /// /// let (abort_handle, abort_registration) = AbortHandle::new_pair(); /// let future = Abortable::new(ready(2), abort_registration); /// abort_handle.abort(); - /// assert_eq!(block_on(future), Err(Aborted)); + /// assert_eq!(future.await, Err(Aborted)); + /// # }); /// ``` pub fn new(future: Fut, reg: AbortRegistration) -> Self { Abortable { @@ -68,13 +70,16 @@ impl AbortHandle { /// Example: /// /// ``` + /// #![feature(async_await)] + /// # futures::executor::block_on(async { /// use futures::future::{ready, Abortable, AbortHandle, Aborted}; - /// use futures::executor::block_on; /// /// let (abort_handle, abort_registration) = AbortHandle::new_pair(); /// let future = Abortable::new(ready(2), abort_registration); /// abort_handle.abort(); - /// assert_eq!(block_on(future), Err(Aborted)); + /// assert_eq!(future.await, Err(Aborted)); + /// # }); + /// ``` pub fn new_pair() -> (Self, AbortRegistration) { let inner = Arc::new(AbortInner { waker: AtomicWaker::new(), diff --git a/futures-util/src/future/fuse.rs b/futures-util/src/future/fuse.rs index bb4c87c2cb..bdb9880222 100644 --- a/futures-util/src/future/fuse.rs +++ b/futures-util/src/future/fuse.rs @@ -27,7 +27,7 @@ impl Fuse { /// # Examples /// /// ``` - /// #![feature(async_await, futures_api)] + /// #![feature(async_await)] /// # futures::executor::block_on(async { /// use futures::channel::mpsc; /// use futures::future::{Fuse, FusedFuture, FutureExt}; diff --git a/futures-util/src/future/mod.rs b/futures-util/src/future/mod.rs index b63366d6ca..768672b1f4 100644 --- a/futures-util/src/future/mod.rs +++ b/futures-util/src/future/mod.rs @@ -451,6 +451,8 @@ pub trait FutureExt: Future { /// // synchronous function to better illustrate the cross-thread aspect of /// // the `shared` combinator. /// + /// #![feature(async_await)] + /// # futures::executor::block_on(async { /// use futures::future::{self, FutureExt}; /// use futures::executor::block_on; /// use std::thread; @@ -461,8 +463,9 @@ pub trait FutureExt: Future { /// let join_handle = thread::spawn(move || { /// assert_eq!(6, block_on(shared2)); /// }); - /// assert_eq!(6, block_on(shared1)); + /// assert_eq!(6, shared1.await); /// join_handle.join().unwrap(); + /// # }); /// ``` #[cfg(feature = "std")] fn shared(self) -> Shared diff --git a/futures-util/src/sink/mod.rs b/futures-util/src/sink/mod.rs index bd77591200..d39edd484f 100644 --- a/futures-util/src/sink/mod.rs +++ b/futures-util/src/sink/mod.rs @@ -86,8 +86,9 @@ pub trait SinkExt: Sink { /// # Examples /// /// ``` + /// #![feature(async_await)] + /// # futures::executor::block_on(async { /// use futures::channel::mpsc; - /// use futures::executor::block_on; /// use futures::sink::SinkExt; /// use futures::stream::StreamExt; /// use std::collections::VecDeque; @@ -98,10 +99,11 @@ pub trait SinkExt: Sink { /// VecDeque::from(vec![Ok(42); x]) /// }); /// - /// block_on(tx.send(5)).unwrap(); + /// tx.send(5).await.unwrap(); /// drop(tx); - /// let received: Vec = block_on(rx.collect()); + /// let received: Vec = rx.collect().await; /// assert_eq!(received, vec![42, 42, 42, 42, 42]); + /// # }); /// ``` fn with_flat_map(self, f: F) -> WithFlatMap where F: FnMut(U) -> St, diff --git a/futures-util/src/stream/iter.rs b/futures-util/src/stream/iter.rs index 6b32fbf68c..3acab7911d 100644 --- a/futures-util/src/stream/iter.rs +++ b/futures-util/src/stream/iter.rs @@ -18,11 +18,13 @@ impl Unpin for Iter {} /// simply always calls `iter.next()` and returns that. /// /// ``` -/// use futures::executor::block_on; +/// #![feature(async_await)] +/// # futures::executor::block_on(async { /// use futures::stream::{self, StreamExt}; /// /// let mut stream = stream::iter(vec![17, 19]); -/// assert_eq!(vec![17, 19], block_on(stream.collect::>())); +/// assert_eq!(vec![17, 19], stream.collect::>().await); +/// # }); /// ``` pub fn iter(i: I) -> Iter where I: IntoIterator, diff --git a/futures-util/src/stream/mod.rs b/futures-util/src/stream/mod.rs index 65e7b1a810..f5436ebbd6 100644 --- a/futures-util/src/stream/mod.rs +++ b/futures-util/src/stream/mod.rs @@ -168,15 +168,17 @@ pub trait StreamExt: Stream { /// # Examples /// /// ``` - /// use futures::executor::block_on; + /// #![feature(async_await)] + /// # futures::executor::block_on(async { /// use futures::stream::{self, StreamExt}; /// /// let mut stream = stream::iter(1..=3); /// - /// assert_eq!(block_on(stream.next()), Some(1)); - /// assert_eq!(block_on(stream.next()), Some(2)); - /// assert_eq!(block_on(stream.next()), Some(3)); - /// assert_eq!(block_on(stream.next()), None); + /// assert_eq!(stream.next().await, Some(1)); + /// assert_eq!(stream.next().await, Some(2)); + /// assert_eq!(stream.next().await, Some(3)); + /// assert_eq!(stream.next().await, None); + /// # }); /// ``` fn next(&mut self) -> Next<'_, Self> where Self: Sized + Unpin, @@ -200,16 +202,18 @@ pub trait StreamExt: Stream { /// # Examples /// /// ``` - /// use futures::executor::block_on; + /// #![feature(async_await)] + /// # futures::executor::block_on(async { /// use futures::stream::{self, StreamExt}; /// /// let stream = stream::iter(1..=3); /// - /// let (item, stream) = block_on(stream.into_future()); + /// let (item, stream) = stream.into_future().await; /// assert_eq!(Some(1), item); /// - /// let (item, stream) = block_on(stream.into_future()); + /// let (item, stream) = stream.into_future().await; /// assert_eq!(Some(2), item); + /// # }); /// ``` fn into_future(self) -> StreamFuture where Self: Sized + Unpin, @@ -231,13 +235,15 @@ pub trait StreamExt: Stream { /// # Examples /// /// ``` - /// use futures::executor::block_on; + /// #![feature(async_await)] + /// # futures::executor::block_on(async { /// use futures::stream::{self, StreamExt}; /// /// let stream = stream::iter(1..=3); /// let stream = stream.map(|x| x + 3); /// - /// assert_eq!(vec![4, 5, 6], block_on(stream.collect::>())); + /// assert_eq!(vec![4, 5, 6], stream.collect::>().await); + /// # }); /// ``` fn map(self, f: F) -> Map where F: FnMut(Self::Item) -> T, @@ -307,14 +313,16 @@ pub trait StreamExt: Stream { /// # Examples /// /// ``` - /// use futures::executor::block_on; + /// #![feature(async_await)] + /// # futures::executor::block_on(async { /// use futures::future; /// use futures::stream::{self, StreamExt}; /// /// let stream = stream::iter(1..=10); /// let evens = stream.filter(|x| future::ready(x % 2 == 0)); /// - /// assert_eq!(vec![2, 4, 6, 8, 10], block_on(evens.collect::>())); + /// assert_eq!(vec![2, 4, 6, 8, 10], evens.collect::>().await); + /// # }); /// ``` fn filter(self, f: F) -> Filter where F: FnMut(&Self::Item) -> Fut, @@ -338,7 +346,8 @@ pub trait StreamExt: Stream { /// /// # Examples /// ``` - /// use futures::executor::block_on; + /// #![feature(async_await)] + /// # futures::executor::block_on(async { /// use futures::future; /// use futures::stream::{self, StreamExt}; /// @@ -348,7 +357,8 @@ pub trait StreamExt: Stream { /// future::ready(ret) /// }); /// - /// assert_eq!(vec![3, 5, 7, 9, 11], block_on(evens.collect::>())); + /// assert_eq!(vec![3, 5, 7, 9, 11], evens.collect::>().await); + /// # }); /// ``` fn filter_map(self, f: F) -> FilterMap where F: FnMut(Self::Item) -> Fut, @@ -371,14 +381,16 @@ pub trait StreamExt: Stream { /// # Examples /// /// ``` - /// use futures::executor::block_on; + /// #![feature(async_await)] + /// # futures::executor::block_on(async { /// use futures::future; /// use futures::stream::{self, StreamExt}; /// /// let stream = stream::iter(1..=3); /// let stream = stream.then(|x| future::ready(x + 3)); /// - /// assert_eq!(vec![4, 5, 6], block_on(stream.collect::>())); + /// assert_eq!(vec![4, 5, 6], stream.collect::>().await); + /// # }); /// ``` fn then(self, f: F) -> Then where F: FnMut(Self::Item) -> Fut, @@ -396,8 +408,9 @@ pub trait StreamExt: Stream { /// # Examples /// /// ``` + /// #![feature(async_await)] + /// # futures::executor::block_on(async { /// use futures::channel::mpsc; - /// use futures::executor::block_on; /// use futures::stream::StreamExt; /// use std::thread; /// @@ -409,8 +422,9 @@ pub trait StreamExt: Stream { /// } /// }); /// - /// let output = block_on(rx.collect::>()); + /// let output = rx.collect::>().await; /// assert_eq!(output, vec![1, 2, 3, 4, 5]); + /// # }); /// ``` fn collect>(self) -> Collect where Self: Sized @@ -431,8 +445,9 @@ pub trait StreamExt: Stream { /// # Examples /// /// ``` + /// #![feature(async_await)] + /// # futures::executor::block_on(async { /// use futures::channel::mpsc; - /// use futures::executor::block_on; /// use futures::stream::StreamExt; /// use std::thread; /// @@ -445,9 +460,10 @@ pub trait StreamExt: Stream { /// } /// }); /// - /// let result = block_on(rx.concat()); + /// let result = rx.concat().await; /// /// assert_eq!(result, vec![7, 8, 9, 4, 5, 6, 1, 2, 3]); + /// # }); /// ``` fn concat(self) -> Concat where Self: Sized, @@ -469,13 +485,15 @@ pub trait StreamExt: Stream { /// # Examples /// /// ``` - /// use futures::executor::block_on; + /// #![feature(async_await)] + /// # futures::executor::block_on(async { /// use futures::future; /// use futures::stream::{self, StreamExt}; /// /// let number_stream = stream::iter(0..6); /// let sum = number_stream.fold(0, |acc, x| future::ready(acc + x)); - /// assert_eq!(block_on(sum), 15); + /// assert_eq!(sum.await, 15); + /// # }); /// ``` fn fold(self, init: T, f: F) -> Fold where F: FnMut(T, Self::Item) -> Fut, @@ -490,8 +508,9 @@ pub trait StreamExt: Stream { /// # Examples /// /// ``` + /// #![feature(async_await)] + /// # futures::executor::block_on(async { /// use futures::channel::mpsc; - /// use futures::executor::block_on; /// use futures::stream::StreamExt; /// use std::thread; /// @@ -512,8 +531,9 @@ pub trait StreamExt: Stream { /// tx3.unbounded_send(rx2).unwrap(); /// }); /// - /// let output = block_on(rx3.flatten().collect::>()); + /// let output = rx3.flatten().collect::>().await; /// assert_eq!(output, vec![1, 2, 3, 4]); + /// # }); /// ``` fn flatten(self) -> Flatten where Self::Item: Stream, @@ -533,7 +553,8 @@ pub trait StreamExt: Stream { /// # Examples /// /// ``` - /// use futures::executor::block_on; + /// #![feature(async_await)] + /// # futures::executor::block_on(async { /// use futures::future; /// use futures::stream::{self, StreamExt}; /// @@ -541,7 +562,8 @@ pub trait StreamExt: Stream { /// /// let stream = stream.skip_while(|x| future::ready(*x <= 5)); /// - /// assert_eq!(vec![6, 7, 8, 9, 10], block_on(stream.collect::>())); + /// assert_eq!(vec![6, 7, 8, 9, 10], stream.collect::>().await); + /// # }); /// ``` fn skip_while(self, f: F) -> SkipWhile where F: FnMut(&Self::Item) -> Fut, @@ -561,7 +583,8 @@ pub trait StreamExt: Stream { /// # Examples /// /// ``` - /// use futures::executor::block_on; + /// #![feature(async_await)] + /// # futures::executor::block_on(async { /// use futures::future; /// use futures::stream::{self, StreamExt}; /// @@ -569,7 +592,8 @@ pub trait StreamExt: Stream { /// /// let stream = stream.take_while(|x| future::ready(*x <= 5)); /// - /// assert_eq!(vec![1, 2, 3, 4, 5], block_on(stream.collect::>())); + /// assert_eq!(vec![1, 2, 3, 4, 5], stream.collect::>().await); + /// # }); /// ``` fn take_while(self, f: F) -> TakeWhile where F: FnMut(&Self::Item) -> Fut, @@ -595,7 +619,8 @@ pub trait StreamExt: Stream { /// # Examples /// /// ``` - /// use futures::executor::block_on; + /// #![feature(async_await)] + /// # futures::executor::block_on(async { /// use futures::future; /// use futures::stream::{self, StreamExt}; /// @@ -606,10 +631,11 @@ pub trait StreamExt: Stream { /// x += item; /// future::ready(()) /// }); - /// block_on(fut); + /// fut.await; /// } /// /// assert_eq!(x, 3); + /// # }); /// ``` fn for_each(self, f: F) -> ForEach where F: FnMut(Self::Item) -> Fut, @@ -690,12 +716,14 @@ pub trait StreamExt: Stream { /// # Examples /// /// ``` - /// use futures::executor::block_on; + /// #![feature(async_await)] + /// # futures::executor::block_on(async { /// use futures::stream::{self, StreamExt}; /// /// let stream = stream::iter(1..=10).take(3); /// - /// assert_eq!(vec![1, 2, 3], block_on(stream.collect::>())); + /// assert_eq!(vec![1, 2, 3], stream.collect::>().await); + /// # }); /// ``` fn take(self, n: u64) -> Take where Self: Sized @@ -711,12 +739,14 @@ pub trait StreamExt: Stream { /// # Examples /// /// ``` - /// use futures::executor::block_on; + /// #![feature(async_await)] + /// # futures::executor::block_on(async { /// use futures::stream::{self, StreamExt}; /// /// let stream = stream::iter(1..=10).skip(5); /// - /// assert_eq!(vec![6, 7, 8, 9, 10], block_on(stream.collect::>())); + /// assert_eq!(vec![6, 7, 8, 9, 10], stream.collect::>().await); + /// # }); /// ``` fn skip(self, n: u64) -> Skip where Self: Sized @@ -777,20 +807,25 @@ pub trait StreamExt: Stream { /// # Examples /// /// ``` - /// use futures::executor::block_on; + /// #![feature(async_await)] + /// # futures::executor::block_on(async { /// use futures::future; /// use futures::stream::{self, StreamExt}; /// /// let mut stream = stream::iter(1..5); /// - /// let sum = block_on(stream.by_ref() - /// .take(2) - /// .fold(0, |a, b| future::ready(a + b))); + /// let sum = stream.by_ref() + /// .take(2) + /// .fold(0, |a, b| future::ready(a + b)) + /// .await; /// assert_eq!(sum, 3); /// /// // You can use the stream again - /// let sum = block_on(stream.take(2).fold(0, |a, b| future::ready(a + b))); + /// let sum = stream.take(2) + /// .fold(0, |a, b| future::ready(a + b)) + /// .await; /// assert_eq!(sum, 7); + /// # }); /// ``` fn by_ref(&mut self) -> &mut Self where Self: Sized @@ -820,7 +855,8 @@ pub trait StreamExt: Stream { /// # Examples /// /// ``` - /// use futures::executor::block_on; + /// #![feature(async_await)] + /// # futures::executor::block_on(async { /// use futures::stream::{self, StreamExt}; /// /// let stream = stream::iter(vec![Some(10), None, Some(11)]); @@ -829,13 +865,14 @@ pub trait StreamExt: Stream { /// // Collect all the results /// let stream = stream_panicking.catch_unwind(); /// - /// let results: Vec> = block_on(stream.collect()); + /// let results: Vec> = stream.collect().await; /// match results[0] { /// Ok(10) => {} /// _ => panic!("unexpected result!"), /// } /// assert!(results[1].is_err()); /// assert_eq!(results.len(), 2); + /// # }); /// ``` #[cfg(feature = "std")] fn catch_unwind(self) -> CatchUnwind @@ -933,15 +970,18 @@ pub trait StreamExt: Stream { /// # Examples /// /// ``` - /// use futures::executor::block_on; + /// #![feature(async_await)] + /// # futures::executor::block_on(async { /// use futures::stream::{self, StreamExt}; /// /// let stream1 = stream::iter(1..=3); /// let stream2 = stream::iter(5..=10); /// - /// let vec = block_on(stream1.zip(stream2) - /// .collect::>()); + /// let vec = stream1.zip(stream2) + /// .collect::>() + /// .await; /// assert_eq!(vec![(1, 5), (2, 6), (3, 7)], vec); + /// # }); /// ``` /// fn zip(self, other: St) -> Zip @@ -957,7 +997,8 @@ pub trait StreamExt: Stream { /// first stream reaches the end, emits the elements from the second stream. /// /// ``` - /// use futures::executor::block_on; + /// #![feature(async_await)] + /// # futures::executor::block_on(async { /// use futures::stream::{self, StreamExt}; /// /// let stream1 = stream::iter(vec![Ok(10), Err(false)]); @@ -965,13 +1006,14 @@ pub trait StreamExt: Stream { /// /// let stream = stream1.chain(stream2); /// - /// let result: Vec<_> = block_on(stream.collect()); + /// let result: Vec<_> = stream.collect().await; /// assert_eq!(result, vec![ /// Ok(10), /// Err(false), /// Err(true), /// Ok(20), /// ]); + /// # }); /// ``` fn chain(self, other: St) -> Chain where St: Stream, diff --git a/futures-util/src/stream/once.rs b/futures-util/src/stream/once.rs index 966d35233f..6cb7a30ee7 100644 --- a/futures-util/src/stream/once.rs +++ b/futures-util/src/stream/once.rs @@ -7,13 +7,15 @@ use pin_utils::unsafe_pinned; /// Creates a stream of single element /// /// ``` +/// #![feature(async_await)] +/// # futures::executor::block_on(async { /// use futures::future; -/// use futures::executor::block_on; /// use futures::stream::{self, StreamExt}; /// /// let mut stream = stream::once(future::ready(17)); -/// let collected = block_on(stream.collect::>()); +/// let collected = stream.collect::>().await; /// assert_eq!(collected, vec![17]); +/// # }); /// ``` pub fn once(future: Fut) -> Once { Once { future: Some(future) } diff --git a/futures-util/src/stream/repeat.rs b/futures-util/src/stream/repeat.rs index 08c6ef054b..e2be72ab24 100644 --- a/futures-util/src/stream/repeat.rs +++ b/futures-util/src/stream/repeat.rs @@ -16,11 +16,13 @@ pub struct Repeat { /// available memory as it tries to just fill up all RAM. /// /// ``` -/// use futures::executor::block_on; +/// #![feature(async_await)] +/// # futures::executor::block_on(async { /// use futures::stream::{self, StreamExt}; /// /// let mut stream = stream::repeat(9); -/// assert_eq!(vec![9, 9, 9], block_on(stream.take(3).collect::>())); +/// assert_eq!(vec![9, 9, 9], stream.take(3).collect::>().await); +/// # }); /// ``` pub fn repeat(item: T) -> Repeat where T: Clone diff --git a/futures-util/src/stream/unfold.rs b/futures-util/src/stream/unfold.rs index 831131a1c5..f721301179 100644 --- a/futures-util/src/stream/unfold.rs +++ b/futures-util/src/stream/unfold.rs @@ -30,7 +30,8 @@ use pin_utils::{unsafe_pinned, unsafe_unpinned}; /// # Example /// /// ``` -/// use futures::executor::block_on; +/// #![feature(async_await)] +/// # futures::executor::block_on(async { /// use futures::future; /// use futures::stream::{self, StreamExt}; /// @@ -44,8 +45,9 @@ use pin_utils::{unsafe_pinned, unsafe_unpinned}; /// } /// }); /// -/// let result = block_on(stream.collect::>()); +/// let result = stream.collect::>().await; /// assert_eq!(result, vec![0, 2, 4]); +/// # }); /// ``` pub fn unfold(init: T, f: F) -> Unfold where F: FnMut(T) -> Fut, diff --git a/futures-util/src/try_stream/mod.rs b/futures-util/src/try_stream/mod.rs index a8037cf9af..ba860c9357 100644 --- a/futures-util/src/try_stream/mod.rs +++ b/futures-util/src/try_stream/mod.rs @@ -453,7 +453,6 @@ pub trait TryStreamExt: TryStream { /// #![feature(async_await)] /// # futures::executor::block_on(async { /// use futures::channel::mpsc; - /// use futures::executor::block_on; /// use futures::stream::TryStreamExt; /// use std::thread; /// @@ -495,7 +494,6 @@ pub trait TryStreamExt: TryStream { /// ``` /// #![feature(async_await)] /// # futures::executor::block_on(async { - /// use futures::executor::block_on; /// use futures::future; /// use futures::stream::{self, StreamExt, TryStreamExt}; /// @@ -535,7 +533,6 @@ pub trait TryStreamExt: TryStream { /// ``` /// #![feature(async_await)] /// # futures::executor::block_on(async { - /// use futures::executor::block_on; /// use futures::future; /// use futures::stream::{self, StreamExt, TryStreamExt}; /// @@ -611,8 +608,9 @@ pub trait TryStreamExt: TryStream { /// # Examples /// /// ``` + /// #![feature(async_await)] + /// # futures::executor::block_on(async { /// use futures::channel::mpsc; - /// use futures::executor::block_on; /// use futures::stream::TryStreamExt; /// use std::thread; /// @@ -625,9 +623,10 @@ pub trait TryStreamExt: TryStream { /// } /// }); /// - /// let result = block_on(rx.try_concat()); + /// let result = rx.try_concat().await; /// /// assert_eq!(result, Ok(vec![7, 8, 9, 4, 5, 6, 1, 2, 3])); + /// # }); /// ``` fn try_concat(self) -> TryConcat where Self: Sized, @@ -763,7 +762,6 @@ pub trait TryStreamExt: TryStream { /// ``` /// #![feature(async_await)] /// # futures::executor::block_on(async { - /// use futures::executor::block_on; /// use futures::future::lazy; /// use futures::stream::{self, StreamExt, TryStreamExt}; /// use futures::io::{AsyncRead, AsyncReadExt};