Skip to content

Commit 69cf4ab

Browse files
committed
Remove StreamObj, caution against using FutureObj
1 parent 82d9538 commit 69cf4ab

File tree

10 files changed

+47
-293
lines changed

10 files changed

+47
-293
lines changed

futures-core/src/future/future_obj.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ use core::{
99
/// A custom trait object for polling futures, roughly akin to
1010
/// `Box<dyn Future<Output = T> + 'a>`.
1111
///
12-
/// This custom trait object was introduced for two reasons:
13-
/// - Currently it is not possible to take `dyn Trait` by value and
14-
/// `Box<dyn Trait>` is not available in no_std contexts.
12+
/// This custom trait object was introduced as currently it is not possible to
13+
/// take `dyn Trait` by value and `Box<dyn Trait>` is not available in no_std
14+
/// contexts.
1515
pub struct LocalFutureObj<'a, T> {
1616
ptr: *mut (),
1717
poll_fn: unsafe fn(*mut (), &mut Context<'_>) -> Poll<T>,
@@ -79,14 +79,13 @@ impl<'a, T> Drop for LocalFutureObj<'a, T> {
7979
/// A custom trait object for polling futures, roughly akin to
8080
/// `Box<dyn Future<Output = T> + Send + 'a>`.
8181
///
82-
/// This custom trait object was introduced for two reasons:
83-
/// - Currently it is not possible to take `dyn Trait` by value and
84-
/// `Box<dyn Trait>` is not available in no_std contexts.
85-
/// - The `Future` trait is currently not object safe: The `Future::poll`
86-
/// method makes uses the arbitrary self types feature and traits in which
87-
/// this feature is used are currently not object safe due to current compiler
88-
/// limitations. (See tracking issue for arbitrary self types for more
89-
/// information #44874)
82+
/// This custom trait object was introduced as currently it is not possible to
83+
/// take `dyn Trait` by value and `Box<dyn Trait>` is not available in no_std
84+
/// contexts.
85+
///
86+
/// You should generally not need to use this type outside of `no_std` or when
87+
/// implementing `Spawn`, consider using [`BoxFuture`](crate::future::BoxFuture)
88+
/// instead.
9089
pub struct FutureObj<'a, T>(LocalFutureObj<'a, T>);
9190

9291
impl<'a, T> Unpin for FutureObj<'a, T> {}

futures-core/src/future/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ pub use core::future::Future;
88
mod future_obj;
99
pub use self::future_obj::{FutureObj, LocalFutureObj, UnsafeFutureObj};
1010

11+
#[cfg(feature = "alloc")]
12+
/// An owned dynamically typed [`Future`] for use in cases where you can't
13+
/// statically type your result or need to add some indirection.
14+
pub type BoxFuture<'a, T> = Pin<alloc::boxed::Box<dyn Future<Output = T> + Send + 'a>>;
15+
1116
/// A `Future` or `TryFuture` which tracks whether or not the underlying future
1217
/// should no longer be polled.
1318
///

futures-core/src/stream/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ use core::task::{Context, Poll};
77
#[cfg(feature = "either")]
88
use either::Either;
99

10-
mod stream_obj;
11-
pub use self::stream_obj::{StreamObj,LocalStreamObj,UnsafeStreamObj};
10+
#[cfg(feature = "alloc")]
11+
/// An owned dynamically typed [`Stream`] for use in cases where you can't
12+
/// statically type your result or need to add some indirection.
13+
pub type BoxStream<'a, T> = Pin<alloc::boxed::Box<dyn Stream<Item = T> + Send + 'a>>;
1214

1315
/// A stream of values produced asynchronously.
1416
///

futures-core/src/stream/stream_obj.rs

-258
This file was deleted.

futures-util/src/future/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use futures_core::stream::Stream;
99
use futures_core::task::{Context, Poll};
1010
#[cfg(feature = "alloc")]
1111
use alloc::boxed::Box;
12+
#[cfg(feature = "alloc")]
13+
use futures_core::future::BoxFuture;
1214

1315
// re-export for `select!`
1416
#[doc(hidden)]
@@ -524,8 +526,8 @@ pub trait FutureExt: Future {
524526

525527
/// Wrap the future in a Box, pinning it.
526528
#[cfg(feature = "alloc")]
527-
fn boxed(self) -> Pin<Box<Self>>
528-
where Self: Sized
529+
fn boxed(self) -> BoxFuture<'static, Self::Output>
530+
where Self: Sized + Send + 'static
529531
{
530532
Box::pin(self)
531533
}

futures/src/lib.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ pub mod future {
185185
FutureObj, LocalFutureObj, UnsafeFutureObj,
186186
};
187187

188+
#[cfg(feature = "alloc")]
189+
pub use futures_core::future::BoxFuture;
190+
188191
pub use futures_util::future::{
189192
empty, Empty,
190193
lazy, Lazy,
@@ -326,9 +329,11 @@ pub mod stream {
326329
327330
pub use futures_core::stream::{
328331
Stream, TryStream, FusedStream,
329-
StreamObj, LocalStreamObj, UnsafeStreamObj,
330332
};
331333

334+
#[cfg(feature = "alloc")]
335+
pub use futures_core::stream::BoxStream;
336+
332337
pub use futures_util::stream::{
333338
iter, Iter,
334339
repeat, Repeat,

futures/tests/future_obj.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use futures::task::{Context, Poll};
66

77
#[test]
88
fn dropping_does_not_segfault() {
9-
FutureObj::new(async { String::new() }.boxed());
9+
FutureObj::new(Box::new(async { String::new() }));
1010
}
1111

1212
#[test]
@@ -29,7 +29,7 @@ fn dropping_drops_the_future() {
2929
}
3030
}
3131

32-
FutureObj::new(Inc(&mut times_dropped).boxed());
32+
FutureObj::new(Box::new(Inc(&mut times_dropped)));
3333

3434
assert_eq!(times_dropped, 1);
3535
}

0 commit comments

Comments
 (0)