10 releases

0.2.8 Aug 24, 2023
0.2.7 Aug 11, 2023
0.1.1 Aug 2, 2023

#1199 in Encoding

Download history 12/week @ 2024-02-20 29/week @ 2024-02-27 11/week @ 2024-03-26 31/week @ 2024-04-02

92 downloads per month

MIT license

22KB
450 lines

Lightpack

crates.io Build docs.rs

A lighweight, no-std binary serialization library.

Example

Lightpack provides derivable traits for encoding (Pack) and decoding (Unpack), as well as for determining a type's encoded size (Size):

#[derive(Size, Pack, Unpack, Debug)]
struct Point {
    x: i16,
    y: i16,
}

To encode, call pack with an endianness (e.g. lightpack::byteorder::BigEndian) on a &mut [u8] slice:

let mut buffer = [0u8; Point::SIZE];
Point { x: 3, y: 4 }.pack::<BigEndian>(&mut buffer);
// => buffer == [0, 3, 0, 4]

To decode, call unpack on a &[u8] slice:

Point::unpack::<BigEndian>(&[0, 3, 0, 4]).unwrap()
// => Point { x: 3, y: 4 }

For a complete example, check out examples/point.rs.

Dependencies

~0.4–0.9MB
~20K SLoC