partial-io

partial-io on crates.io Documentation (latest release) Documentation (master) License

Helpers for testing I/O behavior with partial, interrupted and blocking reads and writes.

This library provides:

Motivation

A Read or Write wrapper is conceptually simple but can be difficult to get right, especially if the wrapper has an internal buffer. Common issues include:

With the AsyncRead and AsyncWrite provided by futures03 and tokio02:

These situations can be hard to think about and hard to test.

partial-io can help in two ways:

  1. For a known bug involving any of these situations, partial-io can help you write a test.
  2. With the quickcheck09 feature enabled, partial-io can also help shake out bugs in your wrapper. See quickcheck_types for more.

Examples

```rust use std::io::{self, Cursor, Read};

use partial_io::{PartialOp, PartialRead};

let data = b"Hello, world!".tovec(); let cursor = Cursor::new(data); // Cursor> implements io::Read let ops = vec![PartialOp::Limited(7), PartialOp::Err(io::ErrorKind::Interrupted)]; let mut partialread = PartialRead::new(cursor, ops);

let mut out = vec![0; 256];

// The first read will read 7 bytes. asserteq!(partialread.read(&mut out).unwrap(), 7); asserteq!(&out[..7], b"Hello, "); // The second read will fail with ErrorKind::Interrupted. asserteq!(partialread.read(&mut out[7..]).unwraperr().kind(), io::ErrorKind::Interrupted); // The iterator has run out of operations, so it no longer truncates reads. asserteq!(partialread.read(&mut out[7..]).unwrap(), 6); assert_eq!(&out[..13], b"Hello, world!"); ```

For a real-world example, see the [tests in zstd-rs].

Contributing

See the CONTRIBUTING file for how to help out.

License

This project is available under the MIT license.