Function netio::copy [] [src]

pub fn copy<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W) -> Result<()> where
    R: BufRead,
    W: Write

Copies the entire content of a buffered reader into a writer.

Similar to std::io::copy, this function will continuously read data from reader and then write it into writer in a streaming fashion until reader returns EOF.

Errors

This function will return an error immediately if any call to fill_buf or write returns any kind of error. Instances of ErrorKind::Interrupted are not handled by this function.

All bytes consumed from the buffered reader will be written to the specified writer and vice versa. It is guaranteed that no data is lost in case of error.

Differences to std::io::copy

  • Does not retry on ErrorKind::Interrupted.
  • Uses BufRead instead of Read.
  • Does not return the number of bytes that are copied.

Advantages

  • Allows for reliable retry on errors.
  • Function is interruptable, e.g. to allow graceful shutdown for server applications.
  • Avoids double buffering if the source already implements BufRead.
  • Allows different buffer sizes by using BufReader::with_capacity.

Disadvantages

The fact that it does not return the number of bytes copied stems from the fact that it cannot return this information in case of error. This would go against the goal of allowing reliable retry.