Skip to content

Commit

Permalink
Move type/trait aliases to futures-util
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Feb 13, 2021
1 parent f71d3f7 commit 19dc254
Show file tree
Hide file tree
Showing 38 changed files with 172 additions and 188 deletions.
50 changes: 0 additions & 50 deletions futures-core/src/future.rs
Expand Up @@ -2,20 +2,10 @@

use core::ops::DerefMut;
use core::pin::Pin;
use core::task::{Context, Poll};

#[doc(no_inline)]
pub use core::future::Future;

/// An owned dynamically typed [`Future`] for use in cases where you can't
/// statically type your result or need to add some indirection.
#[cfg(feature = "alloc")]
pub type BoxFuture<'a, T> = Pin<alloc::boxed::Box<dyn Future<Output = T> + Send + 'a>>;

/// `BoxFuture`, but without the `Send` requirement.
#[cfg(feature = "alloc")]
pub type LocalBoxFuture<'a, T> = Pin<alloc::boxed::Box<dyn Future<Output = T> + 'a>>;

/// A future which tracks whether or not the underlying future
/// should no longer be polled.
///
Expand Down Expand Up @@ -45,46 +35,6 @@ where
}
}

mod private_try_future {
use super::Future;

pub trait Sealed {}

impl<F, T, E> Sealed for F where F: ?Sized + Future<Output = Result<T, E>> {}
}

/// A convenience for futures that return `Result` values that includes
/// a variety of adapters tailored to such futures.
pub trait TryFuture: Future + private_try_future::Sealed {
/// The type of successful values yielded by this future
type Ok;

/// The type of failures yielded by this future
type Error;

/// Poll this `TryFuture` as if it were a `Future`.
///
/// This method is a stopgap for a compiler limitation that prevents us from
/// directly inheriting from the `Future` trait; in the future it won't be
/// needed.
fn try_poll(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<Self::Ok, Self::Error>>;
}

impl<F, T, E> TryFuture for F
where F: ?Sized + Future<Output = Result<T, E>>
{
type Ok = T;
type Error = E;

#[inline]
fn try_poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.poll(cx)
}
}

#[cfg(feature = "alloc")]
mod if_alloc {
use alloc::boxed::Box;
Expand Down
4 changes: 2 additions & 2 deletions futures-core/src/lib.rs
Expand Up @@ -17,10 +17,10 @@ compile_error!("The `cfg-target-has-atomic` feature requires the `unstable` feat
extern crate alloc;

pub mod future;
#[doc(hidden)] pub use self::future::{Future, FusedFuture, TryFuture};
#[doc(hidden)] pub use self::future::{Future, FusedFuture};

pub mod stream;
#[doc(hidden)] pub use self::stream::{Stream, FusedStream, TryStream};
#[doc(hidden)] pub use self::stream::{Stream, FusedStream};

#[macro_use]
pub mod task;
Expand Down
48 changes: 0 additions & 48 deletions futures-core/src/stream.rs
Expand Up @@ -4,15 +4,6 @@ use core::ops::DerefMut;
use core::pin::Pin;
use core::task::{Context, Poll};

/// An owned dynamically typed [`Stream`] for use in cases where you can't
/// statically type your result or need to add some indirection.
#[cfg(feature = "alloc")]
pub type BoxStream<'a, T> = Pin<alloc::boxed::Box<dyn Stream<Item = T> + Send + 'a>>;

/// `BoxStream`, but without the `Send` requirement.
#[cfg(feature = "alloc")]
pub type LocalBoxStream<'a, T> = Pin<alloc::boxed::Box<dyn Stream<Item = T> + 'a>>;

/// A stream of values produced asynchronously.
///
/// If `Future<Output = T>` is an asynchronous version of `T`, then `Stream<Item
Expand Down Expand Up @@ -163,45 +154,6 @@ where
}
}

mod private_try_stream {
use super::Stream;

pub trait Sealed {}

impl<S, T, E> Sealed for S where S: ?Sized + Stream<Item = Result<T, E>> {}
}

