Skip to content

Using an async block or function in a trait #82

@seanmonstar

Description

@seanmonstar

Brief summary

It's very easy to write inherent async fns on a type, but if using them inside a trait method is much harder. For example, this pseudo code doesn't work, and making it work isn't intuitive:

impl Service for Handler {
    type Future = WhatTypeGoesHere;
    fn call(&self, req: Request) -> Self::Future {
        async move {
            self.count(req).await;
            Ok(self.construct_response())
        }
    }
}

Optional details

  • (Optional) Which character(s) would be the best fit and why?
    • Any of the characters, since it's hard in general. Barbara may know how to work around it, but she likely still doesn't like it.
  • (Optional) What are the key points or morals to emphasize?
    • It's confusing that you can't write async fn when implementing a trait.
    • It's hard to return a future from a trait method that came from another async fn or async block.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions