In a test, I'm trying to pass a fake std::io::Read to some code I'm testing.
However, I can't seem to figure out how to use an array or a slice as a Read. I can see there's an impl and the error message indicates that I'm doing something wrong, but I can't see what the problem is.
Here's a minimal reproduction of the problem:
use std::io::Read;
fn do_the_read<R: Read>(r: &mut R) {
// Imagine this function does something...
}
fn main() {
let mut data = [0, 1, 2];
let mut slice: &mut [u8] = data.as_mut();
do_the_read(&mut slice);
}
Error message is:
<anon>:10:3: 10:14 error: the trait bound `[u8]: std::io::Read` is not satisfied [E0277]
<anon>:10 do_the_read(&mut slice);
^~~~~~~~~~~
<anon>:10:3: 10:14 help: see the detailed explanation for E0277
<anon>:10:3: 10:14 help: the following implementations were found:
<anon>:10:3: 10:14 help: <&'a [u8] as std::io::Read>
<anon>:10:3: 10:14 note: required by `do_the_read`
error: aborting due to previous error
Here's a playground link: Rust Playground
PS: It's weird to me that I've successfully written as much Rust code as I have but still can't figure out simple stuff like this! I still don't have a great intuition for how slices work (like what it means to pass a bare [u8] to a function, or how to convert between arrays and slices) so if you have any advice there it'd help too!