Skip to content

Commit ab7743d

Browse files
taiki-ecramertj
authored andcommitted
Change StreamExt::boxed to return BoxStream and add StreamExt::boxed_local
1 parent 8b72bc9 commit ab7743d

File tree

5 files changed

+33
-8
lines changed

5 files changed

+33
-8
lines changed

futures-core/src/future/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ pub use core::future::Future;
99
mod future_obj;
1010
pub use self::future_obj::{FutureObj, LocalFutureObj, UnsafeFutureObj};
1111

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

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

2121
/// A `Future` or `TryFuture` which tracks whether or not the underlying future

futures-core/src/stream.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ use core::ops::DerefMut;
44
use core::pin::Pin;
55
use core::task::{Context, Poll};
66

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

12+
/// `BoxStream`, but without the `Send` requirement.
13+
#[cfg(feature = "alloc")]
14+
pub type LocalBoxStream<'a, T> = Pin<alloc::boxed::Box<dyn Stream<Item = T> + 'a>>;
15+
1216
/// A stream of values produced asynchronously.
1317
///
1418
/// If `Future<Output = T>` is an asynchronous version of `T`, then `Stream<Item

futures-util/src/future/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,9 @@ pub trait FutureExt: Future {
492492
}
493493

494494
/// Wrap the future in a Box, pinning it.
495+
///
496+
/// This method is only available when the `std` or `alloc` feature of this
497+
/// library is activated, and it is activated by default.
495498
#[cfg(feature = "alloc")]
496499
fn boxed<'a>(self) -> BoxFuture<'a, Self::Output>
497500
where Self: Sized + Send + 'a
@@ -502,6 +505,9 @@ pub trait FutureExt: Future {
502505
/// Wrap the future in a Box, pinning it.
503506
///
504507
/// Similar to `boxed`, but without the `Send` requirement.
508+
///
509+
/// This method is only available when the `std` or `alloc` feature of this
510+
/// library is activated, and it is activated by default.
505511
#[cfg(feature = "alloc")]
506512
fn boxed_local<'a>(self) -> LocalBoxFuture<'a, Self::Output>
507513
where Self: Sized + 'a

futures-util/src/stream/mod.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! This module contains a number of functions for working with `Stream`s,
44
//! including the `StreamExt` trait which adds methods to `Stream` types.
55
6+
use crate::future::Either;
67
use core::pin::Pin;
78
use futures_core::future::Future;
89
use futures_core::stream::{FusedStream, Stream};
@@ -13,7 +14,8 @@ use futures_core::task::{Context, Poll};
1314
use futures_sink::Sink;
1415
#[cfg(feature = "alloc")]
1516
use alloc::boxed::Box;
16-
use crate::future::Either;
17+
#[cfg(feature = "alloc")]
18+
use futures_core::stream::{BoxStream, LocalBoxStream};
1719

1820
mod iter;
1921
pub use self::iter::{iter, Iter};
@@ -894,8 +896,21 @@ pub trait StreamExt: Stream {
894896
/// This method is only available when the `std` or `alloc` feature of this
895897
/// library is activated, and it is activated by default.
896898
#[cfg(feature = "alloc")]
897-
fn boxed(self) -> Pin<Box<Self>>
898-
where Self: Sized
899+
fn boxed<'a>(self) -> BoxStream<'a, Self::Item>
900+
where Self: Sized + Send + 'a
901+
{
902+
Box::pin(self)
903+
}
904+
905+
/// Wrap the stream in a Box, pinning it.
906+
///
907+
/// Similar to `boxed`, but without the `Send` requirement.
908+
///
909+
/// This method is only available when the `std` or `alloc` feature of this
910+
/// library is activated, and it is activated by default.
911+
#[cfg(feature = "alloc")]
912+
fn boxed_local<'a>(self) -> LocalBoxStream<'a, Self::Item>
913+
where Self: Sized + 'a
899914
{
900915
Box::pin(self)
901916
}

futures/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ pub mod future {
213213
};
214214

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

218218
pub use futures_util::future::{
219219
lazy, Lazy,
@@ -398,7 +398,7 @@ pub mod stream {
398398
};
399399

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

403403
pub use futures_util::stream::{
404404
iter, Iter,

0 commit comments

Comments
 (0)