Skip to content

Commit

Permalink
Change StreamExt::boxed to return BoxStream and add StreamExt::boxed_…
Browse files Browse the repository at this point in the history
…local
  • Loading branch information
taiki-e authored and cramertj committed Aug 6, 2019
1 parent 8b72bc9 commit ab7743d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
4 changes: 2 additions & 2 deletions futures-core/src/future/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ pub use core::future::Future;
mod future_obj;
pub use self::future_obj::{FutureObj, LocalFutureObj, UnsafeFutureObj};

#[cfg(feature = "alloc")]
/// 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>>;

#[cfg(feature = "alloc")]
/// `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` or `TryFuture` which tracks whether or not the underlying future
Expand Down
6 changes: 5 additions & 1 deletion futures-core/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ use core::ops::DerefMut;
use core::pin::Pin;
use core::task::{Context, Poll};

#[cfg(feature = "alloc")]
/// 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
6 changes: 6 additions & 0 deletions futures-util/src/future/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,9 @@ pub trait FutureExt: Future {
}

/// Wrap the future in a Box, pinning it.
///
/// This method is only available when the `std` or `alloc` feature of this
/// library is activated, and it is activated by default.
#[cfg(feature = "alloc")]
fn boxed<'a>(self) -> BoxFuture<'a, Self::Output>
where Self: Sized + Send + 'a
Expand All @@ -502,6 +505,9 @@ pub trait FutureExt: Future {
/// Wrap the future in a Box, pinning it.
///
/// Similar to `boxed`, but without the `Send` requirement.
///
/// This method is only available when the `std` or `alloc` feature of this
/// library is activated, and it is activated by default.
#[cfg(feature = "alloc")]
fn boxed_local<'a>(self) -> LocalBoxFuture<'a, Self::Output>
where Self: Sized + 'a
Expand Down
21 changes: 18 additions & 3 deletions futures-util/src/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! This module contains a number of functions for working with `Stream`s,
//! including the `StreamExt` trait which adds methods to `Stream` types.
use crate::future::Either;
use core::pin::Pin;
use futures_core::future::Future;
use futures_core::stream::{FusedStream, Stream};
Expand All @@ -13,7 +14,8 @@ use futures_core::task::{Context, Poll};
use futures_sink::Sink;
#[cfg(feature = "alloc")]
use alloc::boxed::Box;
use crate::future::Either;
#[cfg(feature = "alloc")]
use futures_core::stream::{BoxStream, LocalBoxStream};

mod iter;
pub use self::iter::{iter, Iter};
Expand Down Expand Up @@ -894,8 +896,21 @@ pub trait StreamExt: Stream {
/// This method is only available when the `std` or `alloc` feature of this
/// library is activated, and it is activated by default.
#[cfg(feature = "alloc")]
fn boxed(self) -> Pin<Box<Self>>
where Self: Sized
fn boxed<'a>(self) -> BoxStream<'a, Self::Item>
where Self: Sized + Send + 'a
{
Box::pin(self)
}

/// Wrap the stream in a Box, pinning it.
///
/// Similar to `boxed`, but without the `Send` requirement.
///
/// This method is only available when the `std` or `alloc` feature of this
/// library is activated, and it is activated by default.
#[cfg(feature = "alloc")]
fn boxed_local<'a>(self) -> LocalBoxStream<'a, Self::Item>
where Self: Sized + 'a
{
Box::pin(self)
}
Expand Down
4 changes: 2 additions & 2 deletions futures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ pub mod future {
};

#[cfg(feature = "alloc")]
pub use futures_core::future::BoxFuture;
pub use futures_core::future::{BoxFuture, LocalBoxFuture};

pub use futures_util::future::{
lazy, Lazy,
Expand Down Expand Up @@ -398,7 +398,7 @@ pub mod stream {
};

#[cfg(feature = "alloc")]
pub use futures_core::stream::BoxStream;
pub use futures_core::stream::{BoxStream, LocalBoxStream};

pub use futures_util::stream::{
iter, Iter,
Expand Down

0 comments on commit ab7743d

Please sign in to comment.