#data-structures #single #container #optimized #stack #vec #safe

no-std singlevec

Vec-like container optimized for storing only a single item

14 stable releases

1.8.0 Mar 7, 2025
1.7.0 Feb 27, 2025
1.6.0 Oct 21, 2024
1.4.2 Jun 14, 2024

#419 in Algorithms

Download history 3/week @ 2024-12-04 22/week @ 2024-12-11 7/week @ 2025-02-12 207/week @ 2025-02-26 134/week @ 2025-03-05

348 downloads per month

MIT/Apache

38KB
829 lines

singlevec

For when you need a Vec, but intend to store only one item most of the time. In that case, that single item can be stored on the stack and will fall back to heap storage for multiple items.

Like a tinyvec::TinyVec<[T; 1]> that shares methods in common with both Vec and Option.

Simple and 100% safe code for a simple use case.


lib.rs:

SingleVec is Vec-like container type optimized for storing a single item.

0 or 1 items are stored internally as a standard Option - which can be kept on the stack - but falls back to a standard Vec for multiple items - which are stored on the heap.

Although SingleVec shares many of the same traits and methods as Vec, it also shares many of the same methods as Option and Iterator where appropriate. Since only a single optional item is intended to be the common case, those methods can avoid iteration altogether.

Other Features

  • serde provides Serialize and Deserialize support, provided that the inner type also has the same implementation.

Dependencies

~160KB