Skip to content
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

tokio::time::timeout should support std::future::IntoFuture #6665

Open
alexheretic opened this issue Jun 28, 2024 · 0 comments · May be fixed by #6666
Open

tokio::time::timeout should support std::future::IntoFuture #6665

alexheretic opened this issue Jun 28, 2024 · 0 comments · May be fixed by #6666
Labels
A-tokio Area: The main tokio crate C-feature-request Category: A feature request. M-time Module: tokio/time

Comments

@alexheretic
Copy link

Tokio APIs don't currently support std::future::IntoFuture. In particular tokio::time::timeout & timeout_at.

Example: timeout

struct IntoFooture;

impl std::future::IntoFuture for IntoFooture {
    type Output = ();
    type IntoFuture = std::future::Ready<()>;

    fn into_future(self) -> Self::IntoFuture {
        std::future::ready(())
    }
}

fn foo() -> IntoFooture {
    IntoFooture
}

foo().await; // works

_ = tokio::time::timeout(Duration::from_secs(4), foo()).await; // !compile: `IntoFooture` is not a future

So currently the future must be explicitly converted here to use timeout.

_ = tokio::time::timeout(Duration::from_secs(4), foo().into_future()).await; // works

Solution: Support IntoFuture

If we updated the timeout method to:

pub fn timeout<F>(duration: Duration, future: F) -> Timeout<F::IntoFuture>
where
    F: std::future::IntoFuture,
{...}

The original example would work without the .into_future() explicit call.

MSRV

Note: This requires bumping the msrv to 1.64 as this version stabilised IntoFuture.

@alexheretic alexheretic added A-tokio Area: The main tokio crate C-feature-request Category: A feature request. labels Jun 28, 2024
@alexheretic alexheretic linked a pull request Jun 28, 2024 that will close this issue
@Darksonn Darksonn added the M-time Module: tokio/time label Jun 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-feature-request Category: A feature request. M-time Module: tokio/time
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants