-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add AsyncFn
family of traits
#119305
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
Merged
Merged
Add AsyncFn
family of traits
#119305
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
use crate::future::Future; | ||
use crate::marker::Tuple; | ||
|
||
/// An async-aware version of the [`Fn`](crate::ops::Fn) trait. | ||
/// | ||
/// All `async fn` and functions returning futures implement this trait. | ||
#[unstable(feature = "async_fn_traits", issue = "none")] | ||
#[rustc_paren_sugar] | ||
#[fundamental] | ||
#[must_use = "async closures are lazy and do nothing unless called"] | ||
#[cfg_attr(not(bootstrap), lang = "async_fn")] | ||
pub trait AsyncFn<Args: Tuple>: AsyncFnMut<Args> { | ||
/// Future returned by [`AsyncFn::async_call`]. | ||
#[unstable(feature = "async_fn_traits", issue = "none")] | ||
type CallFuture<'a>: Future<Output = Self::Output> | ||
where | ||
Self: 'a; | ||
|
||
/// Call the [`AsyncFn`], returning a future which may borrow from the called closure. | ||
#[unstable(feature = "async_fn_traits", issue = "none")] | ||
extern "rust-call" fn async_call(&self, args: Args) -> Self::CallFuture<'_>; | ||
} | ||
|
||
/// An async-aware version of the [`FnMut`](crate::ops::FnMut) trait. | ||
/// | ||
/// All `async fn` and functions returning futures implement this trait. | ||
#[unstable(feature = "async_fn_traits", issue = "none")] | ||
#[rustc_paren_sugar] | ||
#[fundamental] | ||
#[must_use = "async closures are lazy and do nothing unless called"] | ||
#[cfg_attr(not(bootstrap), lang = "async_fn_mut")] | ||
pub trait AsyncFnMut<Args: Tuple>: AsyncFnOnce<Args> { | ||
/// Future returned by [`AsyncFnMut::async_call_mut`]. | ||
#[unstable(feature = "async_fn_traits", issue = "none")] | ||
type CallMutFuture<'a>: Future<Output = Self::Output> | ||
where | ||
Self: 'a; | ||
|
||
/// Call the [`AsyncFnMut`], returning a future which may borrow from the called closure. | ||
#[unstable(feature = "async_fn_traits", issue = "none")] | ||
extern "rust-call" fn async_call_mut(&mut self, args: Args) -> Self::CallMutFuture<'_>; | ||
} | ||
|
||
/// An async-aware version of the [`FnOnce`](crate::ops::FnOnce) trait. | ||
/// | ||
/// All `async fn` and functions returning futures implement this trait. | ||
#[unstable(feature = "async_fn_traits", issue = "none")] | ||
#[rustc_paren_sugar] | ||
#[fundamental] | ||
#[must_use = "async closures are lazy and do nothing unless called"] | ||
#[cfg_attr(not(bootstrap), lang = "async_fn_once")] | ||
pub trait AsyncFnOnce<Args: Tuple> { | ||
/// Future returned by [`AsyncFnOnce::async_call_once`]. | ||
#[unstable(feature = "async_fn_traits", issue = "none")] | ||
type CallOnceFuture: Future<Output = Self::Output>; | ||
|
||
/// Output type of the called closure's future. | ||
#[unstable(feature = "async_fn_traits", issue = "none")] | ||
type Output; | ||
|
||
/// Call the [`AsyncFnOnce`], returning a future which may move out of the called closure. | ||
#[unstable(feature = "async_fn_traits", issue = "none")] | ||
extern "rust-call" fn async_call_once(self, args: Args) -> Self::CallOnceFuture; | ||
} | ||
|
||
mod impls { | ||
use super::{AsyncFn, AsyncFnMut, AsyncFnOnce}; | ||
use crate::future::Future; | ||
use crate::marker::Tuple; | ||
|
||
#[unstable(feature = "async_fn_traits", issue = "none")] | ||
impl<F: Fn<A>, A: Tuple> AsyncFn<A> for F | ||
where | ||
<F as FnOnce<A>>::Output: Future, | ||
{ | ||
type CallFuture<'a> = <F as FnOnce<A>>::Output where Self: 'a; | ||
|
||
extern "rust-call" fn async_call(&self, args: A) -> Self::CallFuture<'_> { | ||
self.call(args) | ||
} | ||
} | ||
|
||
#[unstable(feature = "async_fn_traits", issue = "none")] | ||
impl<F: FnMut<A>, A: Tuple> AsyncFnMut<A> for F | ||
where | ||
<F as FnOnce<A>>::Output: Future, | ||
{ | ||
type CallMutFuture<'a> = <F as FnOnce<A>>::Output where Self: 'a; | ||
|
||
extern "rust-call" fn async_call_mut(&mut self, args: A) -> Self::CallMutFuture<'_> { | ||
self.call_mut(args) | ||
} | ||
} | ||
|
||
#[unstable(feature = "async_fn_traits", issue = "none")] | ||
impl<F: FnOnce<A>, A: Tuple> AsyncFnOnce<A> for F | ||
where | ||
<F as FnOnce<A>>::Output: Future, | ||
{ | ||
type CallOnceFuture = <F as FnOnce<A>>::Output; | ||
|
||
type Output = <<F as FnOnce<A>>::Output as Future>::Output; | ||
|
||
extern "rust-call" fn async_call_once(self, args: A) -> Self::CallOnceFuture { | ||
self.call_once(args) | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// edition: 2021 | ||
// check-pass | ||
|
||
#![feature(async_fn_traits)] | ||
|
||
use std::ops::AsyncFn; | ||
|
||
async fn foo() {} | ||
|
||
async fn call_asyncly(f: impl AsyncFn(i32) -> i32) -> i32 { | ||
f(1).await | ||
} | ||
|
||
fn main() { | ||
let fut = call_asyncly(|x| async move { x + 1 }); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.