[Solved] How do I copy a Vec<u8> to a Writer

#1

I have a struct containing a field raw: Vec<u8>.
Later I create a child process and want to copy raw to the child process’s stdin. So I’ve tried this:

let mut stdin = child.stdin.as_mut()?;
io::copy(&mut data.raw.as_mut_slice(), &mut stdin)?; // io::copy(reader, writer)

Note that the data struct is passed as data: &Data (i.e., not mutable).
The error I get is:

error[E0277]: the trait bound `[u8]: std::io::Read` is not satisfied
   --> src/process.rs:160:9
    |
160 |         io::copy(&mut data.raw.as_mut_slice(), &mut stdin)
    |         ^^^^^^^^ the trait `std::io::Read` is not implemented for `[u8]`

I don’t mind copying the data if I have to since it won’t normally be a lot, but I can’t figure out how to copy from a Vec to a [u8].

0 Likes

#2

My mistake: I just needed to do:

let stdin = child.stdin.as_mut()?;
stdin.write_all(&data.raw)?;

Sorry!

0 Likes

closed #3

This topic was automatically closed after 34 hours. New replies are no longer allowed.

0 Likes