From d1d83388da72309e6b134f3f11791db59388e7cb Mon Sep 17 00:00:00 2001 From: Alice Ryhl <aliceryhl@google.com> Date: Mon, 22 Jul 2024 23:37:19 +0200 Subject: [PATCH 1/3] macros: accept `IntoFuture` args for macros --- tokio/src/future/maybe_done.rs | 6 ++++-- tokio/src/macros/select.rs | 2 +- tokio/src/macros/support.rs | 2 +- tokio/tests/macros_join.rs | 15 +++++++++++++++ tokio/tests/macros_select.rs | 17 +++++++++++++++++ 5 files changed, 38 insertions(+), 4 deletions(-) diff --git a/tokio/src/future/maybe_done.rs b/tokio/src/future/maybe_done.rs index 506b4f26c18..c371403a23d 100644 --- a/tokio/src/future/maybe_done.rs +++ b/tokio/src/future/maybe_done.rs @@ -22,8 +22,10 @@ pin_project! { } /// Wraps a future into a `MaybeDone`. -pub fn maybe_done<Fut: Future>(future: Fut) -> MaybeDone<Fut> { - MaybeDone::Future { future } +pub fn maybe_done<F: IntoFuture>(future: Fut) -> MaybeDone<F::IntoFuture> { + MaybeDone::Future { + future: future.into_future(), + } } impl<Fut: Future> MaybeDone<Fut> { diff --git a/tokio/src/macros/select.rs b/tokio/src/macros/select.rs index 124d7827086..505c70dd060 100644 --- a/tokio/src/macros/select.rs +++ b/tokio/src/macros/select.rs @@ -495,7 +495,7 @@ doc! {macro_rules! select { // We can't use the `pin!` macro for this because `futures` is a // tuple and the standard library provides no way to pin-project to // the fields of a tuple. - let mut futures = ( $( $fut , )+ ); + let mut futures = ( $( $crate::macros::support::IntoFuture::into_future($fut) , )+ ); // This assignment makes sure that the `poll_fn` closure only has a // reference to the futures, instead of taking ownership of them. diff --git a/tokio/src/macros/support.rs b/tokio/src/macros/support.rs index 10526bcbca7..d077a0823c7 100644 --- a/tokio/src/macros/support.rs +++ b/tokio/src/macros/support.rs @@ -8,6 +8,6 @@ cfg_macros! { } } -pub use std::future::Future; +pub use std::future::{Future, IntoFuture}; pub use std::pin::Pin; pub use std::task::Poll; diff --git a/tokio/tests/macros_join.rs b/tokio/tests/macros_join.rs index 37dd05f0e13..24647a2b7de 100644 --- a/tokio/tests/macros_join.rs +++ b/tokio/tests/macros_join.rs @@ -159,3 +159,18 @@ async fn a_different_future_is_polled_first_every_time_poll_fn_is_polled() { async fn empty_join() { assert_eq!(tokio::join!(), ()); } + +#[tokio::test] +async fn join_into_future() { + struct NotAFuture; + impl std::future::IntoFuture for NotAFuture { + type Output = (); + type IntoFuture = std::future::Ready<()>; + + fn into_future(self) -> Self::IntoFuture { + std::future::Ready(()) + } + } + + tokio::join!(NotAFuture); +} diff --git a/tokio/tests/macros_select.rs b/tokio/tests/macros_select.rs index cbad971ab1f..d61e904bb64 100644 --- a/tokio/tests/macros_select.rs +++ b/tokio/tests/macros_select.rs @@ -692,3 +692,20 @@ mod unstable { ) } } + +#[tokio::test] +async fn select_into_future() { + struct NotAFuture; + impl std::future::IntoFuture for NotAFuture { + type Output = (); + type IntoFuture = std::future::Ready<()>; + + fn into_future(self) -> Self::IntoFuture { + std::future::Ready(()) + } + } + + tokio::select! { + () = NotAFuture => {}, + } +} From 4220a10e2bb0b28e81f55dc9fa6abc8627358213 Mon Sep 17 00:00:00 2001 From: Alice Ryhl <aliceryhl@google.com> Date: Tue, 23 Jul 2024 14:31:51 +0200 Subject: [PATCH 2/3] import IntoFuture --- tokio/src/future/maybe_done.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tokio/src/future/maybe_done.rs b/tokio/src/future/maybe_done.rs index c371403a23d..8b270b3a01f 100644 --- a/tokio/src/future/maybe_done.rs +++ b/tokio/src/future/maybe_done.rs @@ -1,7 +1,7 @@ //! Definition of the [`MaybeDone`] combinator. use pin_project_lite::pin_project; -use std::future::Future; +use std::future::{Future, IntoFuture}; use std::pin::Pin; use std::task::{Context, Poll}; @@ -22,7 +22,7 @@ pin_project! { } /// Wraps a future into a `MaybeDone`. -pub fn maybe_done<F: IntoFuture>(future: Fut) -> MaybeDone<F::IntoFuture> { +pub fn maybe_done<F: IntoFuture>(future: F) -> MaybeDone<F::IntoFuture> { MaybeDone::Future { future: future.into_future(), } From 5f957aeb7e0e853209a9d3942b572d3811b290d7 Mon Sep 17 00:00:00 2001 From: Alice Ryhl <aliceryhl@google.com> Date: Tue, 23 Jul 2024 14:36:45 +0200 Subject: [PATCH 3/3] fix ready future creation --- tokio/tests/macros_join.rs | 2 +- tokio/tests/macros_select.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tokio/tests/macros_join.rs b/tokio/tests/macros_join.rs index 24647a2b7de..4deaf120a95 100644 --- a/tokio/tests/macros_join.rs +++ b/tokio/tests/macros_join.rs @@ -168,7 +168,7 @@ async fn join_into_future() { type IntoFuture = std::future::Ready<()>; fn into_future(self) -> Self::IntoFuture { - std::future::Ready(()) + std::future::ready(()) } } diff --git a/tokio/tests/macros_select.rs b/tokio/tests/macros_select.rs index d61e904bb64..c91d696426c 100644 --- a/tokio/tests/macros_select.rs +++ b/tokio/tests/macros_select.rs @@ -701,7 +701,7 @@ async fn select_into_future() { type IntoFuture = std::future::Ready<()>; fn into_future(self) -> Self::IntoFuture { - std::future::Ready(()) + std::future::ready(()) } }