byteloaf

Crates.iobyteloaf
lib.rsbyteloaf
version0.2.0
sourcesrc
created_at2021-11-29 15:58:17.248436
updated_at2021-11-29 15:58:17.248436
descriptionUse a heap-allocated byte buffer (a 'loaf') via independently owned, accessed, and moved slices.
homepage
repositoryhttps://github.com/sirkibsirkib/byteloaf/
max_upload_size
id489350
size30,180
Christopher Esterhuyse (sirkibsirkib)

documentation

README

Byte Loaf 🍞

What

This library lets one treat a heap-allocated byte buffer as a loaf of bread 🍞; its contents can be arbitrarily partitioned into slices, which can be passed around, accessed, and owned independently.

Concretely, a single-part loaf can be created, and treated as a mutable byte buffer as usual.

let x = LoafPart::new(5);
let slice = x.as_slice_mut();

slice.write_all(b"hello").unwrap();
assert_eq!(slice, b"hello");

slice[3] = b'Q';
assert_eq!(slice, b"helQo");

Ownership of the loaf's bytes can be further sub-divided by splitting existing parts.

let y = x.split_at(3);
assert_eq!(x.as_slice(), b"hel"  );
assert_eq!(y.as_slice(),    b"lo");

For parts owning contiguous bytes, their sub-division of ownership can be re-drawn, or joined into one part.

x.with_try_resplit_at(y, 0..4).unwrap();
assert_eq!(x.as_slice(), b"hell" );
assert_eq!(y.as_slice(),     b"o");
let z = x.with_try_join(y).unwrap();
assert_eq!(z.as_slice(), b"hello");

Why

This library was created as a utility for storing independently-owned byte slices, while minimizing the number of heap allocations. For example, this is useful for hanging onto the contents of sent UDP datagrams until their receipt is acknowledged later.

Commit count: 8

cargo fmt