Impl AsRef<T> for Cursor<T>

Hello. It would be very useful this implementation:

impl AsRef<T> for Cursor<T>
{
    fn as_ref(&self) -> &T {
        &self.inner
    }
}

Example:

fn match_bytes<C: AsRef<[u8]>(content: C) -> bool {
    ...
}

fn read_bytes<R: io::Read + io::Seek>(content: R) -> io::Result<usize> {
    ...
}

fn cursor_from_disk() -> io::Cursor<Mmap> {
 ...
}

fn cursor_from_memory() -> io::Cursor<Vec<u8>> {
 ...
}

fn main() {
    let a = cursor_from_disk();
    println!("{}, {:?}", match_bytes(&a), read_bytes(&a));
    let b = cursor_from_memory();
    println!("{}, {:?}", match_bytes(&b), read_bytes(&b));
}

Without this need create CursorWrapper(io::Cursor) and implementation traits for Wrapper. What do you think about such an implementation?

Your proposal seems inconsistent. You say AsRef<T> for Cursor<T>, but the example code shows using a Cursor<Vec<u8>> for AsRef<[u8]>.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.