Skip to content

Commit

Permalink
Use assert_* functions for all future/stream/sink/io combinators
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Jan 15, 2021
1 parent 855541d commit 951ed2e
Show file tree
Hide file tree
Showing 35 changed files with 186 additions and 104 deletions.
5 changes: 3 additions & 2 deletions futures-util/src/future/abortable.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::assert_future;
use crate::task::AtomicWaker;
use futures_core::future::Future;
use futures_core::task::{Context, Poll};
Expand Down Expand Up @@ -39,10 +40,10 @@ impl<Fut> Abortable<Fut> where Fut: Future {
/// # });
/// ```
pub fn new(future: Fut, reg: AbortRegistration) -> Self {
Self {
assert_future::<Result<Fut::Output, Aborted>, _>(Self {
future,
inner: reg.inner,
}
})
}
}

Expand Down
7 changes: 4 additions & 3 deletions futures-util/src/future/future/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
use alloc::boxed::Box;
use core::pin::Pin;

use crate::future::{assert_future, Either};
use crate::stream::assert_stream;
use crate::fns::{inspect_fn, into_fn, ok_fn, InspectFn, IntoFn, OkFn};
use crate::future::{assert_future, Either};
use crate::never::Never;
use crate::stream::assert_stream;
#[cfg(feature = "alloc")]
use futures_core::future::{BoxFuture, LocalBoxFuture};
use futures_core::{
Expand Down Expand Up @@ -506,7 +506,8 @@ pub trait FutureExt: Future {
where
Self: Sized,
{
remote_handle::remote_handle(self)
let (wrapped, handle) = remote_handle::remote_handle(self);
(assert_future::<(), _>(wrapped), handle)
}

/// Wrap the future in a Box, pinning it.
Expand Down
25 changes: 18 additions & 7 deletions futures-util/src/future/join.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#![allow(non_snake_case)]

use crate::future::{MaybeDone, maybe_done};
use super::assert_future;
use crate::future::{maybe_done, MaybeDone};
use core::fmt;
use core::pin::Pin;
use futures_core::future::{Future, FusedFuture};
use futures_core::future::{FusedFuture, Future};
use futures_core::task::{Context, Poll};
use pin_project_lite::pin_project;

use super::assert_future;

macro_rules! generate {
($(
$(#[$doc:meta])*
Expand Down Expand Up @@ -144,7 +143,8 @@ where
Fut2: Future,
Fut3: Future,
{
Join3::new(future1, future2, future3)
let f = Join3::new(future1, future2, future3);
assert_future::<(Fut1::Output, Fut2::Output, Fut3::Output), _>(f)
}

/// Same as [`join`](join()), but with more futures.
Expand Down Expand Up @@ -176,7 +176,8 @@ where
Fut3: Future,
Fut4: Future,
{
Join4::new(future1, future2, future3, future4)
let f = Join4::new(future1, future2, future3, future4);
assert_future::<(Fut1::Output, Fut2::Output, Fut3::Output, Fut4::Output), _>(f)
}

/// Same as [`join`](join()), but with more futures.
Expand Down Expand Up @@ -211,5 +212,15 @@ where
Fut4: Future,
Fut5: Future,
{
Join5::new(future1, future2, future3, future4, future5)
let f = Join5::new(future1, future2, future3, future4, future5);
assert_future::<
(
Fut1::Output,
Fut2::Output,
Fut3::Output,
Fut4::Output,
Fut5::Output,
),
_,
>(f)
}
4 changes: 2 additions & 2 deletions futures-util/src/future/join_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use core::task::{Context, Poll};
use alloc::boxed::Box;
use alloc::vec::Vec;

use super::MaybeDone;
use super::{MaybeDone, assert_future};

fn iter_pin_mut<T>(slice: Pin<&mut [T]>) -> impl Iterator<Item = Pin<&mut T>> {
// Safety: `std` _could_ make this unsound if it were to decide Pin's
Expand Down Expand Up @@ -85,7 +85,7 @@ where
I::Item: Future,
{
let elems: Box<[_]> = i.into_iter().map(MaybeDone::Future).collect();
JoinAll { elems: elems.into() }
assert_future::<Vec<<I::Item as Future>::Output>, _>(JoinAll { elems: elems.into() })
}

impl<F> Future for JoinAll<F>
Expand Down
3 changes: 2 additions & 1 deletion futures-util/src/future/lazy.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::assert_future;
use core::pin::Pin;
use futures_core::future::{FusedFuture, Future};
use futures_core::task::{Context, Poll};
Expand Down Expand Up @@ -34,7 +35,7 @@ impl<F> Unpin for Lazy<F> {}
pub fn lazy<F, R>(f: F) -> Lazy<F>
where F: FnOnce(&mut Context<'_>) -> R,
{
Lazy { f: Some(f) }
assert_future::<R, _>(Lazy { f: Some(f) })
}

impl<F, R> FusedFuture for Lazy<F>
Expand Down
3 changes: 2 additions & 1 deletion futures-util/src/future/maybe_done.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Definition of the MaybeDone combinator

use super::assert_future;
use core::mem;
use core::pin::Pin;
use futures_core::future::{FusedFuture, Future};
Expand Down Expand Up @@ -40,7 +41,7 @@ impl<Fut: Future + Unpin> Unpin for MaybeDone<Fut> {}
/// # });
/// ```
pub fn maybe_done<Fut: Future>(future: Fut) -> MaybeDone<Fut> {
MaybeDone::Future(future)
assert_future::<(), _>(MaybeDone::Future(future))
}

impl<Fut: Future> MaybeDone<Fut> {
Expand Down
5 changes: 3 additions & 2 deletions futures-util/src/future/pending.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::assert_future;
use core::marker;
use core::pin::Pin;
use futures_core::future::{FusedFuture, Future};
Expand Down Expand Up @@ -33,9 +34,9 @@ impl<T> FusedFuture for Pending<T> {
/// # });
/// ```
pub fn pending<T>() -> Pending<T> {
Pending {
assert_future::<T, _>(Pending {
_data: marker::PhantomData,
}
})
}

impl<T> Future for Pending<T> {
Expand Down
3 changes: 2 additions & 1 deletion futures-util/src/future/poll_fn.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Definition of the `PollFn` adapter combinator

use super::assert_future;
use core::fmt;
use core::pin::Pin;
use futures_core::future::Future;
Expand Down Expand Up @@ -36,7 +37,7 @@ pub fn poll_fn<T, F>(f: F) -> PollFn<F>
where
F: FnMut(&mut Context<'_>) -> Poll<T>
{
PollFn { f }
assert_future::<T, _>(PollFn { f })
}

impl<F> fmt::Debug for PollFn<F> {
Expand Down
3 changes: 2 additions & 1 deletion futures-util/src/future/ready.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::assert_future;
use core::pin::Pin;
use futures_core::future::{FusedFuture, Future};
use futures_core::task::{Context, Poll};
Expand Down Expand Up @@ -45,7 +46,7 @@ impl<T> Future for Ready<T> {
/// # });
/// ```
pub fn ready<T>(t: T) -> Ready<T> {
Ready(Some(t))
assert_future::<T, _>(Ready(Some(t)))
}

/// Create a future that is immediately ready with a success value.
Expand Down
3 changes: 2 additions & 1 deletion futures-util/src/future/select.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::assert_future;
use core::pin::Pin;
use futures_core::future::{Future, FusedFuture};
use futures_core::task::{Context, Poll};
Expand Down Expand Up @@ -75,7 +76,7 @@ impl<A: Unpin, B: Unpin> Unpin for Select<A, B> {}
pub fn select<A, B>(future1: A, future2: B) -> Select<A, B>
where A: Future + Unpin, B: Future + Unpin
{
Select { inner: Some((future1, future2)) }
assert_future::<Either<(A::Output, B), (B::Output, A)>, _>(Select { inner: Some((future1, future2)) })
}

impl<A, B> Future for Select<A, B>
Expand Down
3 changes: 2 additions & 1 deletion futures-util/src/future/select_all.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::assert_future;
use crate::future::FutureExt;
use core::iter::FromIterator;
use core::mem;
Expand Down Expand Up @@ -38,7 +39,7 @@ pub fn select_all<I>(iter: I) -> SelectAll<I::Item>
inner: iter.into_iter().collect()
};
assert!(!ret.inner.is_empty());
ret
assert_future::<(<I::Item as Future>::Output, usize, Vec<I::Item>), _>(ret)
}

impl<Fut: Future + Unpin> Future for SelectAll<Fut> {
Expand Down
3 changes: 2 additions & 1 deletion futures-util/src/future/select_ok.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::assert_future;
use crate::future::TryFutureExt;
use core::iter::FromIterator;
use core::mem;
Expand Down Expand Up @@ -36,7 +37,7 @@ pub fn select_ok<I>(iter: I) -> SelectOk<I::Item>
inner: iter.into_iter().collect()
};
assert!(!ret.inner.is_empty(), "iterator provided to select_ok was empty");
ret
assert_future::<Result<(<I::Item as TryFuture>::Ok, Vec<I::Item>), <I::Item as TryFuture>::Error>, _>(ret)
}

impl<Fut: TryFuture + Unpin> Future for SelectOk<Fut> {
Expand Down
5 changes: 3 additions & 2 deletions futures-util/src/future/try_future/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::fns::{
UnwrapOrElseFn,
};
use crate::future::{assert_future, Inspect, Map};
use crate::sink::assert_sink;
use crate::stream::assert_stream;

// Combinators
Expand Down Expand Up @@ -173,7 +174,7 @@ pub trait TryFutureExt: TryFuture {
Self::Ok: Sink<Item, Error = Self::Error>,
Self: Sized,
{
FlattenSink::new(self)
assert_sink::<Item, Self::Error, _>(FlattenSink::new(self))
}

/// Maps this future's success value to a different value.
Expand Down Expand Up @@ -501,7 +502,7 @@ pub trait TryFutureExt: TryFuture {
Self::Ok: TryFuture<Error = Self::Error>,
Self: Sized,
{
TryFlatten::new(self)
assert_future::<Result<<Self::Ok as TryFuture>::Ok, Self::Error>, _>(TryFlatten::new(self))
}

/// Flatten the execution of this future when the successful result of this
Expand Down
16 changes: 11 additions & 5 deletions futures-util/src/future/try_join.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(non_snake_case)]

use crate::future::{TryMaybeDone, try_maybe_done};
use crate::future::{assert_future, try_maybe_done, TryMaybeDone};
use core::fmt;
use core::pin::Pin;
use futures_core::future::{Future, TryFuture};
Expand Down Expand Up @@ -150,7 +150,7 @@ where
Fut1: TryFuture,
Fut2: TryFuture<Error = Fut1::Error>,
{
TryJoin::new(future1, future2)
assert_future::<Result<(Fut1::Ok, Fut2::Ok), Fut1::Error>, _>(TryJoin::new(future1, future2))
}

/// Same as [`try_join`](try_join()), but with more futures.
Expand Down Expand Up @@ -179,7 +179,9 @@ where
Fut2: TryFuture<Error = Fut1::Error>,
Fut3: TryFuture<Error = Fut1::Error>,
{
TryJoin3::new(future1, future2, future3)
assert_future::<Result<(Fut1::Ok, Fut2::Ok, Fut3::Ok), Fut1::Error>, _>(TryJoin3::new(
future1, future2, future3,
))
}

/// Same as [`try_join`](try_join()), but with more futures.
Expand Down Expand Up @@ -211,7 +213,9 @@ where
Fut3: TryFuture<Error = Fut1::Error>,
Fut4: TryFuture<Error = Fut1::Error>,
{
TryJoin4::new(future1, future2, future3, future4)
assert_future::<Result<(Fut1::Ok, Fut2::Ok, Fut3::Ok, Fut4::Ok), Fut1::Error>, _>(
TryJoin4::new(future1, future2, future3, future4),
)
}

/// Same as [`try_join`](try_join()), but with more futures.
Expand Down Expand Up @@ -246,5 +250,7 @@ where
Fut4: TryFuture<Error = Fut1::Error>,
Fut5: TryFuture<Error = Fut1::Error>,
{
TryJoin5::new(future1, future2, future3, future4, future5)
assert_future::<Result<(Fut1::Ok, Fut2::Ok, Fut3::Ok, Fut4::Ok, Fut5::Ok), Fut1::Error>, _>(
TryJoin5::new(future1, future2, future3, future4, future5),
)
}
6 changes: 3 additions & 3 deletions futures-util/src/future/try_join_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use core::task::{Context, Poll};
use alloc::boxed::Box;
use alloc::vec::Vec;

use super::{TryFuture, TryMaybeDone};
use super::{assert_future, TryFuture, TryMaybeDone};

fn iter_pin_mut<T>(slice: Pin<&mut [T]>) -> impl Iterator<Item = Pin<&mut T>> {
// Safety: `std` _could_ make this unsound if it were to decide Pin's
Expand Down Expand Up @@ -93,9 +93,9 @@ where
I::Item: TryFuture,
{
let elems: Box<[_]> = i.into_iter().map(TryMaybeDone::Future).collect();
TryJoinAll {
assert_future::<Result<Vec<<I::Item as TryFuture>::Ok>, <I::Item as TryFuture>::Error>, _>(TryJoinAll {
elems: elems.into(),
}
})
}

impl<F> Future for TryJoinAll<F>
Expand Down
3 changes: 2 additions & 1 deletion futures-util/src/future/try_maybe_done.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Definition of the TryMaybeDone combinator

use super::assert_future;
use core::mem;
use core::pin::Pin;
use futures_core::future::{FusedFuture, Future, TryFuture};
Expand All @@ -25,7 +26,7 @@ impl<Fut: TryFuture + Unpin> Unpin for TryMaybeDone<Fut> {}

/// Wraps a future into a `TryMaybeDone`
pub fn try_maybe_done<Fut: TryFuture>(future: Fut) -> TryMaybeDone<Fut> {
TryMaybeDone::Future(future)
assert_future::<Result<(), Fut::Error>, _>(TryMaybeDone::Future(future))
}

impl<Fut: TryFuture> TryMaybeDone<Fut> {
Expand Down
5 changes: 4 additions & 1 deletion futures-util/src/future/try_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ impl<A: Unpin, B: Unpin> Unpin for TrySelect<A, B> {}
pub fn try_select<A, B>(future1: A, future2: B) -> TrySelect<A, B>
where A: TryFuture + Unpin, B: TryFuture + Unpin
{
TrySelect { inner: Some((future1, future2)) }
super::assert_future::<Result<
Either<(A::Ok, B), (B::Ok, A)>,
Either<(A::Error, B), (B::Error, A)>,
>, _>(TrySelect { inner: Some((future1, future2)) })
}

impl<A: Unpin, B: Unpin> Future for TrySelect<A, B>
Expand Down
Loading

0 comments on commit 951ed2e

Please sign in to comment.