Skip to content

Commit

Permalink
Use await syntax for most examples
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e authored and cramertj committed May 10, 2019
1 parent 0fbab45 commit 99e25aa
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 71 deletions.
5 changes: 3 additions & 2 deletions futures-test/src/future/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<i32>();
///
/// (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
Expand Down
13 changes: 9 additions & 4 deletions futures-util/src/future/abortable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ impl<Fut> Abortable<Fut> 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 {
Expand Down Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion futures-util/src/future/fuse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl<Fut: Future> Fuse<Fut> {
/// # Examples
///
/// ```
/// #![feature(async_await, futures_api)]
/// #![feature(async_await)]
/// # futures::executor::block_on(async {
/// use futures::channel::mpsc;
/// use futures::future::{Fuse, FusedFuture, FutureExt};
Expand Down
5 changes: 4 additions & 1 deletion futures-util/src/future/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Self>
Expand Down
8 changes: 5 additions & 3 deletions futures-util/src/sink/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ pub trait SinkExt<Item>: Sink<Item> {
/// # 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;
Expand All @@ -98,10 +99,11 @@ pub trait SinkExt<Item>: Sink<Item> {
/// VecDeque::from(vec![Ok(42); x])
/// });
///
/// block_on(tx.send(5)).unwrap();
/// tx.send(5).await.unwrap();
/// drop(tx);
/// let received: Vec<i32> = block_on(rx.collect());
/// let received: Vec<i32> = rx.collect().await;
/// assert_eq!(received, vec![42, 42, 42, 42, 42]);
/// # });
/// ```
fn with_flat_map<U, St, F>(self, f: F) -> WithFlatMap<Self, Item, U, St, F>
where F: FnMut(U) -> St,
Expand Down
6 changes: 4 additions & 2 deletions futures-util/src/stream/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ impl<I> Unpin for Iter<I> {}
/// 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::<Vec<i32>>()));
/// assert_eq!(vec![17, 19], stream.collect::<Vec<i32>>().await);
/// # });
/// ```
pub fn iter<I>(i: I) -> Iter<I::IntoIter>
where I: IntoIterator,
Expand Down
Loading

0 comments on commit 99e25aa

Please sign in to comment.