Suppose I have a function:
fn cast<T>(n: U64) -> T // U64 is my own defined type
where
for<'a> T: TryFrom<&'a U64>,
for<'a> <T as TryFrom<&'a U64>>::Error: Debug,
{
T::try_from(&n).unwrap()
}
Here, for some reason, Rust refuses to infer that <T as TryFrom<&'a U64>>::Error implements Debug. Can be checked by adding more code:
struct U32(u32);
struct U64(u64);
#[derive(Debug)]
struct TooLargeErr;
impl TryFrom<&U64> for U32 {
type Error = TooLargeErr;
fn try_from(n: &U64) -> Result<U32, Self::Error> {
u32::try_from(n.0).map(U32).map_err(|_| TooLargeErr)
}
}
fn main() {
// This fails to compile because: `<_ as TryFrom<&'a U64>>::Error` ... doesn't implement `Debug`
// However, `TooLargeErr` does implement Debug.
let n: U32 = cast(U64(1));
assert_eq!(n.0, 1);
}
If we replace main function with:
fn main() {
let n = U32::try_from(&U64(1)).unwrap();
assert_eq!(n.0, 1);
}
then it will be compiled, so Rust is able to infer that TooLargeErr implements Debug, but it doesn't do the same in generic bounds.
Here's a link on playground
Meta
Reproduces at Rust 1.50.0 stable, beta, nightly
Suppose I have a function:
Here, for some reason, Rust refuses to infer that
<T as TryFrom<&'a U64>>::Errorimplements Debug. Can be checked by adding more code:If we replace main function with:
then it will be compiled, so Rust is able to infer that TooLargeErr implements Debug, but it doesn't do the same in generic bounds.
Here's a link on playground
Meta
Reproduces at Rust 1.50.0 stable, beta, nightly