I was trying to write a function that copies a user-specified number of bytes from a reader to a writer, and I came up with this:
fn io_copy(
    reader: &mut std::io::Read,
    writer: &mut std::io::Write,
    byte_count: usize,
) -> std::io::Result<()> {
    let mut buffer: [u8; 16384] = unsafe { std::mem::uninitialized() };
    let mut remaining = byte_count;
    while remaining > 0 {
        let to_read = if remaining > 16384 { 16384 } else { remaining };
        reader.read_exact(&mut buffer[0..to_read])?;
        remaining -= to_read;
        writer.write(&buffer[0..to_read])?;
    }
    Ok(())
}
It works fine, but I wanted to do it without an arbitrarily sized intermediate buffer, and I wondered if such a function already existed. I found std::io::copy, but that copies the whole stream, and I only want to copy a limited amount. I figured I could use take on the reader, but I'm having trouble getting rid of errors. This is what I have so far:
fn io_copy<R>(reader: &mut R, writer: &mut std::io::Write, byte_count: usize) -> std::io::Result<()>
where
    R: std::io::Read + Sized,
{
    let mut r = reader.by_ref().take(byte_count as u64);
    std::io::copy(&mut r, writer)?;
    Ok(())
}
This gives me an error:
error[E0507]: cannot move out of borrowed content
 --> src/lib.rs:6:21
  |
6 |         let mut r = reader.by_ref().take(byte_count as u64);
  |                     ^^^^^^^^^^^^^^^ cannot move out of borrowed content
I don't understand how to get around this.