Scroll - making scrolling through buffers fun since 2016
Scroll - making scrolling through buffers fun since 2016
_______________
()==( (@==()
'______________'|
| |
| ἀρετή |
__)_____________|
()==( (@==()
'--------------'
Add to your Cargo.toml
[dependencies]
scroll = "0.11"Scroll implements several traits for read/writing generic containers (byte buffers are currently implemented by default). Most familiar will likely be the Pread trait, which at its basic takes an immutable reference to self, an immutable offset to read at, (and a parsing context, more on that later), and then returns the deserialized value.
Because self is immutable, all reads can be performed in parallel and hence are trivially parallelizable.
A simple example demonstrates its flexibility:
…Pread and PwriteScroll implements a custom derive that can provide Pread and Pwrite implementations for your structs.
use scroll::{Pread, Pwrite, BE};
#[derive(Pread, Pwrite)]
struct Data {
one: u32,
two: u16,
three: u8,
}
fn main() -> Result {
let bytes: [u8; 7] = [0xde, 0xad, 0xbe, 0xef, 0xfa, 0xce, 0xff];
// Read a single `Data` at offset zero in big-endian byte order.
let data: Data = bytes.pread_with(0, BE)?;
assert_eq!(data.one, 0xdeadbeef);
assert_eq!(data.two, 0xface);
assert_eq!(data.three, 0xff);
// Write it back to a buffer
let mut out: [u8; 7] = [0; 7];
out.pwrite_with(data, 0, BE)?;
assert_eq!(bytes, out);
Ok(())
}This feature is not enabled by default, you must enable the derive feature in Cargo.toml to use it:
[dependencies]
scroll = { version = "0.10", features = ["derive"] }std::io APIScroll can also read/write simple types from a std::io::Read or std::io::Write implementor. The built-in numeric types are taken care of for you. If you want to read a custom type, you need to implement the FromCtx (how to parse) and SizeWith (how big the parsed thing will be) traits. You must compile with default features. For example:
use std::io::Cursor;
use scroll::IOread;
fn main() -> Result {
let bytes_ = [0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0xef,0xbe,0x00,0x00,];
let mut bytes = Cursor::new(bytes_);
// this will bump the cursor's Seek
let foo = bytes.ioread::()?;
// ..ditto
let bar = bytes.ioread::()?;
Ok(())
}Similarly, we can write to anything that implements std::io::Write quite naturally:
use scroll::{IOwrite, LE, BE};
use std::io::{Write, Cursor};
fn main() -> Result {
let mut bytes = [0x0u8; 10];
let mut cursor = Cursor::new(&mut bytes[..]);
cursor.write_all(b"hello")?;
cursor.iowrite_with(0xdeadbeef as u32, BE)?;
assert_eq!(cursor.into_inner(), [0x68, 0x65, 0x6c, 0x6c, 0x6f, 0xde, 0xad, 0xbe, 0xef, 0x0]);
Ok(())
}Scroll is designed to be highly configurable - it allows you to implement various context (Ctx) sensitive traits, which then grants the implementor automatic uses of the Pread and/or Pwrite traits.
For example, suppose we have a datatype and we want to specify how to parse or serialize this datatype out of some arbitrary byte buffer. In order to do this, we need to provide a TryFromCtx impl for our datatype.
In particular, if we do this for the [u8] target, using the convention (usize, YourCtx), you will automatically get access to
calling pread_with:: on arrays of bytes.
…Please see the official documentation, or a simple example for more.
Any ideas, thoughts, or contributions are welcome!
No open issues yet, or sync has not completed.