pub struct UnsizedVec<T: ?Sized> { /* private fields */ }
Expand description

Like Vec, but can store unsized values.

Memory layout

When T is a Sized type, the memory layout is more or less the same as Vec<T>; pointer to a heap allocation, as well as a usize each for length and capacity. Elements are laid out in order, one after the other.

When T is a slice, there are two heap allocations. The first is to the slices themsleves; they are laid out end-to-end, one after the other, with no padding in between. The second heap allocation is to a list of offsets, to store where each element begins and ends.

When T is neither of the above, there are still two allocations. The first allocation still contains the elements of the vector laid out end-to-end, but now every element is padded to at least the alignment of the most-aligned element in the UnsizedVec. For this reason, adding a new element to the Vec with a larger alignment than any of the elements already in it will add new padding to all the existing elements, which will involve a lot of copying and probably a reallocation. If you want to avoid excessive copies, use the UnsizedVec::with_byte_capacity_align function to set the padding up front.

In the third case, the second allocation, in addition to storing offsets, also stores the pointer metadata of each element.

Example

#![feature(unsized_fn_params)]
use core::fmt::Debug;
use unsized_vec::{*, emplace::box_new_with};

// `Box::new()` necessary only to coerce the values to trait objects.
let obj: Box<dyn Debug> = Box::new(1);
let obj_2: Box<dyn Debug> = Box::new((97_u128, "oh noes"));
let mut vec: UnsizedVec<dyn Debug> = unsized_vec![*obj, *obj_2];
for traitobj in &vec {
    dbg!(traitobj);
};

assert_eq!(vec.len(), 2);

let popped = box_new_with(|e| vec.pop_unwrap(e));
dbg!(&*popped);

assert_eq!(vec.len(), 1);

Implementations

Make a new, empty UnsizedVec.

Make a new UnsizedVec with at least the the given capacity and alignment. (align is ignored for Aligned types).

Panics

Panics if align is not a power of two.

Add element to end of the vec.

Insert element into vec at given index, shifting following elements to the right.

Remove an element from the end of the vec. This returns the element, which is unsized, through the “emplacer” mechanism. Call box_new_with(|e| your_unsized_vec.pop_unwrap(e)) to get the element boxed.

Panics

Panics if len is 0.

Remove the element at the given index, shifting those after it to the left. This returns the element, which is unsized, through the “emplacer” mechanism. Call box_new_with(|e| your_unsized_vec.remove(index, e)) to get the element boxed.

Doesn’t shink the allocation, and doesn’t reduce alignment.

Panics

Panics if index is out of bounds.

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Executes the destructor for this type. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
The returned type after indexing.
Performs the indexing (container[index]) operation. Read more
Performs the mutable indexing (container[index]) operation. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.