Expand description

Docs

This library provides a data view for reading and writing data in a byte array.

It also works with [no_std] environment.

By default, this library uses little endian as the default endianness. But you can override the endianness by using BE (for big endian) or NE (for native endian) in fetures flag.

For example, if you want to use big endian,

[dependencies]
data-view = { version = "5", features = ["BE"] }

Examples

Add this to your project’s Cargo.toml file.

[dependencies]
data-view = "5"

DataView

use data_view::DataView;

let mut view = DataView::new([0; 8]);

view.write(12_u16);
view.write(34_u16);
view.write(5678_u32);

view.offset = 0;

assert_eq!(view.read::<u16>(), Some(12));
assert_eq!(view.read::<u16>(), Some(34));
assert_eq!(view.read::<u32>(), Some(5678));

View

use data_view::View;

let mut buf = [0; 8];

buf.write_at(0, 42_u16);
buf.write_at(2, 123_u32);

assert_eq!(buf.read_at::<u16>(0), Some(42));
assert_eq!(buf.read_at::<u32>(2), Some(123));
Alternative

There are many alternative libraries,

But I didn’t like API of these libraries. The have a lot of functions for reading and writing data. For example, read_u16, read_u32, write_i64, And so on…

Luckily, Rust support Generics function, This is why this library exists.

Structs

This struct represents a data view for reading and writing data in a byte array. When read/write, This increment current offset by the size of the value.

Traits

This trait contains unsafe methods for efficiently reading and writing data.

A data view for reading and writing data in byte array.