-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Infer async closure args from Fn
bound even if there is no corresponding Future
bound on return
#129072
Merged
bors
merged 1 commit into
rust-lang:master
from
compiler-errors:more-powerful-async-closure-inference
Aug 15, 2024
Merged
Infer async closure args from Fn
bound even if there is no corresponding Future
bound on return
#129072
Changes from all commits
Commits
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 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 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,49 @@ | ||
//@ check-pass | ||
//@ edition: 2021 | ||
|
||
// Make sure that we infer the args of an async closure even if it's passed to | ||
// a function that requires the async closure implement `Fn*` but does *not* have | ||
// a `Future` bound on the return type. | ||
|
||
#![feature(async_closure)] | ||
|
||
use std::future::Future; | ||
|
||
trait TryStream { | ||
type Ok; | ||
type Err; | ||
} | ||
|
||
trait TryFuture { | ||
type Ok; | ||
type Err; | ||
} | ||
|
||
impl<F, T, E> TryFuture for F where F: Future<Output = Result<T, E>> { | ||
type Ok = T; | ||
type Err = E; | ||
} | ||
|
||
trait TryStreamExt: TryStream { | ||
fn try_for_each<F, Fut>(&self, f: F) | ||
where | ||
F: FnMut(Self::Ok) -> Fut, | ||
Fut: TryFuture<Ok = (), Err = Self::Err>; | ||
} | ||
|
||
impl<S> TryStreamExt for S where S: TryStream { | ||
fn try_for_each<F, Fut>(&self, f: F) | ||
where | ||
F: FnMut(Self::Ok) -> Fut, | ||
Fut: TryFuture<Ok = (), Err = Self::Err>, | ||
{ } | ||
} | ||
|
||
fn test(stream: impl TryStream<Ok = &'static str, Err = ()>) { | ||
stream.try_for_each(async |s| { | ||
s.trim(); // Make sure we know the type of `s` at this point. | ||
Ok(()) | ||
}); | ||
} | ||
|
||
fn main() {} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why directly use the
input_tys
but create a new infer var for the return type? You could also reuse that one, can't uThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return type vid that is in scope is the type of the future, but we want the type of the future's output. We don't have it, so we need to make a new type var here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment please :3