1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
//! A low-level, zero-copy and panic-free binary serializer and deserializer.
//!
//! # Usage
//!
//! First, add the following to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! byte = "0.3"
//! ```
//!
//! `Byte` is a `no_std` library; it can be used in any `#![no_std]` situation or crate.
//!
//! # Overview
//!
//! `Byte` is designed for encoding or decoding binary data in a fast and low level way.
//! A classical use case is I2C communication packages encoding.
//!
//! `Byte` provides two core traits `TryRead` and `TryWrite`.
//! Types implement these traits can be serialized into or deserialized from byte slices.
//!
//! The library is meant to be simple, and it will always be.
//!
//! # Examples
//!
//! Deserialize a `u32` from bytes:
//!
//! ```
//! use byte::*;
//!
//! let bytes: &[u8] = &[0xde, 0xad, 0xbe, 0xef];
//!
//! let offset = &mut 0;
//! let num = bytes.read_with::<u32>(offset, BE).unwrap();
//! assert_eq!(num, 0xdeadbeef);
//! assert_eq!(*offset, 4);
//! ```
//!
//! Deserialize a `&str` from bytes:
//!
//! ```
//! use byte::*;
//! use byte::ctx::{Str, NULL};
//!
//! let bytes: &[u8] = b"hello, world!\0dump";
//!
//! let offset = &mut 0;
//! let str = bytes.read_with::<&str>(offset, Str::Delimiter(NULL)).unwrap();
//! assert_eq!(str, "hello, world!");
//! assert_eq!(*offset, 14);
//! ```
//!
//! `Byte` supports serializing and deserializing language primitives by default.
//!
//! - `&str` (with `Str` context)
//! - `&[u8]` (with `Byte` context)
//! - `u8`, `i8`, `u64`, `f64` ... (with `Endian` context)
//! - `bool`
//!
//! # Define custom serializable/deserializable types
//!
//! In this example, we implement `TryRead` and `TryWrite` for the `Header` type,
//! which has a variable-length name and a boolean field.
//!
//! ## Binary Structure
//!
//! ```text
//! | | Name's Length (Big Endian) | Name | Enabled |
//! | ----- | -------------------------- | ---- | ---- | ---- | ---- | ---- | ------- |
//! | Byte | 0 | 5 | 'H' | 'E' | 'L' | 'L' | 'O' | 0 |
//! ```
//!
//! ## Example
//!
//! The only thing you may be curious about is the returned usize;
//! that's the number of bytes consumed by the read/write operation.
//!
//! ```
//! use byte::*;
//! use byte::ctx::*;
//!
//! struct Header<'a> {
//! name: &'a str,
//! enabled: bool,
//! }
//!
//! impl<'a> TryRead<'a, Endian> for Header<'a> {
//! fn try_read(bytes: &'a [u8], endian: Endian) -> Result<(Self, usize)> {
//! let offset = &mut 0;
//!
//! let name_len = bytes.read_with::<u16>(offset, endian)? as usize;
//! let header = Header {
//! name: bytes.read_with::<&str>(offset, Str::Len(name_len))?,
//! enabled: bytes.read::<bool>(offset)?,
//! };
//!
//! Ok((header, *offset))
//! }
//! }
//!
//! impl<'a> TryWrite<Endian> for Header<'a> {
//! fn try_write(self, bytes: &mut [u8], endian: Endian) -> Result<usize> {
//! let offset = &mut 0;
//!
//! bytes.write_with::<u16>(offset, self.name.len() as u16, endian)?;
//! bytes.write::<&str>(offset, self.name)?;
//! bytes.write::<bool>(offset, self.enabled)?;
//!
//! Ok(*offset)
//! }
//! }
//! ```
//!
//! ## Usage
//!
//! ```ignore
//! let bytes = [0, 5, b"H"[0], b"E"[0], b"L"[0], b"L"[0], b"O"[0], 0];
//!
//! let header: Header = bytes.read_with(&mut 0, BE).unwrap();
//!
//! assert_eq!(header.name, "HELLO");
//! assert_eq!(header.enabled, false);
//!
//! let mut write = [0u8; 8];
//! write.write_with(&mut 0, header, BE).unwrap();
//! assert_eq!(write, bytes);
//! ```
#![no_std]
#![forbid(unsafe_code)]
pub mod ctx;
use core::marker::PhantomData;
pub use ctx::{BE, LE};
/// A specialized Result type for `Byte`
pub type Result<T> = core::result::Result<T, Error>;
/// The error type for the `byte` crate.
///
/// - `Error::BadOffset` will be returned when the offset parameter exceeds the slice's length.
///
/// - `Error::BadInput` and `Error::Incomplete` will be returned when `try_read()` or
/// `try_write()` finds the bytes are invalid or not long enough to determine their validity.
///
/// Note that we usually use `bytes.read()` in `try_read()` which may return `Error::BadOffset`,
/// indicating incomplete data. So the error will automatically be converted into
/// `Error::Incomplete` if you use `bytes.read()` (the same applies to `write()`).
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum Error {
/// The requested data is bigger than the available range
Incomplete,
/// The offset is invalid
BadOffset(usize),
/// The requested data content is invalid
BadInput { err: &'static str },
}
/// A helper function that checks whether the given length exceeded the length
/// of the slice; returns `Err(Error::Incomplete)` otherwise.
///
/// # Example
///
/// ```
/// use byte::*;
///
/// let bytes = [0u8; 4];
/// assert_eq!(check_len(&bytes, 4), Ok(4));
/// assert_eq!(check_len(&bytes, 5), Err(Error::Incomplete));
/// ```
#[inline]
pub fn check_len(bytes: &[u8], len: usize) -> Result<usize> {
if bytes.len() < len {
Err(Error::Incomplete)
} else {
Ok(len)
}
}
/// A data structure that can be deserialized.
/// Types implementing this trait can be `read()` from a byte slice.
pub trait TryRead<'a, Ctx = ()>
where
Self: Sized,
{
/// Try to read from a byte slice using a specific context.
///
/// Read the value out of bytes; the bytes passed in are splitted by offset
/// and should be read at head.
/// If successful, `try_read()` should return a tuple with the value and the
/// number of bytes consumed.
///
/// # Example
///
/// ```
/// use byte::*;
///
/// // Demo type showing how to read boolean from bytes.
/// // This functionality is already provided by this crate.
/// pub struct Bool(bool);
///
/// impl<'a> TryRead<'a> for Bool {
/// #[inline]
/// fn try_read(bytes: &'a [u8], _ctx: ()) -> Result<(Self, usize)> {
/// check_len(bytes, 1)?;
///
/// Ok((Bool(bytes[0] != 0), 1))
/// }
/// }
/// ```
fn try_read(bytes: &'a [u8], ctx: Ctx) -> Result<(Self, usize)>;
}
/// A data structure that can be serialized.
/// Types implement this trait can be `write()` into a byte slice.
pub trait TryWrite<Ctx = ()> {
/// Try to write to a byte slice using a specific context.
///
/// Write the value into bytes; the bytes passed in are splitted by offset
/// and should be written at head.
/// If successful `try_write()` should return the number of bytes written.
///
/// # Example
///
/// ```
/// use byte::*;
///
/// pub struct HasBool(bool);
///
/// impl TryWrite for HasBool {
/// #[inline]
/// fn try_write(self, bytes: &mut [u8], _ctx: ()) -> Result<usize> {
/// check_len(bytes, 1)?;
///
/// bytes[0] = if self.0 { u8::max_value() } else { 0 };
///
/// Ok(1)
/// }
/// }
/// ```
fn try_write(self, bytes: &mut [u8], ctx: Ctx) -> Result<usize>;
}
/// Extension methods for byte slices.
///
/// # Offset
///
/// The offset is the first parameter of each method.
///
/// It tells the starting position, and will be increased by the number
/// which will be increased by size the operation consumed.
pub trait BytesExt<Ctx> {
/// Reads a value from a byte slice using the default context.
///
/// # Example
///
/// ```
/// use byte::*;
///
/// let bytes: &[u8] = &[0, 1];
///
/// let bool1: bool = bytes.read(&mut 0).unwrap();
/// let bool2: bool = bytes.read(&mut 1).unwrap();
///
/// assert_eq!(bool1, false);
/// assert_eq!(bool2, true);
/// ```
fn read<'a, T>(&'a self, offset: &mut usize) -> Result<T>
where
T: TryRead<'a, Ctx>,
Ctx: Default,
{
self.read_with(offset, Default::default())
}
/// Reads a value from a byte slice specifying the context.
///
/// # Example
///
/// ```
/// use byte::*;
/// use byte::ctx::*;
///
/// let bytes: &[u8] = b"hello, world!";
///
/// let str: &str = bytes.read_with(&mut 0, Str::Delimiter(b"!"[0])).unwrap();
/// assert_eq!(str, "hello, world");
/// ```
fn read_with<'a, T>(&'a self, offset: &mut usize, ctx: Ctx) -> Result<T>
where
T: TryRead<'a, Ctx>;
/// Reads multiple values of the same type using an iterator.
///
/// # Example
///
/// ```
/// use byte::*;
/// use byte::ctx::*;
///
/// let bytes: &[u8] = b"hello\0world\0dead\0beef\0more";
/// let mut offset = 0;
/// {
/// let mut iter = bytes.read_iter(&mut offset, Str::Delimiter(NULL));
/// assert_eq!(iter.next(), Some("hello"));
/// assert_eq!(iter.next(), Some("world"));
/// assert_eq!(iter.next(), Some("dead"));
/// assert_eq!(iter.next(), Some("beef"));
/// assert_eq!(iter.next(), None);
/// }
/// assert_eq!(offset, 22);
/// ```
fn read_iter<'a, 'i, T>(&'a self, offset: &'i mut usize, ctx: Ctx) -> Iter<'a, 'i, T, Ctx>
where
T: TryRead<'a, Ctx>,
Ctx: Clone;
/// Writes a value into a byte slice using the default context.
///
/// # Example
///
/// ```
/// use byte::*;
///
/// let mut bytes = [0u8; 2];
///
/// bytes.write(&mut 0, false).unwrap();
/// bytes.write(&mut 1, true).unwrap();
///
/// assert_eq!(bytes, [0, 0xff]);
/// ```
fn write<T>(&mut self, offset: &mut usize, t: T) -> Result<()>
where
T: TryWrite<Ctx>,
Ctx: Default,
{
self.write_with(offset, t, Default::default())
}
/// Writes a value into a byte slice specifiying the context.
///
/// # Example
///
/// ```
/// use byte::*;
/// use byte::ctx::*;
///
/// let mut bytes_be = [0u8; 2];
/// let mut bytes_le = [0u8; 2];
///
/// bytes_be.write_with::<u16>(&mut 0, 0xff, BE).unwrap();
/// bytes_le.write_with::<u16>(&mut 0, 0xff, LE).unwrap();
///
/// assert_eq!(bytes_be, [0, 0xff]);
/// assert_eq!(bytes_le, [0xff, 0]);
/// ```
fn write_with<T>(&mut self, offset: &mut usize, t: T, ctx: Ctx) -> Result<()>
where
T: TryWrite<Ctx>;
}
impl<Ctx> BytesExt<Ctx> for [u8] {
#[inline]
fn read_with<'a, T>(&'a self, offset: &mut usize, ctx: Ctx) -> Result<T>
where
T: TryRead<'a, Ctx>,
{
let slice = self;
if *offset > slice.len() {
return Err(Error::BadOffset(*offset));
};
match TryRead::try_read(&slice[*offset..], ctx) {
Ok((t, size)) => {
*offset += size;
Ok(t)
}
Err(Error::BadOffset(_)) => Err(Error::Incomplete),
Err(err) => Err(err),
}
}
fn read_iter<'a, 'i, T>(&'a self, offset: &'i mut usize, ctx: Ctx) -> Iter<'a, 'i, T, Ctx>
where
T: TryRead<'a, Ctx>,
Ctx: Clone,
{
Iter {
bytes: self,
offset,
ctx,
phantom: PhantomData,
}
}
fn write_with<T>(&mut self, offset: &mut usize, t: T, ctx: Ctx) -> Result<()>
where
T: TryWrite<Ctx>,
{
let slice = self;
if *offset > slice.len() {
return Err(Error::BadOffset(*offset));
};
match TryWrite::try_write(t, &mut slice[*offset..], ctx) {
Ok(size) => {
*offset += size;
Ok(())
}
Err(Error::BadOffset(_)) => Err(Error::Incomplete),
Err(err) => Err(err),
}
}
}
/// An iterator that reads values of the same type from a byte slice.
///
/// # Example
///
/// ```
/// use byte::*;
/// use byte::ctx::*;
///
/// let bytes: &[u8] = b"hello\0world\0dead\0beef\0more";
/// let mut offset = 0;
/// {
/// let mut iter = bytes.read_iter(&mut offset, Str::Delimiter(NULL));
/// assert_eq!(iter.next(), Some("hello"));
/// assert_eq!(iter.next(), Some("world"));
/// assert_eq!(iter.next(), Some("dead"));
/// assert_eq!(iter.next(), Some("beef"));
/// assert_eq!(iter.next(), None);
/// }
/// assert_eq!(offset, 22);
/// ```
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct Iter<'a, 'i, T, Ctx>
where
T: TryRead<'a, Ctx>,
Ctx: Clone,
{
bytes: &'a [u8],
offset: &'i mut usize,
ctx: Ctx,
phantom: PhantomData<T>,
}
impl<'a, 'i, T, Ctx> Iterator for Iter<'a, 'i, T, Ctx>
where
T: TryRead<'a, Ctx>,
Ctx: Clone,
{
type Item = T;
#[inline]
fn next(&mut self) -> Option<T> {
TryRead::try_read(&self.bytes[*self.offset..], self.ctx.clone())
.ok()
.map(|(t, size)| {
*self.offset += size;
t
})
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(0, None)
}
}