Expand description
An indirection-collapsing container that generalizes nested.
A FlatVec can be used like a Vec<String> or Vec<Vec<u8>>, but with a maximum of 2 heap
allocations instead of n + 1 (currently it’s always 2 but that should change with 1.51).
Insertion into and retrieval from a FlatVec is mediated by two traits, IntoFlat and
FromFlat, which are both parameterized on two types. The simplest way to use this crate is to
impl IntoFlat<T, u8> for T and impl FromFlat<'_, T, u8> for T for some type T in your crate.
But since the interface supports a generic backing type parameter, the flattened input objects
can be stored as any representation that is convenient. u8 is a reasonable default choice,
but may not be quite right for your application.
Additionally, since FromFlat has a lifetime parameter, accessing the stored objects in a
FlatVec can be a zero-copy operation. For example, one may flatten objects with indirections
into a dense in-memory storage, then access them later via a reference-wrapping handle type.
A simple example of this is in examples/domain_name.rs.
This interface is extremely powerful and essentially amounts to in-memory serialization and
conversion all in one. For example, a user can construct a FlatVec that compresses all of its
elements with gzip. This is not necessarily a good idea, but you can do it.
Structs§
- FlatVec
- An indirection-collapsing container with minimal allocation
- Storage
- A wrapper over the innards of a
FlatVecwhich exposes mutating operations which cannot corrupt other elements when inserting a new element.
Traits§
- From
Flat - Implement
FromFlat<'a, Flattened> for Destto get aDestfrom aFlatVec<Flattened> - Into
Flat - Implement
IntoFlat<Flattened> for Sourceto insert aSourceinto aFlatVec<Flattened>