Skip to content

Commit 31948bd

Browse files
chmlncramertj
authored andcommitted
Non-Send boxed() (#1563)
Add BoxFutureLocal and boxed_local() function
1 parent 3309357 commit 31948bd

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

futures-core/src/future/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ pub use self::future_obj::{FutureObj, LocalFutureObj, UnsafeFutureObj};
1414
/// statically type your result or need to add some indirection.
1515
pub type BoxFuture<'a, T> = Pin<alloc::boxed::Box<dyn Future<Output = T> + Send + 'a>>;
1616

17+
#[cfg(feature = "alloc")]
18+
/// `BoxFuture`, but without the `Send` requirement.
19+
pub type LocalBoxFuture<'a, T> = Pin<alloc::boxed::Box<dyn Future<Output = T> + 'a>>;
20+
1721
/// A `Future` or `TryFuture` which tracks whether or not the underlying future
1822
/// should no longer be polled.
1923
///

futures-util/src/future/mod.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use futures_core::task::{Context, Poll};
1010
#[cfg(feature = "alloc")]
1111
use alloc::boxed::Box;
1212
#[cfg(feature = "alloc")]
13-
use futures_core::future::BoxFuture;
13+
use futures_core::future::{BoxFuture, LocalBoxFuture};
1414

1515
// re-export for `select!`
1616
#[doc(hidden)]
@@ -499,6 +499,16 @@ pub trait FutureExt: Future {
499499
Box::pin(self)
500500
}
501501

502+
/// Wrap the future in a Box, pinning it.
503+
///
504+
/// Similar to `boxed`, but without the `Send` requirement.
505+
#[cfg(feature = "alloc")]
506+
fn boxed_local<'a>(self) -> LocalBoxFuture<'a, Self::Output>
507+
where Self: Sized + 'a
508+
{
509+
Box::pin(self)
510+
}
511+
502512
/// Turns a [`Future<Output = T>`](Future) into a
503513
/// [`TryFuture<Ok = T, Error = ()`>](futures_core::future::TryFuture).
504514
fn unit_error(self) -> UnitError<Self>

0 commit comments

Comments
 (0)