#buffer #reader #byte #tap #read #copies #own

tap-reader

Wraps an existing reader and copies the read bytes into it's own buffer

2 stable releases

Uses old Rust 2015

1.0.1 Sep 9, 2018

#2467 in Encoding

Download history 93/week @ 2024-07-22 70/week @ 2024-07-29 82/week @ 2024-08-05 106/week @ 2024-08-12 70/week @ 2024-08-19 74/week @ 2024-08-26 51/week @ 2024-09-02 57/week @ 2024-09-09 48/week @ 2024-09-16 68/week @ 2024-09-23 80/week @ 2024-09-30 36/week @ 2024-10-14 40/week @ 2024-10-21 50/week @ 2024-10-28 46/week @ 2024-11-04

172 downloads per month
Used in 3 crates (2 directly)

GPL-3.0 license

15KB
79 lines

tap-reader

Wraps an existing reader and copies the read bytes into it's own buffer

Installation

Add the following line to your Cargo.toml:

[dependencies]
tap-reader = "1"

Usage

To use, add the following lines to your crate root (src/lib.rs, src/main.rs, etc.)

extern crate tap_reader;

use tap_reader::Tap;

Example

extern crate tap_reader;
extern crate serde_json;
extern crate serde;
#[macro_use] extern crate serde_derive;

use std::error::Error;
use std::io::Cursor;
use tap_reader::Tap;

#[derive(Debug, Deserialize, PartialEq)]
struct Foo {
    foo: String,
    bar: usize,
}

fn main() -> Result<(), Box<Error>> {
    let input = r#"{"foo":"hello, world!","bar":42}"#;
    let bytes = input.as_bytes();
    let reader = Cursor::new(bytes);
    let mut reader = Tap::new(reader);
    
    let foo: Foo = match serde_json::from_reader(&mut reader) {
        Ok(foo) => foo,
        Err(e) => {
            eprintln!("Error serializing Foo from input: '{}'\n error was: {}",
                      String::from_utf8_lossy(&reader.bytes),
                      e.description());
            return Err(());
        }
    };
    
    assert_eq!(
        foo,
        Foo {
            foo: "hello, world!".to_string(),
            bar: 42
        }
    );
    assert_eq!(reader.bytes, bytes);
    Ok(())
}

No runtime deps