I have a argument that implements a generic TryFrom and want to handle the error. How do I lock down that the Error implements debug so that, regardless, I am able to bubble up the error handling?:
pub async fn do_some<'a, T>(
convertible: &'a T,
) -> Result<String, MyError>
where
ConvertTo: TryFrom<&'a T>,
{
let mut dat: ConvertTo = convertible.try_into().map_err(
// but err doesn't implement anything since its type is unknown...
|err| MyError::from(format!("{:?}", err))
)?;
todo()!
}
The problem is that Error is declared individually inside the TryFrom, so I couldn't do something like: TryFrom<&'aT, E: Debug>.
How can I decently convert that error into what my app needs?
The only workaround I can think of is calling TryInto() when the type is not generic, since then the Error is concretely known, but then I'm repeating myself everywhere that calls do_some
this gets me a lot closer, but unless I manually map_err trying to use ? results in:
error[E0277]: `?` couldn't convert the error to `MyError`
--> src/data.rs:29:58
|
29 | let mut dat = ConvertTo::try_from(convertible)?;
| ^ the trait `From<<data::ConvertTo as TryFrom<&'a T>>::Error>` is not implemented for `MyError`
|
= note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
= note: required for `Result<std::string::String, MyError>` to implement `FromResidual<Result<Infallible, <data::ConvertTo as TryFrom<&'a T>>::Error>>`
I'm not sure how to implement that recommendation... When I tried it said it was "unstable"
lol, magic. How should I read the compiler output to know to implement Debug instead? Sometimes if I read the output literally I go about implementing something that can't be implemented (stable). In this case, how did you know that's what it actually meant?
I knew that roughly speaking[1] the ? operator on Result works like
match res {
Ok(o) => o,
Err(e) => return Err(<_>::from(e)),
}
For the first approach, the only thing you know about <data::ConvertTo as TryFrom<&'a T>>::Error are the bounds from the trait and from your own bounds: it implements Debug (and Sized). Otherwise, since this is a generic context, it could be anything. So if you want to make use of ?, you'd have to have the From implementation for everything that implements Debug (and Sized).
The second approach is to just put the necessary bound so that ? works on the function instead.
It would be nice if the compiler said something like "MyError doesn't implement From<[the associated type]>" in this situation, or linked to ? docs etc.