From 0dd40786b555c04afa52b9d0c789a29dbd4e3dd2 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 31 Jan 2024 18:22:16 +0000 Subject: [PATCH] Harmonize blanket implementations for AsyncFn* traits --- library/alloc/src/boxed.rs | 29 ++++++++++++++ library/alloc/src/lib.rs | 1 + library/core/src/ops/async_function.rs | 53 ++++++++++++++++++-------- 3 files changed, 68 insertions(+), 15 deletions(-) diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index 953041b8c20..92600b8e5bd 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -159,6 +159,7 @@ use core::iter::FusedIterator; use core::marker::Tuple; use core::marker::Unsize; use core::mem::{self, SizedTypeProperties}; +use core::ops::{AsyncFn, AsyncFnMut, AsyncFnOnce}; use core::ops::{ CoerceUnsized, Coroutine, CoroutineState, Deref, DerefMut, DispatchFromDyn, Receiver, }; @@ -2030,6 +2031,34 @@ impl + ?Sized, A: Allocator> Fn for Box { } } +#[unstable(feature = "async_fn_traits", issue = "none")] +impl + ?Sized, A: Allocator> AsyncFnOnce for Box { + type Output = F::Output; + type CallOnceFuture = F::CallOnceFuture; + + extern "rust-call" fn async_call_once(self, args: Args) -> Self::CallOnceFuture { + F::async_call_once(*self, args) + } +} + +#[unstable(feature = "async_fn_traits", issue = "none")] +impl + ?Sized, A: Allocator> AsyncFnMut for Box { + type CallMutFuture<'a> = F::CallMutFuture<'a> where Self: 'a; + + extern "rust-call" fn async_call_mut(&mut self, args: Args) -> Self::CallMutFuture<'_> { + F::async_call_mut(self, args) + } +} + +#[unstable(feature = "async_fn_traits", issue = "none")] +impl + ?Sized, A: Allocator> AsyncFn for Box { + type CallFuture<'a> = F::CallFuture<'a> where Self: 'a; + + extern "rust-call" fn async_call(&self, args: Args) -> Self::CallFuture<'_> { + F::async_call(self, args) + } +} + #[unstable(feature = "coerce_unsized", issue = "18598")] impl, U: ?Sized, A: Allocator> CoerceUnsized> for Box {} diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index 96d43e11dc6..3341b564d1f 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -106,6 +106,7 @@ #![feature(array_windows)] #![feature(ascii_char)] #![feature(assert_matches)] +#![feature(async_fn_traits)] #![feature(async_iterator)] #![feature(coerce_unsized)] #![feature(const_align_of_val)] diff --git a/library/core/src/ops/async_function.rs b/library/core/src/ops/async_function.rs index efbe9d164c3..19b1220f05e 100644 --- a/library/core/src/ops/async_function.rs +++ b/library/core/src/ops/async_function.rs @@ -65,44 +65,67 @@ pub trait AsyncFnOnce { mod impls { use super::{AsyncFn, AsyncFnMut, AsyncFnOnce}; - use crate::future::Future; use crate::marker::Tuple; #[unstable(feature = "async_fn_traits", issue = "none")] - impl, A: Tuple> AsyncFn for F + impl AsyncFn for &F where - >::Output: Future, + F: AsyncFn, { - type CallFuture<'a> = >::Output where Self: 'a; + type CallFuture<'a> = F::CallFuture<'a> where Self: 'a; extern "rust-call" fn async_call(&self, args: A) -> Self::CallFuture<'_> { - self.call(args) + F::async_call(*self, args) } } #[unstable(feature = "async_fn_traits", issue = "none")] - impl, A: Tuple> AsyncFnMut for F + impl AsyncFnMut for &F where - >::Output: Future, + F: AsyncFn, { - type CallMutFuture<'a> = >::Output where Self: 'a; + type CallMutFuture<'a> = F::CallFuture<'a> where Self: 'a; extern "rust-call" fn async_call_mut(&mut self, args: A) -> Self::CallMutFuture<'_> { - self.call_mut(args) + F::async_call(*self, args) } } #[unstable(feature = "async_fn_traits", issue = "none")] - impl, A: Tuple> AsyncFnOnce for F + impl<'a, A: Tuple, F: ?Sized> AsyncFnOnce for &'a F where - >::Output: Future, + F: AsyncFn, { - type CallOnceFuture = >::Output; - - type Output = <>::Output as Future>::Output; + type Output = F::Output; + type CallOnceFuture = F::CallFuture<'a>; extern "rust-call" fn async_call_once(self, args: A) -> Self::CallOnceFuture { - self.call_once(args) + F::async_call(self, args) + } + } + + #[unstable(feature = "async_fn_traits", issue = "none")] + impl AsyncFnMut for &mut F + where + F: AsyncFnMut, + { + type CallMutFuture<'a> = F::CallMutFuture<'a> where Self: 'a; + + extern "rust-call" fn async_call_mut(&mut self, args: A) -> Self::CallMutFuture<'_> { + F::async_call_mut(*self, args) + } + } + + #[unstable(feature = "async_fn_traits", issue = "none")] + impl<'a, A: Tuple, F: ?Sized> AsyncFnOnce for &'a mut F + where + F: AsyncFnMut, + { + type Output = F::Output; + type CallOnceFuture = F::CallMutFuture<'a>; + + extern "rust-call" fn async_call_once(self, args: A) -> Self::CallOnceFuture { + F::async_call_mut(self, args) } } }