1

Static async fn in traits by tmandry · Pull Request #3185 · rust-lang/rfcs · Git...

 2 years ago
source link: https://github.com/rust-lang/rfcs/pull/3185
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Conversation

Copy link

Contributor

tmandry commented 2 days ago

edited

This is an RFC to support async fn in traits that can be called via static dispatch. These will desugar to an anonymous associated type.

Rendered

Copy link

Contributor

ibraheemdev commented 18 hours ago

I don't see anything mentioned about Sendness. Is requiring the future to be send a potential future extension?

Copy link

Member

kennytm commented 17 hours ago

i suppose that requires ability to name the type of the returned future, stated in the last section of the "Future possibilities".

trait Service2 {
    async fn request(&self, key: i32) -> Response
    where
        Self::request::Output: Send;
}

the RFC does need to state whether the default is Send or ?Send though. "letting the compiler automatically" is not an answer because the generic code don't know the impl.

async fn foo<S: Service>(s: &S) {
    let future = s.request(1);
    // can we assume `typeof(future): Send` or not?? 
    future.await;
}

Copy link

Contributor

Author

tmandry commented 9 hours ago

i suppose that requires ability to name the type of the returned future, stated in the last section of the "Future possibilities".

That's right. There's a separate RFC being worked on to add this functionality and make it possible to write those bounds.

the RFC does need to state whether the default is Send or ?Send though. "letting the compiler automatically" is not an answer because the generic code don't know the impl.

There is no Send default. In cases like the example you gave this is rarely a problem in practice. The reason is that we leak auto traits from async fn (and any function with return position impl Trait). If you call foo with a type S where S::request::Output: Send, the future returned by foo will be Send and callers are allowed to rely on this.

The story with dynamic dispatch is more complicated; there you need explicit Send bounds. But we aren't tackling that in this RFC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels
Projects

None yet

Milestone

No milestone

Linked issues

Successfully merging this pull request may close these issues.

None yet

4 participants

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK