Box<Trait+Send> doesn't implement Trait


I have a trait named AsyncResponseTarget, and a value of type Box<AsyncResponseTarget+Send>. I wish to invoke a method from AsyncResponseTarget, but rustc tells me

error: the trait `net_traits::AsyncResponseTarget` is not implemented for the type `net_traits::AsyncResponseTarget + Send`

I am having trouble figuring out how I can resolve this. If I make an explicit impl for Box<AsyncResponseTarget+Send> and try to call through to the boxed trait object, I get the same error.



Relatedly, I tried to solve this by making AsyncResponseTarget inherit from Send so I could just have Box<AsyncResponseTarget>, but I now get:

error: the trait `net_traits::AsyncResponseTarget` is not implemented for the type `net_traits::AsyncResponseTarget` [E0277]

which is not the most… intuitive error message.



In order to get this to work the easiest way is to just to manually impl AsyncResponseTarget for Box<AsyncResponseTarget>.

Box doesn’t have any specialness about it in this sense, it’s just another struct that you would need to implement your trait for. I’m not sure if this will change in the future, but currently a manual implementation for Box<T> is needed.



It turns out my problem was that AsyncResponseTarget isn’t object-safe (it had a method that has a type parameter). It seems like this could use some UX improvement in the errors produced.

1 Like