Crate unsized_vec
source ·Expand description
UnsizedVec<T>
, like Vec<T>
, is a contiguous growable array
type with heap-allocated contents. Unlike Vec<T>
, it can store unsized values.
Vectors have O(1) indexing, amortized O(1) push (to the end) and
O(1) pop (from the end). When T
is Sized
, they use one heap
allocation; when it is not, they use two.
This crate is nightly-only and experimental.
§Examples
You can explicitly create an UnsizedVec
with UnsizedVec::new
:
let v: UnsizedVec<i32> = UnsizedVec::new();
…or by using the unsize_vec!
macro:
let v: UnsizedVec<[u32]> = unsize_vec![];
let v: UnsizedVec<dyn Debug> = unsize_vec![1_u32, "hello!", 3.0_f64, (), -17_i32];
You can push
or push_unsize
values onto the end of a vector (which will grow the vector
as needed):
let mut v: UnsizedVec<dyn Debug> = unsize_vec![1_u32, "hello!", 3.0_f64, (), -17_i32];
v.push_unsize(3);
Popping values works in much the same way:
let mut v: UnsizedVec<dyn Debug> = unsize_vec![1_u32, "hello!"];
// "hello!" is copied directly into a new heap allocation
let two: Option<Box<dyn Debug>> = v.pop_into().map(box_new_with);
Vectors also support indexing (through the Index
and IndexMut
traits):
let mut v: UnsizedVec<dyn Debug> = unsize_vec![1_u32, "hello!", [(); 0]];
let greeting = &v[1];
dbg!(greeting);
Macros§
- Like
unsized_vec
, but unsizes its arguments using theUnsize
trait.
Structs§
- The error type for
try_reserve
methods. - The iterator returned by
UnsizedVec::iter
. - The iterator returned by
UnsizedVec::iter_mut
. - Like
Vec
, but can store unsized values.