#vec #safe #array-vec #inline #heap #default

no-std tinyvec

tinyvec provides 100% safe vec-like data structures

33 releases (14 stable)

1.8.0 Jul 8, 2024
1.6.1 Jun 25, 2024
1.6.0 Apr 25, 2022
1.5.1 Nov 9, 2021
0.3.3 Mar 26, 2020

#37 in Data structures

Download history 2189153/week @ 2024-07-19 2167998/week @ 2024-07-26 2276627/week @ 2024-08-02 2564591/week @ 2024-08-09 2454162/week @ 2024-08-16 2652351/week @ 2024-08-23 2305001/week @ 2024-08-30 2534822/week @ 2024-09-06 2250457/week @ 2024-09-13 2488240/week @ 2024-09-20 2796696/week @ 2024-09-27 3173617/week @ 2024-10-04 3340549/week @ 2024-10-11 3055613/week @ 2024-10-18 2380691/week @ 2024-10-25 2191429/week @ 2024-11-01

11,442,104 downloads per month
Used in 11,812 crates (143 directly)

Zlib OR Apache-2.0 OR MIT

330KB
13K SLoC

License:Zlib Minimum Rust Version crates.io docs.rs

Unsafe-Zero-Percent

tinyvec

A 100% safe crate of vec-like types. Not just safe at the public API boundary, fully safe for all internal code too: #![forbid(unsafe_code)]

The provided types are as follows:

  • ArrayVec is an array-backed vec-like data structure. It panics on overflow.
  • SliceVec is similar, but using a &mut [T] as the data backing.
  • TinyVec (alloc feature) is an enum that's either an Inline(ArrayVec) or a Heap(Vec). If a TinyVec is Inline and would overflow its array it automatically transitions to Heap and continues whatever it was doing.

To attain this "100% safe code" status there is one compromise: the element type of the vecs must implement Default.

For more API details, please see the docs.rs documentation

tinyvec Alternatives?

Maybe you don't want to use tinyvec, there's other crates you might use instead!

  • arrayvec is a crate with array-backed structures.
  • smallvec is a crate where the array-backed data can be moved to the heap on overflow.

The main difference is that both of those crates use unsafe code. This mostly allows them to get rid of the Default limitation for elements that tinyvec imposes. The smallvec and arrayvec crates are generally correct, but there's been occasional bugs leading to UB. With tinyvec, any uncaught bugs can't lead to UB, because the crate is safe code all the way through. If you want that absolute level of assurance against UB, use tinyvec.

Dependencies

~190KB