Skip to main content

Crate compactvec

Crate compactvec 

Source
Expand description

§compact-vec

A CompactVec is a Vec<u32>-compatible container that automatically stores unsigned integers in the smallest possible byte width (1, 2, 3, or 4 bytes per element), upgrading in place when a newly pushed value exceeds the current maximum representable value.

§Quick example

use compact_vec::CompactVec;

let mut cv = CompactVec::new();
cv.push(0);          // stored as u8  – 1 byte
cv.push(255);        // stored as u8  – 1 byte
cv.push(256);        // triggers upgrade: all elements re-encoded as u16
assert_eq!(cv.width_bits(), 16);
assert_eq!(cv.get(0), Some(0));
assert_eq!(cv.get(2), Some(256));

§Memory savings

Value rangeWidthSavings vs Vec<u32>
0 – 2558 bit75 %
256 – 65 53516 bit50 %
65 536 – 16 M24 bit25 %
16 M – 4 G32 bit0 %

§Feature flags

FlagEffect
serdeImplements Serialize / Deserialize

Structs§

CompactVec
A Vec<u32>-compatible container that packs integers into the smallest possible byte width, upgrading automatically when a new value demands more space.
IntoIter
Consuming iterator over a CompactVec, produced by CompactVec::into_iter.
Iter
Borrowing iterator over a CompactVec.

Enums§

BitWidth
Discriminant for the current per-element storage width of a CompactVec.