How does one pass a Vec<T> to a function when the trait Seek is required?

566 views generics

I have an API that I need to pass a Vec to that requires it's 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 satisfies the Write trait, but not the Seek trait. Is there any way to avoid using a File here?

answered question

1 Answer


You can wrap the Vec in a std::io::Cursor like so:

let mut buf: Cursor<Vec<u8>> = Cursor::new(Vec::new());
some_func(&mut buf);

See the documentation here.

posted this

Have an answer?


Please login first before posting an answer.