Crate flatvec[][src]

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/

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.


An indirection-collapsing container with minimal allocation

A wrapper over the innards of a FlatVec which exposes mutating operations which cannot corrupt other elements when inserting a new element.


Implement FromFlat<'a, Flattened> for Dest to get a Dest from a FlatVec<Flattened>

Implement IntoFlat<Flattened> for Source to insert a Source into a FlatVec<Flattened>

Type Definitions