Struct rkyv::util::AlignedVec [−][src]
pub struct AlignedVec { /* fields omitted */ }
Expand description
A vector of bytes that aligns its memory to 16 bytes.
Implementations
Constructs a new, empty AlignedVec
.
The vector will not allocate until elements are pushed into it.
Examples
use rkyv::AlignedVec;
let mut vec = AlignedVec::new();
Constructs a new, empty AlignedVec
with the specified capacity.
The vector will be able to hold exactly capacity
bytes without reallocating. If
capacity
is 0, the vector will not allocate.
Examples
use rkyv::AlignedVec;
let mut vec = AlignedVec::with_capacity(10);
// The vector contains no items, even though it has capacity for more
assert_eq!(vec.len(), 0);
assert_eq!(vec.capacity(), 10);
// These are all done without reallocating...
for i in 0..10 {
vec.push(i);
}
assert_eq!(vec.len(), 10);
assert_eq!(vec.capacity(), 10);
// ...but this may make the vector reallocate
vec.push(11);
assert_eq!(vec.len(), 11);
assert!(vec.capacity() >= 11);
Clears the vector, removing all values.
Note that this method has no effect on the allocated capacity of the vector.
Examples
use rkyv::AlignedVec;
let mut v = AlignedVec::new();
v.extend_from_slice(&[1, 2, 3, 4]);
v.clear();
assert!(v.is_empty());
Shrinks the capacity of the vector as much as possible.
It will drop down as close as possible to the length but the allocator may still inform the vector that there is space for a few more elements.
Examples
use rkyv::AlignedVec;
let mut vec = AlignedVec::with_capacity(10);
vec.extend_from_slice(&[1, 2, 3]);
assert_eq!(vec.capacity(), 10);
vec.shrink_to_fit();
assert!(vec.capacity() >= 3);
Returns an unsafe mutable pointer to the vector’s buffer.
The caller must ensure that the vector outlives the pointer this function returns, or else it will end up pointing to garbage. Modifying the vector may cause its buffer to be reallocated, which would also make any pointers to it invalid.
Examples
use rkyv::AlignedVec;
// Allocate vecotr big enough for 4 bytes.
let size = 4;
let mut x = AlignedVec::with_capacity(size);
let x_ptr = x.as_mut_ptr();
// Initialize elements via raw pointer writes, then set length.
unsafe {
for i in 0..size {
*x_ptr.add(i) = i as u8;
}
x.set_len(size);
}
assert_eq!(&*x, &[0, 1, 2, 3]);
Extracts a mutable slice of the entire vector.
Equivalent to &mut s[..]
.
Examples
use rkyv::AlignedVec;
let mut vec = AlignedVec::new();
vec.extend_from_slice(&[1, 2, 3, 4, 5]);
assert_eq!(vec.as_mut_slice().len(), 5);
for i in 0..5 {
assert_eq!(vec.as_mut_slice()[i], i as u8 + 1);
vec.as_mut_slice()[i] = i as u8;
assert_eq!(vec.as_mut_slice()[i], i as u8);
}
Returns a raw pointer to the vector’s buffer.
The caller must ensure that the vector outlives the pointer this function returns, or else it will end up pointing to garbage. Modifying the vector may cause its buffer to be reallocated, which would also make any pointers to it invalid.
The caller must also ensure that the memory the pointer (non-transitively) points to is
never written to (except inside an UnsafeCell
) using this pointer or any pointer derived
from it. If you need to mutate the contents of the slice, use
as_mut_ptr
.
Examples
use rkyv::AlignedVec;
let mut x = AlignedVec::new();
x.extend_from_slice(&[1, 2, 4]);
let x_ptr = x.as_ptr();
unsafe {
for i in 0..x.len() {
assert_eq!(*x_ptr.add(i), 1 << i);
}
}
Extracts a slice containing the entire vector.
Equivalent to &s[..]
.
Examples
use rkyv::AlignedVec;
let mut vec = AlignedVec::new();
vec.extend_from_slice(&[1, 2, 3, 4, 5]);
assert_eq!(vec.as_slice().len(), 5);
for i in 0..5 {
assert_eq!(vec.as_slice()[i], i as u8 + 1);
}
Returns the number of elements the vector can hold without reallocating.
Examples
use rkyv::AlignedVec;
let vec = AlignedVec::with_capacity(10);
assert_eq!(vec.capacity(), 10);
Reserves capacity for at least additional
more bytes to be inserted into the given
AlignedVec
. The collection may reserve more space to avoid frequent reallocations. After
calling reserve
, capacity will be greater than or equal to self.len() + additional
. Does
nothing if capacity is already sufficient.
Panics
Panics if the new capacity exceeds usize::MAX
bytes.
Examples
use rkyv::AlignedVec;
let mut vec = AlignedVec::new();
vec.push(1);
vec.reserve(10);
assert!(vec.capacity() >= 11);
Returns true
if the vector contains no elements.
Examples
use rkyv::AlignedVec;
let mut v = Vec::new();
assert!(v.is_empty());
v.push(1);
assert!(!v.is_empty());
Returns the number of elements in the vector, also referred to as its ‘length’.
Examples
use rkyv::AlignedVec;
let mut a = AlignedVec::new();
a.extend_from_slice(&[1, 2, 3]);
assert_eq!(a.len(), 3);
Copies and appends all bytes in a slice to the AlignedVec
.
The elements of the slice are appended in-order.
Examples
use rkyv::AlignedVec;
let mut vec = AlignedVec::new();
vec.push(1);
vec.extend_from_slice(&[2, 3, 4]);
assert_eq!(vec.as_slice(), &[1, 2, 3, 4]);
Removes the last element from a vector and returns it, or None
if it is empty.
Examples
use rkyv::AlignedVec;
let mut vec = AlignedVec::new();
vec.extend_from_slice(&[1, 2, 3]);
assert_eq!(vec.pop(), Some(3));
assert_eq!(vec.as_slice(), &[1, 2]);
Reserves the minimum capacity for exactly additional
more elements to be inserted in the
given AlignedVec
. After calling reserve_exact
, capacity will be greater than or equal
to self.len() + additional
. Does nothing if the capacity is already sufficient.
Note that the allocator may give the collection more space than it requests. Therefore, capacity can not be relied upon to be precisely minimal. Prefer reserve if future insertions are expected.
Panics
Panics if the new capacity overflows usize
.
Examples
use rkyv::AlignedVec;
let mut vec = AlignedVec::new();
vec.push(1);
vec.reserve_exact(10);
assert!(vec.capacity() >= 11);
Forces the length of the vector to new_len
.
This is a low-level operation that maintains none of the normal invariants of the type.
Safety
new_len
must be less than or equal tocapacity()
- The elements at
old_len..new_len
must be initialized
Examples
use rkyv::AlignedVec;
let mut vec = AlignedVec::with_capacity(3);
vec.extend_from_slice(&[1, 2, 3]);
// SAFETY:
// 1. `old_len..0` is empty to no elements need to be initialized.
// 2. `0 <= capacity` always holds whatever capacity is.
unsafe {
vec.set_len(0);
}
Converts the vector into Box<[u8]>
.
This will preserve the alignment guarantees provided by AlignedVec
. Note that this will
drop any excess capacity.
Examples
use rkyv::AlignedVec;
let mut v = AlignedVec::new();
v.extend_from_slice(&[1, 2, 3]);
let slice = v.into_boxed_slice();
Any excess capacity is removed:
use rkyv::AlignedVec;
let mut vec = AlignedVec::with_capacity(10);
vec.extend_from_slice(&[1, 2, 3]);
assert_eq!(vec.capacity(), 10);
let slice = vec.into_boxed_slice();
assert_eq!(slice.len(), 3);
Converts the vector into Vec<u8>
.
This will preserve the alignment guarantees provided by AlignedVec
until the vector is
modified. This will not drop any excess capacity, unlike
into_boxed_slice()
.
Examples
use rkyv::AlignedVec;
let mut v = AlignedVec::new();
v.extend_from_slice(&[1, 2, 3]);
let vec = v.into_vec();
assert_eq!(vec.len(), 3);
assert_eq!(vec.as_slice(), &[1, 2, 3]);
Any excess capacity is preserved:
use rkyv::AlignedVec;
let mut vec = AlignedVec::with_capacity(10);
vec.extend_from_slice(&[1, 2, 3]);
assert_eq!(vec.capacity(), 10);
let vec = vec.into_vec();
assert_eq!(vec.len(), 3);
assert_eq!(vec.capacity(), 10);
Trait Implementations
Performs the conversion.
Write a buffer into this writer, returning how many bytes were written. Read more
Attempts to write an entire buffer into this writer. Read more
Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
can_vector
)Determines if this Write
r has an efficient write_vectored
implementation. Read more
write_all_vectored
)Attempts to write multiple buffers into this writer. Read more
Writes a formatted string into this writer, returning any error encountered. Read more
Auto Trait Implementations
impl RefUnwindSafe for AlignedVec
impl UnwindSafe for AlignedVec
Blanket Implementations
type ArchivedMetadata = ()
type ArchivedMetadata = ()
The archived version of the pointer metadata for this type.
Converts some archived metadata to the pointer metadata for itself.
Mutably borrows from an owned value. Read more