I'm using Rocket framework and I want to make an async HTTP request in my handler, something like this
#[get("/")]
async fn handler() -> String {
some_func().await;
"OK".into()
}
And as a result, I get the next error
the trait `rocket::response::Responder<'_>` is not implemented for `impl core::future::future::Future`
I tried to write implementation but failed. Is there a way to implement trait for impl Trait?
Or maybe specify the return type of async fn so I can return my custom type with necessary traits implemented?
With the release of Rocket 0.5 on 17 November 2023, async handlers are fully supported in Rocket. See the relevant section in the announcement or view the Futures and Async section in the overview.
Until Rocket 0.5.0 is released you will have to use the development version for async routes. Notably this also means you can use stable Rust to compile.
In your Cargo.toml
rocket = { git = "https://github.com/SergioBenitez/Rocket" }
rocket_contrib = { git = "https://github.com/SergioBenitez/Rocket" }
Once using the development version you can write async routes exactly as you've done so in the question.
Note that various APIs may be different. See https://api.rocket.rs/master/rocket/index.html for documentation for the development version.