Crates.io | tbytes |
lib.rs | tbytes |
version | 0.1.0 |
source | src |
created_at | 2023-12-07 02:50:37.311015 |
updated_at | 2024-03-25 18:04:39.475568 |
description | A tiny library for reading and writing typed data into buffers |
homepage | https://gitlab.com/mavka/libs/t-bytes |
repository | https://gitlab.com/mavka/libs/t-bytes |
max_upload_size | |
id | 1060652 |
size | 94,691 |
T-Bytes is a tiny library for reading and writing typed data into bytes buffers. It is designed mainly for no-std
,
no-alloc
targets or crates which consider to support no-std
.
Documentation can be found here.
cargo add tbytes
It would be easier to start from an example. Here we create a buffer and populate it with heterogeneous values.
use tbytes::errors::TBytesError;
use tbytes::{BytesWriterFor, TBytesReader, TBytesReaderFor, TBytesWriter};
fn main() -> Result<(), TBytesError> {
type Content = (i16, f32, [u8; 2], [i16; 2]);
let mut buffer = [0u8; 12];
let mut writer = TBytesWriter::from(buffer.as_mut_slice());
let into_values: Content = (-1048, 0.32, [10, 31], [-1, 240]);
writer.write(into_values.0)?;
writer.write(into_values.1)?;
writer.write_slice(into_values.2.as_slice())?;
writer.write_array(into_values.3)?;
assert!(matches!(writer.write(0u8), Err(TBytesError::OutOfBounds)));
let reader = TBytesReader::from(buffer.as_slice());
let mut from_values: Content = Content::default();
from_values.0 = reader.read()?;
from_values.1 = reader.read()?;
from_values.2 = reader.read_array()?;
from_values.3 = reader.read_array()?;
assert!(matches!(
reader.read() as Result<u8, TBytesError>,
Err(TBytesError::OutOfBounds)
));
assert_eq!(into_values, from_values);
Ok(())
}
See examples for advanced usage.
basic
— basic read/write.
cargo run --example basic
We want to keep as simple as possible but simplicity comes with a price.
little endian
is supported.TBytesReaderFor
/ TBytesWriterFor
. Or even
better, submit a pull request!Here we simply comply with the suggested dual licensing according to Rust API Guidelines (C-PERMISSIVE).
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.