1

I have an API that I need to pass a Vec<u8> to that requires its parameter to implement std::io::Seek:

fn some_func<T: Seek + Write>(foo: &mut T) {/* body */}

The crate author suggests using a File here, however I want to avoid using that here as it would result in unneeded file creation. A Vec<u8> satisfies the Write trait, but not the Seek trait. Is there any way to avoid using a File here?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Josh Abraham
  • 959
  • 1
  • 8
  • 18

1 Answers1

3

You can wrap the Vec<u8> in a std::io::Cursor:

let mut buf: Cursor<Vec<u8>> = Cursor::new(Vec::new());
some_func(&mut buf);
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Josh Abraham
  • 959
  • 1
  • 8
  • 18