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

tap-reader

包装现有的读取器并将读取的字节复制到自己的缓冲区中

2个稳定版本

使用旧的Rust 2015

1.0.1 2018年9月9日

#2349编码

Download history 106/week @ 2024-03-15 133/week @ 2024-03-22 192/week @ 2024-03-29 114/week @ 2024-04-05 111/week @ 2024-04-12 140/week @ 2024-04-19 120/week @ 2024-04-26 111/week @ 2024-05-03 107/week @ 2024-05-10 90/week @ 2024-05-17 91/week @ 2024-05-24 100/week @ 2024-05-31 53/week @ 2024-06-07 83/week @ 2024-06-14 104/week @ 2024-06-21 48/week @ 2024-06-28

305 每月下载量
3 个crate中使用 (2 个直接使用)

GPL-3.0 许可证

15KB
79

tap-reader

包装现有的读取器并将读取的字节复制到自己的缓冲区中

安装

将以下行添加到您的 Cargo.toml

[dependencies]
tap-reader = "1"

用法

要使用,请将以下行添加到您的crate根目录 (src/lib.rs, src/main.rs, 等.)

extern crate tap_reader;

use tap_reader::Tap;

示例

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(())
}

无运行时依赖