Code Monkey home page Code Monkey logo

rust-partial-io's Introduction

partial-io Build Status crates.io

A Rust utility library to test resilience of Read or Write wrappers.

If you'd like to help out, see CONTRIBUTING.md.

Documentation (latest release)

Documentation (master)

Example

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

use partial_io::{PartialOp, PartialRead};

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

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

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

Quick start

Add this to your Cargo.toml:

[dev-dependencies]
partial-io = "0.2"

Next, add this to your crate:

#[cfg(test)]
extern crate partial_io;

Now you can use partial-io in your tests.

Tokio integration

partial-io can optionally integrate with the tokio-io library to provide wrappers for AsyncRead and AsyncWrite instances. Enable the tokio feature to use this:

[dev-dependencies]
partial-io = { version = "0.2", features = ["tokio"] }

QuickCheck integration

partial-io can optionally integrate with the quickcheck library to generate random test cases. Enable the quickcheck feature to use this:

[dev-dependencies]
partial-io = { version = "0.2", features = ["quickcheck"] }

See the documentation for how to use quickcheck to generate tests.

License

partial-io is BSD-licensed. We also provide an additional patent grant.

rust-partial-io's People

Contributors

facebook-github-bot avatar jsgf avatar kulshrax avatar sid0 avatar sunshowers avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.