I have a similar situation as Unable to tokio::run a boxed Future because the trait bound Send is not satisfied): I want to use tokio::run on types of Box<T: Future ...> where T does not implement Send.
My situation is different in that I didn't write the method which returns my Box, and so can't change its method signature. It performs a web request and returns this boxed future:
pub fn get_resource(&self,) -> Box<Future<Item = RestStruct, Error = Error<serde_json::Value>>>
Note the lack of Send.
Per the Tokio documentation, I need to implement Send on my boxed type, which isn't automatic even if the future's associated types are Send:
Astute readers may notice the explicit
Sendtrait notation within theBoxdefinition. The notation is added becauseFutureis not explicitlySendby default; this causes problems later when trying to pass this future or one of its derivatives intotokio::run.
I'm having trouble figuring out an appropriately concise way to do this. It seems like something along these lines should work:
impl<T, I, E> Send for T
where
T: Future<Item = I, Error = E>,
I: Send,
E: Send,
{
}
I get a number of surprising errors. I'll detail them below, but my question can be generally phrased, how do I specify: any type satisfying a given first trait under certain trait bounds must also implicitly satisfy a second trait?
The errors my solution produced:
the trait
std::marker::Sendrequires anunsafe impldeclaration
I think I understand that one based on the documentation about Send, though to clarify: this would indeed constitute a "properly implemented Send" as described there, because the associated types are Send?
type parameter
Tmust be used as the type parameter for some local type (e.g.,MyStruct<T>)
I understand this to be related to the newtype workaround as described in the docs, which would really get in the way of doing what I want to do if so. Does this mean I can't accomplish what I want? Is there something like a type alias for bounded traits that would get around this?
conflicting implementations of trait
std::marker::Sendfor type&_
That one, I don't understand at all. &_ is far broader a type than the constrained one I tried to specify using all those where bounds; I would not expect &_ to be Future<Item = I: Send, Error = E: Send>.