#fixed-size #heap #compile-time #length #array #capacity

fixed-capacity-vec

Variable-length buffer backed by a fixed-size heap array

4 releases (2 stable)

1.0.1 Sep 14, 2023
1.0.0 Sep 13, 2023
0.1.0 Sep 1, 2023
0.0.1 Aug 30, 2023

#114 in Data structures

Download history 1177/week @ 2024-07-22 2985/week @ 2024-07-29 29043/week @ 2024-08-05 11159/week @ 2024-08-12 3406/week @ 2024-08-19 2630/week @ 2024-08-26 2351/week @ 2024-09-02 1863/week @ 2024-09-09 122315/week @ 2024-09-16 51036/week @ 2024-09-23 377198/week @ 2024-09-30 673503/week @ 2024-10-07 540591/week @ 2024-10-14 116766/week @ 2024-10-21 6878/week @ 2024-10-28 1843/week @ 2024-11-04

800,197 downloads per month
Used in 56 crates (via hashx)

MIT/Apache

25KB
420 lines

FixedCapacityVec - like a Vec but with capacity fixed at compile-time

FixedCapacityVec is a simple special-purpose data structure. One use case is applications which want to incrementally construct a nontrivial fixed-size table.

Relationship to types from std

All of the following types store only the actual buffer on the heap, and they are interconvertible without copying the data.

Type Size and representation (as eg on stack) Full? Mutability
Vec 3 words: pointer, length, capacity maybe indefinitely appendable
Box<[T]> 2 words: pointer, length = capacity always length fixed at runtime
FixedCapacityVec<T, N> 2 words: pointer, length maybe appendable, but capacity fixed at compile time
Box<[T; N]> 1 word: pointer always length fixed at compile time
         Vec<T>              HEAP                     Box<[T]>          HEAP 
             +----------+     +------------+            +----------+     +----------+
             | pointer -----> | T          |            | pointer -----> | T        |
             | length   |     | T ...      |            | length   |     | T ...    |
             | capacity |     +--length----+            +----------+     +--length--+
             +----------+     | (spare)    |
                              +--capacity--+

   FixedCapacityVec<T, N>    HEAP                     Box<[T; N]>        HEAP
             +----------+     +-------------+            +----------+     +---------+
             | pointer -----> | T           |            | pointer -----> | T       |
             | length   |     | T ...       |            +----------+     | T ...   |
             +----------+     |--length-----|                             +--N------+
                              | (spare)     |
                              +--N----------|

Relationship to some other crates' vec-like types

FixedCapacityVec serves a different purpose to ArrayVec and SmallVec.

The data for a FixedCapacityVec always lives on the heap.

The advantage of a FixedCapacityVec over Vec is that the size of the FixedCapacityVec itself is small, and that the capacity does not need to be represented or looked up. And it can be converted very cheaply to a boxed array, or to a Vec or boxed slice, without copying.

ArrayVec is also a fixed-capacity vec-like structure. But the data for an ArrayVec lives next to the metadata. So to convert a full ArrayVec to a boxed array, the data must be copied. Even if it was a full Box<ArrayVec<..>>, at the very least, its allocation would have to be shrunk. One of ArrayVec's use cases is its ability to incrementally construct (usually short) arrays without heap allocation.

SmallVec's benefit is that a small amount of data can be stored on the stack, not that the SmallVec itself is particularly small; indeed, both the ArrayVec and SmallVec are big enough for N items T.

   ArrayVec<T, N>      	   SmallVec<[T; N]>          SmallVec<[T; N]>
     +-----------+           +--------------+        +---------------+    HEAP
     | T         |           | length <= N  |        | capacity > N  |     +------------+
     | T ...     |           +--0-----------+        | pointer ----------> | T          |
     +--length---+           | T            |  OR    | length        |     | T ...      |
     | (uninit)  |           | T ...        |        +---------------+     +--length----+
     +--N--------+           |--length------|        | (unused)      |     | (uninit)   |
     | length    |           | (spare)      |        |               |     +--capacity--+
     +-----------+           +--N-----------+        +---------------+

No runtime deps

Features