4 stable releases
1.0.3 | Jul 11, 2020 |
---|---|
1.0.0 | Dec 9, 2019 |
#10 in #binary-reader
19KB
312 lines
This crate provides convenience methods for reading and writing data to binary buffers. It supports writing primitive types, as well as Strings and Vectors to in-memory streams and files.
Installation
This crate works with Cargo and is on
crates.io. Add it to your Cargo.toml
like so:
[dependencies]
buffer_io = "1"
If you want to write data to a buffer you do it like so:
use crate::buffer::{BufferReader, BufferWriter, SeekOrigin};
use std::io::Cursor;
let mut buffer = BufferWriter::new(Cursor::new(Vec::new()));
buffer.write_u32(9001)?;
buffer.write_u32(9002).unwrap()?;
buffer.write_string("Hello World!")?;
You can then return the full buffer as a vector:
let data = buffer.to_vec()?;
Reading buffers is just as simple.
let mut reader = BufferReader::new(File::open("test.bin")?);
let magic = reader.read_u32()?;
let body = reader.read_string()?;
Dependencies
~240–690KB
~16K SLoC