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.
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
Static async fn in traits #3185
Static async fn in traits #3185
Changes from 6 commits
9bc6b37
5c7db81
0a1ddd3
a610469
ea25d08
e54f7ed
6cf7d28
72b7875
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
Is there a possibility that this restriction could be later lifted in a backwards-compatible way?
This is important, because with the explicit
impl Future
you're able to run some preliminary logic before constructing the final future, which makes it possible for the future not to capture some of the input arguments, potentially making the futureSend
where it wouldn't be otherwise.In regular async functions, it's sort of an implementation detail of the function whether it's implemented as an actual
async fn
or as anfn -> impl Future
— while this fact "leaks" to rustdoc and elsewhere, it can be more or less flipped back and forth without breaking the public interface of the function. Whereas this design would "bake in", at the trait level, the fact that implementation functions must use theasync fn
syntax, even though the users of the trait don't actually care since they invoke a method and get a future from it either way.So my concern is, would anyone actually use the
async fn
syntax when defining a trait, knowing that this forces an unnecessary requirement on all implementations?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.
@bugaevc IMHO, as you have already alluded to, these function signatures are not interchangeable and I don't think they should be treated as such. At a glance
async fn
tells me that it is fully async and notawait
ing has no side effects at all.fn -> impl Future
, on the other hand, may or may not have side effects. Notawait
ing it may also block the thread or not. To cut the long story short, I think these function signatures provide different guarantees. A fact we might want to make explicit.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.
One way that this could happen would be for
impl
s to infer the associated type's values.That would be nice for things other than
async fn
too -- one could imaginejust working without needing to say
type Output = i32;
, treating the associated type kinda like it was a TAIT.(With TAIT it could almost be written something like
which is noisier without really adding anything particularly useful to the reader.)
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.
Yes, I had the same idea. Inferring associated types would be very nice outside of async traits as well. The downside is that when reading a trait impl, you might have to do a bit more searching to identify a given associated type.
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.
We can allow this later by adding return position
impl Trait
in traits and making this feature interoperable with it (i.e., redefine theasync fn
in traits desugaring in terms of return positionimpl Trait
). I think we should do so, because as you and @cramertj point out, library authors will want to know they aren't backing themselves into a corner by using this feature.