A utility library to read and write primitive types on a wrapped buffer.
To use buf-view
, first add this to your Cargo.toml
:
toml
[dependencies]
buf-view = "0.1.0"
Wrap a buffer to read only.
```rust use buf_view::BufView;
let buf = [0, 1, 2, 3, 4, 5, 6, 7]; let mut buf_view = BufView::wrap(&buf);
asserteq!(bufview.readu8(), 0); asserteq!(bufview.readu16(), 0x0102); asserteq!(bufview.readu32(), 0x03040506); asserteq!(bufview.getu16(1), 0x0102);
// wrap from vector let v = vec![0, 1, 2, 3, 4, 5, 6, 7]; let mut bufview = BufView::wrap(v.asslice()); asserteq!(bufview.readu8(), 0); asserteq!(bufview.readu32(), 0x01020304);
// wrap from &str let s = "01234567"; let mut bufview = BufView::wrap(s.asbytes()); asserteq!(bufview.readu8(), 0x30); asserteq!(bufview.readu32(), 0x31323334); ```
Wrap a buffer to read and write.
```rust use buf_view::BufViewMut;
let mut buf = [0u8;7]; let mut buf_view = BufViewMut::wrap(&mut buf);
bufview.writeu8(0); bufview.writeu16(0x0102); bufview.writeu32(0x03040506);
asserteq!(bufview.readu8(), 0); asserteq!(bufview.readu16(), 0x0102); asserteq!(bufview.readu32(), 0x03040506); asserteq!(bufview.getu16(1), 0x0102); ```
This project is licensed under the MIT license.
All contributions are welcomed!