Calling functions of associated types without a cast

I'm trying to find a way to more cleanly call the function of an associated type of my trait. I have, for example, this trait:

trait Connection {
     fn new(...) -> Self;
}

It is an associated type of another trait:

trait ConnectionPool {
     type Connection: Connection;
    
     fn new_connection(...) -> Self::Connection {
          <Self::Connection as Connection>::new(...)
     }
}

My issue is that I can't use Self::Connection::new, I have to use a cast, otherwise the compiler is not sure which trait I'm referencing (even though there is only one option)- is there a way to avoid this?
I've tried writing a macro to at least make this a little cleaner (This situation happens a few times in my code base), but I couldn't find a way to do it, since a macro has to generate a complete statement- The macro would've to also do the function call.

First off, don't worry about it. It's the idiomatic way.

Second, I don't know what you are doing, but for me, omitting the as Trait part still lets the code compile.

1 Like

Interesting. In my real code, the traits are actually decorated with async_trait, and all the relevant functions are async- and indeed, this behavior seems limited to async functions in async_trait- I just tried to change one of the functions to be non-async and it does compile. So I guess this a bug / limitation of async_trait. Thanks for the help!