/// A convenience for streams that return `Result` values that includes
/// a variety of adapters tailored to such futures.
pub trait TryStream: Stream + private_try_stream::Sealed {
/// The type of successful values yielded by this future
type Ok;

/// The type of failures yielded by this future
type Error;

/// Poll this `TryStream` as if it were a `Stream`.
///
/// This method is a stopgap for a compiler limitation that prevents us from
/// directly inheriting from the `Stream` trait; in the future it won't be
/// needed.
fn try_poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>)
-> Poll<Option<Result<Self::Ok, Self::Error>>>;
}

impl<S, T, E> TryStream for S
where S: ?Sized + Stream<Item = Result<T, E>>
{
type Ok = T;
type Error = E;

fn try_poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>)
-> Poll<Option<Result<Self::Ok, Self::Error>>>
{
self.poll_next(cx)
}
}

#[cfg(feature = "alloc")]
mod if_alloc {
use alloc::boxed::Box;
Expand Down
2 changes: 1 addition & 1 deletion futures-util/src/compat/compat03as01.rs
Expand Up @@ -6,7 +6,7 @@ use futures_01::{
use futures_01::{
AsyncSink as AsyncSink01, Sink as Sink01, StartSend as StartSend01,
};
use futures_core::{
use crate::{
task::{RawWaker, RawWakerVTable},
future::TryFuture as TryFuture03,
stream::TryStream as TryStream03,
Expand Down
2 changes: 1 addition & 1 deletion futures-util/src/future/future/mod.rs
Expand Up @@ -12,7 +12,7 @@ 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 crate::future::{BoxFuture, LocalBoxFuture};
use futures_core::{
future::Future,
stream::Stream,
Expand Down
59 changes: 56 additions & 3 deletions futures-util/src/future/mod.rs
Expand Up @@ -9,14 +9,67 @@
//! from a closure that defines its return value, and [`ready`](ready()),
//! which constructs a future with an immediate defined value.

use core::pin::Pin;
use futures_core::task::{Poll, Context};
#[cfg(feature = "alloc")]
use alloc::boxed::Box;

#[doc(no_inline)]
pub use core::future::Future;

#[cfg(feature = "alloc")]
pub use futures_core::future::{BoxFuture, LocalBoxFuture};
pub use futures_core::future::{FusedFuture, TryFuture};
pub use futures_core::future::FusedFuture;
pub use futures_task::{FutureObj, LocalFutureObj, UnsafeFutureObj};

/// An owned dynamically typed [`Future`] for use in cases where you can't
/// statically type your result or need to add some indirection.
#[cfg(feature = "alloc")]
pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;

/// `BoxFuture`, but without the `Send` requirement.
#[cfg(feature = "alloc")]
pub type LocalBoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + 'a>>;


mod private_try_future {
use super::Future;

pub trait Sealed {}

impl<F, T, E> Sealed for F where F: ?Sized + Future<Output = Result<T, E>> {}
}

/// A convenience for futures that return `Result` values that includes
/// a variety of adapters tailored to such futures.
pub trait TryFuture: Future + private_try_future::Sealed {
/// The type of successful values yielded by this future
type Ok;

/// The type of failures yielded by this future
type Error;

/// Poll this `TryFuture` as if it were a `Future`.
///
/// This method is a stopgap for a compiler limitation that prevents us from
/// directly inheriting from the `Future` trait; in the future it won't be
/// needed.
fn try_poll(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<Self::Ok, Self::Error>>;
}

impl<F, T, E> TryFuture for F
where F: ?Sized + Future<Output = Result<T, E>>
{
type Ok = T;
type Error = E;

#[inline]
fn try_poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.poll(cx)
}
}

// Extension traits and combinators
#[allow(clippy::module_inception)]
mod future;
Expand Down
3 changes: 1 addition & 2 deletions futures-util/src/future/select_ok.rs
@@ -1,10 +1,9 @@
use super::assert_future;
use crate::future::TryFutureExt;
use crate::future::{Future, TryFuture, TryFutureExt};
use core::iter::FromIterator;
use core::mem;
use core::pin::Pin;
use alloc::vec::Vec;
use futures_core::future::{Future, TryFuture};
use futures_core::task::{Context, Poll};

/// Future for the [`select_ok`] function.
Expand Down
2 changes: 1 addition & 1 deletion futures-util/src/future/try_future/into_future.rs
@@ -1,5 +1,5 @@
use crate::future::{FusedFuture, Future, TryFuture};
use core::pin::Pin;
use futures_core::future::{FusedFuture, Future, TryFuture};
use futures_core::task::{Context, Poll};
use pin_project_lite::pin_project;

Expand Down
4 changes: 2 additions & 2 deletions futures-util/src/future/try_future/mod.rs
Expand Up @@ -5,12 +5,12 @@

#[cfg(feature = "compat")]
use crate::compat::Compat;
use core::pin::Pin;
use futures_core::{
use crate::{
future::TryFuture,
stream::TryStream,
task::{Context, Poll},
};
use core::pin::Pin;
#[cfg(feature = "sink")]
use futures_sink::Sink;

Expand Down
4 changes: 2 additions & 2 deletions futures-util/src/future/try_future/try_flatten.rs
@@ -1,7 +1,7 @@
use crate::future::{FusedFuture, Future, TryFuture};
use crate::stream::{FusedStream, Stream, TryStream};
use core::pin::Pin;
use futures_core::future::{FusedFuture, Future, TryFuture};
use futures_core::ready;
use futures_core::stream::{FusedStream, Stream, TryStream};
#[cfg(feature = "sink")]
use futures_sink::Sink;
use futures_core::task::{Context, Poll};
Expand Down
2 changes: 1 addition & 1 deletion futures-util/src/future/try_future/try_flatten_err.rs
@@ -1,5 +1,5 @@
use crate::future::{FusedFuture, Future, TryFuture};
use core::pin::Pin;
use futures_core::future::{FusedFuture, Future, TryFuture};
use futures_core::ready;
use futures_core::task::{Context, Poll};
use pin_project_lite::pin_project;
Expand Down
3 changes: 1 addition & 2 deletions futures-util/src/future/try_join.rs
@@ -1,9 +1,8 @@
#![allow(non_snake_case)]

use crate::future::{assert_future, try_maybe_done, TryMaybeDone};
use crate::future::{assert_future, try_maybe_done, Future, TryFuture, TryMaybeDone};
use core::fmt;
use core::pin::Pin;
use futures_core::future::{Future, TryFuture};
use futures_core::task::{Context, Poll};
use pin_project_lite::pin_project;

Expand Down
3 changes: 1 addition & 2 deletions futures-util/src/future/try_maybe_done.rs
@@ -1,9 +1,8 @@
//! Definition of the TryMaybeDone combinator

use super::assert_future;
use crate::future::{assert_future, FusedFuture, Future, TryFuture};
use core::mem;
use core::pin::Pin;
use futures_core::future::{FusedFuture, Future, TryFuture};
use futures_core::ready;
use futures_core::task::{Context, Poll};

Expand Down
3 changes: 1 addition & 2 deletions futures-util/src/future/try_select.rs
@@ -1,7 +1,6 @@
use crate::future::{Either, Future, TryFuture, TryFutureExt};
use core::pin::Pin;
use futures_core::future::{Future, TryFuture};
use futures_core::task::{Context, Poll};
use crate::future::{Either, TryFutureExt};

/// Future for the [`try_select()`] function.
#[must_use = "futures do nothing unless you `.await` or poll them"]
Expand Down
5 changes: 2 additions & 3 deletions futures-util/src/sink/mod.rs
Expand Up @@ -6,10 +6,9 @@
//! - The [`SinkExt`] trait, which provides adapters for chaining and composing
//! sinks.

use crate::future::{assert_future, Either};
use crate::future::{assert_future, Either, Future};
use crate::stream::{Stream, TryStream};
use core::pin::Pin;
use futures_core::future::Future;
use futures_core::stream::{Stream, TryStream};
use futures_core::task::{Context, Poll};

#[cfg(feature = "compat")]
Expand Down
5 changes: 2 additions & 3 deletions futures-util/src/sink/send_all.rs
@@ -1,9 +1,8 @@
use crate::stream::{StreamExt, TryStreamExt, Fuse};
use crate::future::Future;
use crate::stream::{Fuse, Stream, StreamExt, TryStream, TryStreamExt};
use core::fmt;
use core::pin::Pin;
use futures_core::future::Future;
use futures_core::ready;
use futures_core::stream::{TryStream, Stream};
use futures_core::task::{Context, Poll};
use futures_sink::Sink;

Expand Down

0 comments on commit 19dc254

Please sign in to comment.