Crate tap_reader[−][src]
Tapped reader
Wraps an existing reader and copies the read bytes into it's own buffer
This can be useful for when you are deserializing a stream of bytes, and want to be able to provide the original input for log messages, error messages, etc
Example
extern crate tap_reader; extern crate serde_json; extern crate serde; #[macro_use] extern crate serde_derive; use std::io::Cursor; use tap_reader::Tap; #[derive(Debug, Deserialize, PartialEq)] struct Foo { foo: String, bar: usize, } 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);
extern crate tap_reader; use std::io::{Cursor, Read}; use tap_reader::Tap; let input = "hello, world!".to_string(); let bytes = input.as_bytes(); let cursor = Cursor::new(bytes); let mut reader = Tap::new(cursor); let mut output = String::new(); reader.read_to_string(&mut output)?; assert_eq!(output, input); assert_eq!(bytes, &reader.bytes[..]);
Structs
Tap |
|