Skip to content

Commit 2bf5e98

Browse files
committed
Remove StreamObj, caution against using FutureObj
1 parent 82d9538 commit 2bf5e98

File tree

8 files changed

+44
-293
lines changed

8 files changed

+44
-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/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/futures_ordered.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
use futures::channel::oneshot;
44
use futures::executor::{block_on, block_on_stream};
5-
use futures::future::{self, join, FutureExt, FutureObj};
6-
use futures::stream::{StreamExt, FuturesOrdered};
7-
use futures::task::Context;
5+
use futures::future::{self, FutureExt, BoxFuture};
6+
use futures::stream::{StreamExt, futures_ordered, FuturesOrdered};
87
use futures_test::task::noop_waker_ref;
98

109
#[test]
@@ -34,10 +33,10 @@ fn works_2() {
3433
let (b_tx, b_rx) = oneshot::channel::<i32>();
3534
let (c_tx, c_rx) = oneshot::channel::<i32>();
3635

37-
let mut stream = vec![
38-
FutureObj::new(Box::new(a_rx)),
39-
FutureObj::new(Box::new(join(b_rx, c_rx).map(|(a, b)| Ok(a? + b?)))),
40-
].into_iter().collect::<FuturesOrdered<_>>();
36+
let mut stream = futures_ordered(vec![
37+
a_rx.boxed(),
38+
join(b_rx, c_rx).map(|(a, b)| Ok(a? + b?)).boxed(),
39+
]);
4140

4241
let mut cx = Context::from_waker(noop_waker_ref());
4342
a_tx.send(33).unwrap();

0 commit comments

Comments
 (0)