I was writting a function to flat a Result. This is what I got.
type BoxDyn = Box<dyn Error + Send + Sync>;
fn flat_result<U, T, G>(r: Result<Result<U, G>, T>) -> Result<U, BoxDyn>
where
T: Into<BoxDyn>,
G: Into<BoxDyn>,
{
match r {
Err(x) => Err(x.into()),
Ok(x) => match x {
Err(x) => Err(x.into()),
Ok(x) => Ok(x),
},
}
}
It works fine. But I should be able to write it using two ? like this.
type BoxDyn = Box<dyn Error + Send + Sync>;
fn flat_result<U, T, G>(r: Result<Result<U, G>, T>) -> Result<U, BoxDyn>
where
T: Into<BoxDyn>,
G: Into<BoxDyn>,
{
Ok(r??)
}
When I try this, I get this error:
`G` cannot be shared between threads safely
required because of the requirements on the impl of `From<G>` for `Box<dyn std::error::Error + Send + Sync>`
required because of the requirements on the impl of `FromResidual<Result<Infallible, G>>` for `Result<U, Box<dyn std::error::Error + Send + Sync>>`
But it seems to me that it should not happen because the documentation says that the ? is equivalent to a match expression, where the Err(err) branch expands to an early return Err(From::from(err)). And also From<T> for U implies Into<U> for T. So it would not matter What G or T are, if they implement Into it should work. What is the problem?