Async trait methods with associated return type and dynamic dispatch
There are 2 issues here.
First, for a async trait to have default implementations, it requires that the trait itself have Send
or Sync
(depending on the receiver type) as a supertrait (there is a brief explanation in the "dyn traits" section of the readme for the crate: https://docs.rs/async-trait/latest/async_trait/)
Second the connect
function is not object safe, because its return type is an associated function. This is nothing to do with async-ness, it's an object safety concern. This simplified example has the same issue:
fn main() {
let x: Box<dyn Foo<Bar = ()>> = Box::new(());
}
trait Foo {
type Bar;
fn connect() -> Self::Bar;
}
impl Foo for () {
type Bar = ();
fn new() -> Self::Bar {
todo!()
}
}
However, you're unlikely to want to invoke connect
on a trait object, since it has no self
parameter. Instead, you can opt that specific function out of the trait object by adding the Self: Sized
constraint to connect
.
You can then create a trait object, but connect
won't be available.