tbytes/
lib.rs

1//! # T-Bytes
2//!
3//! T-Bytes is a tiny library for reading and writing typed data into bytes buffers. It is designed
4//! mainly for `no-std`, `no-alloc` targets or crates which consider to support for `no-std`.
5//!
6//! # Usage
7//!
8//! It would be easier to start from an example. Here we create a buffer and populate it with
9//! heterogeneous values.
10//!
11//! ```rust
12//! use tbytes::errors::TBytesError;
13//! use tbytes::{TBytesWriterFor, TBytesReader, TBytesReaderFor, TBytesWriter};
14//!
15//! fn main() -> Result<(), TBytesError> {
16//!     type Content = (i16, f32, [u8; 2], [i16; 2]);
17//!     let mut buffer = [0u8; 12];
18//!
19//!     let mut writer = TBytesWriter::from(buffer.as_mut_slice());
20//!
21//!     let into_values: Content = (-1048, 0.32, [10, 31], [-1, 240]);
22//!     writer.write(into_values.0)?;
23//!     writer.write(into_values.1)?;
24//!     writer.write_slice(into_values.2.as_slice())?;
25//!     writer.write_array(into_values.3)?;
26//!
27//!     assert!(matches!(writer.write(0u8), Err(TBytesError::OutOfBounds)));
28//!
29//!     let reader = TBytesReader::from(buffer.as_slice());
30//!
31//!     let mut from_values: Content = Content::default();
32//!     from_values.0 = reader.read()?;
33//!     from_values.1 = reader.read()?;
34//!     from_values.2 = reader.read_array()?;
35//!     from_values.3 = reader.read_array()?;
36//!
37//!     assert!(matches!(
38//!         reader.read() as Result<u8, TBytesError>,
39//!         Err(TBytesError::OutOfBounds)
40//!     ));
41//!
42//!     assert_eq!(into_values, from_values);
43//!
44//!     Ok(())
45//! }
46//! ```
47//!
48//! # Limitations
49//!
50//! * At the moment only `little endian` is supported.
51//! * We currently supports only numeric types and arrays of such types. But you can always
52//!   implement your own [`TBytesReaderFor`] / [`TBytesWriterFor`]. Or even better, submit a
53//!   [pull request](https://gitlab.com/mavka/libs/t-bytes/-/merge_requests)!
54#![warn(missing_docs)]
55#![deny(rustdoc::broken_intra_doc_links)]
56#![doc(
57    html_logo_url = "https://gitlab.com/mavka/libs/t-bytes/-/raw/main/avatar.png?ref_type=heads",
58    html_favicon_url = "https://gitlab.com/mavka/libs/t-bytes/-/raw/main/avatar.png?ref_type=heads"
59)]
60#![cfg_attr(not(feature = "std"), no_std)]
61
62pub mod bytes_reader;
63pub use bytes_reader::{TBytesReader, TBytesReaderBackend, TBytesReaderFor};
64pub mod bytes_writer;
65pub use bytes_writer::{TBytesWriter, TBytesWriterBackend, TBytesWriterFor};
66
67pub mod errors;