Skip to content

Make BoxFuture and LocalBoxFuture a concrete type with #[must_use] #1691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 65 additions & 2 deletions futures-core/src/future/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ mod future_obj;
pub use self::future_obj::{FutureObj, LocalFutureObj, UnsafeFutureObj};

#[cfg(feature = "alloc")]
#[must_use = "futures do nothing unless polled"]
#[allow(missing_debug_implementations)]
/// An owned dynamically typed [`Future`] for use in cases where you can't
/// statically type your result or need to add some indirection.
pub type BoxFuture<'a, T> = Pin<alloc::boxed::Box<dyn Future<Output = T> + Send + 'a>>;
pub struct BoxFuture<'a, T>(Pin<alloc::boxed::Box<dyn Future<Output = T> + Send + 'a>>);

#[cfg(feature = "alloc")]
#[must_use = "futures do nothing unless polled"]
#[allow(missing_debug_implementations)]
/// `BoxFuture`, but without the `Send` requirement.
pub type LocalBoxFuture<'a, T> = Pin<alloc::boxed::Box<dyn Future<Output = T> + 'a>>;
pub struct LocalBoxFuture<'a, T>(Pin<alloc::boxed::Box<dyn Future<Output = T> + 'a>>);

/// A `Future` or `TryFuture` which tracks whether or not the underlying future
/// should no longer be polled.
Expand Down Expand Up @@ -81,6 +85,7 @@ impl<F, T, E> TryFuture for F

#[cfg(feature = "alloc")]
mod if_alloc {
use core::mem;
use alloc::boxed::Box;
use super::*;

Expand All @@ -96,4 +101,62 @@ mod if_alloc {
<F as FusedFuture>::is_terminated(&**self)
}
}

impl<O> Future for BoxFuture<'_, O> {
type Output = O;

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut_pin = self.0.as_mut();
mut_pin.poll(cx)
}
}

impl<O> BoxFuture<'_, O> {
/// Converts Pin<Box<Future<Output=O>> to PinnedFuture<O>
pub fn new<'a>(f: Pin<Box<dyn Future<Output = O> + Send + 'a>>) -> BoxFuture<'a, O> {
BoxFuture(f)
}
}

impl<O> Future for LocalBoxFuture<'_, O> {
type Output = O;

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut_pin = self.0.as_mut();
mut_pin.poll(cx)
}
}

impl<O> LocalBoxFuture<'_, O> {
/// Converts Pin<Box<Future<Output=O>> to PinnedFuture<O>
pub fn new<'a>(f: Pin<Box<dyn Future<Output = O> + 'a>>) -> LocalBoxFuture<'a, O> {
LocalBoxFuture(f)
}
}

unsafe impl<'a, T> UnsafeFutureObj<'a, T> for BoxFuture<'a, T> where T: 'a
{
fn into_raw(mut self) -> *mut (dyn Future<Output = T> + 'a) {
let ptr = unsafe { self.0.as_mut().get_unchecked_mut() as *mut _ };
mem::forget(self);
ptr
}

unsafe fn drop(ptr: *mut (dyn Future<Output = T> + 'a)) {
drop(Pin::from(Box::from_raw(ptr)))
}
}

unsafe impl<'a, T> UnsafeFutureObj<'a, T> for LocalBoxFuture<'a, T> where T: 'a
{
fn into_raw(mut self) -> *mut (dyn Future<Output = T> + 'a) {
let ptr = unsafe { self.0.as_mut().get_unchecked_mut() as *mut _ };
mem::forget(self);
ptr
}

unsafe fn drop(ptr: *mut (dyn Future<Output = T> + 'a)) {
drop(Pin::from(Box::from_raw(ptr)))
}
}
}
4 changes: 2 additions & 2 deletions futures-util/src/future/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ pub trait FutureExt: Future {
fn boxed<'a>(self) -> BoxFuture<'a, Self::Output>
where Self: Sized + Send + 'a
{
Box::pin(self)
BoxFuture::new(Box::pin(self))
}

/// Wrap the future in a Box, pinning it.
Expand All @@ -505,7 +505,7 @@ pub trait FutureExt: Future {
fn boxed_local<'a>(self) -> LocalBoxFuture<'a, Self::Output>
where Self: Sized + 'a
{
Box::pin(self)
LocalBoxFuture::new(Box::pin(self))
}

/// Turns a [`Future<Output = T>`](Future) into a
Expand Down