Skip to content

Add more helpers for Future #594

Closed as not planned
Closed as not planned
@NobodyXu

Description

@NobodyXu

Proposal

Problem statement

I propose to add more helpers to the std::future::Future trait to make it easier to use for async code.

Motivating examples or use cases

futures_util::FutureExt is widely used in async code as the stdlib does not provide the helper functions needed for many async crate.

std::iter::Iterator on the other hand, has lots of helper functions and thus most crates don't need to pull in any extra dependencies to use it, as compared to Future.

Solution sketch

I propose to add the following provided trait methods, similar to how Iterator works to enable simple usage and ability to override for better/more efficient implementation:

impl Future {
    /// Wrap the future in a Box, pinning it.
    fn boxed<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>
    where
        Self: Sized + Send + 'a;

    /// Wrap the future in a Box, pinning it.
    ///
    /// Similar to `boxed`, but without the `Send` requirement.
    fn boxed_local<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + 'a>>
    where
        Self: Sized + 'a;
}

And join for array/tuple

impl<T, const N: usize> [T; N]
where
    T: Future
{
    async fn join(self) -> [T::Output; N];
}

impl<T, O, R, const N: usize> [T; N]
where
    T: Future<Output = O>,
    O: Try<Residual = R>,
    R: Residual<[R::Output; N]>,
{
    async fn try_join<U>(self) -> <R as Residual<[R::Output; N]>>::TryType;
}

impl<T1, ...> (T1, ...)
where
    T1, ...: Future
{
    async fn join(self) -> (T1::Output, ...);
}

impl<T1, ..., O1, ..., R1, ..., const N: usize> (T1, ...)
where
    T1, ...: Future<Output = O>,
    O1, ...: Try<Residual = R>,
    R1, ...: Residual<(R1::Output, ...)>,
{
    async fn try_join<U>(self) -> <R as Residual<(R1::Output, ...)>>::TryType;
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions