#vec #vector

minivec

A version of Vec that's only the size of a single pointer

10 unstable releases (3 breaking)

0.4.0 Sep 19, 2021
0.3.1 Jul 10, 2021
0.3.0 Apr 23, 2021
0.2.3 Feb 22, 2021
0.1.0 Sep 27, 2020

#1580 in Data structures

Download history 370/week @ 2023-10-29 445/week @ 2023-11-05 512/week @ 2023-11-12 433/week @ 2023-11-19 184/week @ 2023-11-26 127/week @ 2023-12-03 242/week @ 2023-12-10 436/week @ 2023-12-17 390/week @ 2023-12-24 585/week @ 2023-12-31 387/week @ 2024-01-07 255/week @ 2024-01-14 516/week @ 2024-01-21 268/week @ 2024-01-28 28/week @ 2024-02-04 66/week @ 2024-02-11

904 downloads per month
Used in 3 crates

BSL-1.0 license

87KB
2K SLoC

MiniVec

std::vec::Vec is a cool class but it's just too big! MiniVec is only the size of a pointer.

Acknowledgements

This library is dedicated to the great Glen Joseph Fernandes whose constant tutelage has been instrumental in making me the programmer that I am.


lib.rs:

A space-optimized version of alloc::vec::Vec that's only the size of a single pointer! Ideal for low-level APIs where ABI calling conventions will typically require most structs be spilled onto the stack and copied instead of being passed solely in registers.

For example, in the x64 msvc ABI:

There's a strict one-to-one correspondence between a function call's arguments and the registers used for those arguments. Any argument that doesn't fit in 8 bytes, or isn't 1, 2, 4, or 8 bytes, must be passed by reference. A single argument is never spread across multiple registers.

In addition, its single word size makes it ideal for use as a struct member where multiple inclusions of Vec as a field can balloon the size.

MiniVec is a #[repr(transparent)] struct so its layout is that of core::ptr::NonNull<u8>.


In general, MiniVec aims to be API compatible with what's currently stable in the stdlib so some Nightly features are not supported. MiniVec also supports myriad extensions, one such being support for over-alignment via the associated function with_alignment.

MiniVec has stable implementations of the following nightly-only associated functions on Vec:

MiniVec has the following associated functions not found in Vec:

MiniVec has the following extensions to the existing Vec API:

  • push returns a mutable reference to the newly created element

Eventual TODO's:

  • add try_reserve methods once stable
  • add myriad specializations to associated functions such as FromIterator once stable
  • add Allocator support once stable

Dependencies

~170KB