How to bound TryFrom Error for a generic argument?

⚓ rust    📅 2025-04-25    👤 surdeus    👁️ 1      

surdeus

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

3 posts - 2 participants

Read full topic

🏷️ rust_feed