mirror of
https://github.com/tokio-rs/tokio.git
synced 2025-10-01 12:20:39 +00:00
macros: accept IntoFuture
args for macros (#6710)
This commit is contained in:
parent
6a1a7b1591
commit
dd1d37167d
@ -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,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: F) -> MaybeDone<F::IntoFuture> {
|
||||
MaybeDone::Future {
|
||||
future: future.into_future(),
|
||||
}
|
||||
}
|
||||
|
||||
impl<Fut: Future> MaybeDone<Fut> {
|
||||
|
@ -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.
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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 => {},
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user