| Crates.io | buf-view |
| lib.rs | buf-view |
| version | 0.1.0 |
| created_at | 2022-02-15 14:50:33.344891+00 |
| updated_at | 2022-02-15 14:50:33.344891+00 |
| description | Read/write primitive types on a wrapper buffer view |
| homepage | https://github.com/guojing7/buf-view |
| repository | https://github.com/guojing7/buf-view |
| max_upload_size | |
| id | 532761 |
| size | 30,025 |
A utility library to read and write primitive types on a wrapped buffer.
To use buf-view, first add this to your Cargo.toml:
[dependencies]
buf-view = "0.1.0"
Wrap a buffer to read only.
use buf_view::BufView;
let buf = [0, 1, 2, 3, 4, 5, 6, 7];
let mut buf_view = BufView::wrap(&buf);
assert_eq!(buf_view.read_u8(), 0);
assert_eq!(buf_view.read_u16(), 0x0102);
assert_eq!(buf_view.read_u32(), 0x03040506);
assert_eq!(buf_view.get_u16(1), 0x0102);
// wrap from vector
let v = vec![0, 1, 2, 3, 4, 5, 6, 7];
let mut buf_view = BufView::wrap(v.as_slice());
assert_eq!(buf_view.read_u8(), 0);
assert_eq!(buf_view.read_u32(), 0x01020304);
// wrap from &str
let s = "01234567";
let mut buf_view = BufView::wrap(s.as_bytes());
assert_eq!(buf_view.read_u8(), 0x30);
assert_eq!(buf_view.read_u32(), 0x31323334);
Wrap a buffer to read and write.
use buf_view::BufViewMut;
let mut buf = [0u8;7];
let mut buf_view = BufViewMut::wrap(&mut buf);
buf_view.write_u8(0);
buf_view.write_u16(0x0102);
buf_view.write_u32(0x03040506);
assert_eq!(buf_view.read_u8(), 0);
assert_eq!(buf_view.read_u16(), 0x0102);
assert_eq!(buf_view.read_u32(), 0x03040506);
assert_eq!(buf_view.get_u16(1), 0x0102);
This project is licensed under the MIT license.
All contributions are welcomed!