-
-
Notifications
You must be signed in to change notification settings - Fork 85
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
Problem with implementing trait for async fn
s
#47
Comments
This is a compiler limitation with how use async_trait::async_trait;
use std::future::Future;
trait AnyFn {
type Output;
fn call(&self) -> Self::Output;
}
impl<F, T> AnyFn for F
where
F: Fn() -> T,
{
type Output = T;
fn call(&self) -> Self::Output {
self()
}
}
#[async_trait]
trait Trait {
async fn run(&self);
}
#[async_trait]
impl<F> Trait for F
where
F: AnyFn + Sync,
F::Output: Future<Output = ()> + Send,
{
async fn run(&self) {
self.call().await
}
} |
Thanks! It's also worth noticing that this can be done throw #![feature(unboxed_closures)]
#[async_trait]
trait Trait {
async fn run(&self);
}
#[async_trait]
impl<F> Trait for F
where
F: Fn<()> + Sync,
F::Output: Future<Output = ()> + Send,
{
async fn run(&self) {
self().await
}
} |
I've just found out that in my code this doesn't work :( Because
So my code produce the following error:
However, unboxed See also rust-lang/rust#48869 and rust-lang/rust#50238 |
That's so crazy. :( |
I'm having trouble making this solution work when using a borrowed argument as with:
It seems no matter what I try I run into lifetime issues associated with to_whom that I can't satisfy. This appears to be due to the fact that I'm needing to specify a constraint that This rustc issue I filed may also be relevant: rust-lang/rust#95182 as this is what's required to make it work without traits despite bizarre rustc error messages. |
Closing as this is not going to be actionable in async-trait. |
@jasta, it's more than a year later but I was also having trouble with this. It seems like it's possible if you make the |
I'm trying to implement trait with
async fn
for allasync fn
s. Simplified example:simplified `cargo expand`
But sadly, this doesn't work:
(adding
Fut: 'async_trait
is impossible because it leads toimpl has stricter requirements than trait
errors)But with by-hand desugaring this implementation is possible:
So, my questions are:
async move {}
from the last example? (Box::pin(self())
leads to the same errorthe parameter type
Futmay not live long enough
)async_trait
to accept implementations like in the first example? (after some changes in how macro works)The text was updated successfully, but these errors were encountered: