I have a
foo: Vec<u8>. I need to create something which implements trait
Read. How do I do this?
I have a
I was running into the same issue as https://stackoverflow.com/questions/42240663/how-to-read-stdioread-from-a-vec-or-slice
It appears the answer is
&foo[..], to convert the Vec to a slice, which, as the llink above you provided shows, implements Read.
Yes but if it’s not meant to be read as text then it won’t work, so keep that in mind.
Read is implemented for
&[u8]by copying from the slice.
Note that reading updates the slice to point to the yet unread part. The slice will be empty when EOF is reached.
Although I see no mention of it in the impl
This confuses me. Does this modify the originating Vec?
No, as far as I can see it does this:
- You create a reference (from now it’ll be easier to think of it as a pointer) to the start of the
- You read
u8from the pointer
- This increments the pointer by
u8(And adjusts the len because a
&[T]is a fat pointer)
It doesn’t change the
This interpretation makes sense. Thanks!