buf-view

Crates.iobuf-view
lib.rsbuf-view
version0.1.0
sourcesrc
created_at2022-02-15 14:50:33.344891
updated_at2022-02-15 14:50:33.344891
descriptionRead/write primitive types on a wrapper buffer view
homepagehttps://github.com/guojing7/buf-view
repositoryhttps://github.com/guojing7/buf-view
max_upload_size
id532761
size30,025
guojing (guojing7)

documentation

README

buf-view

A utility library to read and write primitive types on a wrapped buffer.

usage

To use buf-view, first add this to your Cargo.toml:

[dependencies]
buf-view = "0.1.0"

BufView

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);

BufViewMut

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);

License

This project is licensed under the MIT license.

Contribution

All contributions are welcomed!

Commit count: 5

cargo fmt