musli-zerocopy 0.0.54

Zero copy primitives for use in Müsli.
Documentation

musli-zerocopy

Zero copy primitives for use in Müsli.

This provides a base set of tools to deal with types which do not require copying during deserialization.

Limits

Offset, the size of unsized values, and slice lengths are all limited to 32-bit, that is the longest unsized value that can be stored in 2**32 bytes.

Examples

use musli_zerocopy::{AlignedBuf, Pair, Unsized, ZeroCopy};

#[derive(ZeroCopy)]
#[repr(C)]
struct Custom {
    field: u32,
    string: Unsized<str>,
}

let mut buf = AlignedBuf::new();

let string = buf.store_unsized("string")?;

let c1 = buf.store(&Custom { field: 1, string })?;
let c2 = buf.store(&Custom { field: 2, string })?;

let mut map = Vec::new();

map.push(Pair::new(1, c1));
map.push(Pair::new(2, c2));

let map = buf.insert_map(&mut map)?;
let buf = buf.as_aligned();
let map = buf.bind(map)?;

let c1 = buf.load(map.get(&1)?.context("Missing key 1")?)?;
assert_eq!(c1.field, 1);
assert_eq!(buf.load(c1.string)?, "string");

let c2 = buf.load(map.get(&2)?.context("Missing key 2")?)?;
assert_eq!(c2.field, 2);
assert_eq!(buf.load(c2.string)?, "string");

assert!(map.get(&3)?.is_none());