While vectors don’t support std::io::Read, slices do.
There is some confusion here caused by Rust being able to coerce a Vec into a slice in some situations but not others.
In this case, an explicit coercion to a slice is needed because at the stage coercions are applied, the compiler doesn’t know that Vec<u8> doesn’t implement Read.
The code in the question will work when the vector is coerced into a slice using one of the following methods:
read_4_bytes(&*vec_as_file)read_4_bytes(&vec_as_file[..])read_4_bytes(vec_as_file.as_slice()).
Note:
- When asking the question initially, I was taking
&Readinstead ofRead. This made passing a reference to a slice fail, unless I’d passed in&&*vec_as_filewhich I didn’t think to do. - Recent versions of rust you can also use
as_slice()to convert a Vec to a slice. - Thanks to @arete on
#rustfor finding the solution!