Cursor for BufReader

Some of the existing library/function that I use consumes Cursor:

let input_reader = BufReader::new(file);
let mut reader = Cursor::new(input_reader);

But then when I try to call any of the function from that 3rd-party library, it complains about missing Read and Seek traits. Is there any way to quickly convert BufReader to Cursor without additional copying, etc?

what crate and what function are you trying to use? your code makes no sense to me. a Cursor is meant to wrap a in-memory byte buffer, what's the point of wrapping a BufReader? BufReader itself is Read and Seek, given the inner type R is Read and Seek (which is the case for File).

For example, binrw's BinReaderExt trait expects Cursor, and doesn't work with BufReader:

pub trait BinReaderExt: Read + Seek + Sized {

    fn read_le<'a, T>(&mut self) -> BinResult<T>
       where T: BinRead,
             T::Args<'a>: Required + Clone { ... }

the document says the otherway, it just needs Read and Seek, nothing about Cursor is mentioned.

you must misunderstand something, BufReader only implements Read if the inner reader implements Read, same for Seek. if the compiler is complaining the bounds, it's probably because your inner Reader type is not Read or Seek, Cursor is for other purposes.

You are right of course, I might have had a brain fog. BufReader works just fine with some minor modification.

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.