Skip to content

Commit

Permalink
Use assert_* functions for all future/stream/sink/io combinators (rus…
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e authored Jan 17, 2021
1 parent 69277e0 commit a2063bc
Show file tree
Hide file tree
Showing 35 changed files with 177 additions and 89 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
3 changes: 2 additions & 1 deletion futures-util/src/future/future/mod.rs
Original file line number Diff line number Diff line change
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
12 changes: 7 additions & 5 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 super::assert_future;
use crate::future::{maybe_done, MaybeDone};
use core::fmt;
use core::pin::Pin;
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,6 @@ 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::mem;
use core::pin::Pin;
use core::task::{Context, Poll};

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

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 @@ -81,7 +81,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 @@ -35,7 +36,7 @@ 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
3 changes: 2 additions & 1 deletion 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,7 +34,7 @@ impl<T> FusedFuture for Pending<T> {
/// # });
/// ```
pub fn pending<T>() -> Pending<T> {
Pending { _data: marker::PhantomData }
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
5 changes: 4 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 crate::future::{Either, FutureExt};
use core::pin::Pin;
use futures_core::future::{FusedFuture, Future};
Expand Down Expand Up @@ -77,7 +78,9 @@ 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 alloc::vec::Vec;
use core::iter::FromIterator;
Expand Down Expand Up @@ -37,7 +38,7 @@ where
{
let ret = SelectAll { 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
6 changes: 5 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 alloc::vec::Vec;
use core::iter::FromIterator;
Expand Down Expand Up @@ -35,7 +36,10 @@ where
{
let ret = SelectOk { 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
4 changes: 2 additions & 2 deletions futures-util/src/future/try_future/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ pub trait TryFutureExt: TryFuture {
Self::Ok: Sink<Item, Error = Self::Error>,
Self: Sized,
{
FlattenSink::new(self)
crate::sink::assert_sink::<Item, Self::Error, _>(FlattenSink::new(self))
}

/// Maps this future's success value to a different value.
Expand Down Expand Up @@ -501,7 +501,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::{try_maybe_done, TryMaybeDone};
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: 4 additions & 2 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::mem;
use core::pin::Pin;
use core::task::{Context, Poll};

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 @@ -89,7 +89,9 @@ where
I::Item: TryFuture,
{
let elems: Box<[_]> = i.into_iter().map(TryMaybeDone::Future).collect();
TryJoinAll { elems: elems.into() }
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 @@ -52,7 +52,10 @@ 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 a2063bc

Please sign in to comment.