Generic return type reported to be unimplemented of associated type bounds

Recently I've been working on a audio_taglib, suppose I have the following Id3v1Raw struct and TryFrom impl

pub struct Id3v1Raw { ... }
impl TryFrom<&[u8]> for Id3v1Raw {
    type Error = &'static str
    ...
}

then I have the following read_bytes, match_to_raw and read_raw function signatures

pub fn read_mp3_id3v1_bytes<F>(f: &mut F) -> Result<Vec<u8>, Box<dyn Error>>
    where F: Read + Seek

// other read_bytes functions...

#[inline]
fn match_read_bytes_result<T>(result: Result<Vec<u8>, Box<dyn Error>>) -> Result<Option<T>, io::Error>
where
    for<'a> T: TryFrom<&'a [u8]>
    for<'a> <T as TryFrom<&'a [u8]>::Error: fmt::Debug
{
    match result {
        Ok(bytes) =>
            Ok(Some(bytes[..].try_into().unwrap()))
        },
        Err(err) => match err.downcast::<io::Error>() {
            Ok(as_io_err) => Err(*as_io_err),
            Err(_) => Ok(None)
        }
    }
}

pub fn read_mp3_id3v1_raw<F>(f: &mut F) -> Result<Option<Id3v1Raw>, io::Error>
    where F: Read + Seek
{
    match_read_bytes_result(read_mp3_id3v1_bytes(f))
}

// same goes for other *Raw types

however, this is not going to compile, the compiler complains that:

210 | for<'a> <T as TryFrom<&'a [u8]>>::Error: fmt::Debug
| ---------- required by this bound in match_read_bytes_result
...
227 | match_read_bytes_result(read_mp3_id3v1_bytes(f))
| ^^^^^^^^^^^^^^^^^^^^^^^ <_ as TryFrom<&'a [u8]>>::Error cannot be formatted using {:?} because it doesn't implement Debug

This is not going to work unless I change the associated type bounds to specific type restriction, here is &'static str which I used for TryFrom<&[u8]> impl

#[inline]
fn match_read_bytes_result<T>(result: Result<Vec<u8>, Box<dyn Error>>) -> Result<Option<T>, io::Error>
where
    for<'a> T: TryFrom<&'a [u8], Error = &'static str>

I'would like to figure out what I was doing wrong, It seems that the compiler can not infer return type assosciated type bounds isn't it?

I believe this is the same as this thread, i.e. Issue 76407.

2 Likes

Thx.
This is simply a sad storry :sweat_smile:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.