Struct RawVec
pub struct RawVec { /* private fields */ }Expand description
Stores bytes at a fixed power-of-two alignment after allocation.
The logical length excludes leading alignment padding. Moving or reserving storage preserves the logical bytes but invalidates pointers into the buffer.
§Examples
use cel_runtime::RawVec;
let mut bytes = RawVec::with_base_alignment(64);
bytes.reserve(8);
assert!(bytes.capacity() >= 8);
assert!(bytes.is_empty());Implementations§
§impl RawVec
impl RawVec
pub fn with_base_alignment(base_alignment: usize) -> RawVec
pub fn with_base_alignment(base_alignment: usize) -> RawVec
Creates a new RawVec with base alignment.
- Precondition:
base_alignmentis a power of two.
§Examples
use cel_runtime::RawVec;
use std::mem::align_of;
let vec = RawVec::with_base_alignment(align_of::<u32>());pub fn with_base_alignment_and_capacity(
base_alignment: usize,
capacity: usize,
) -> RawVec
pub fn with_base_alignment_and_capacity( base_alignment: usize, capacity: usize, ) -> RawVec
Creates a new RawVec with base alignment and initial capacity.
- Precondition:
base_alignmentis a power of two. - Complexity: Allocates O(
capacity + base_alignment) bytes.
§Examples
let bytes = cel_runtime::RawVec::with_base_alignment_and_capacity(64, 8);
assert!(bytes.capacity() >= 8);
assert!(bytes.is_empty());pub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the capacity of the vector in bytes.
§Examples
let bytes = cel_runtime::RawVec::with_base_alignment_and_capacity(8, 16);
assert!(bytes.capacity() >= 16);pub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the current length of the vector in bytes.
§Examples
let bytes = cel_runtime::RawVec::with_base_alignment(8);
assert_eq!(bytes.len(), 0);pub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the vector contains no bytes.
§Examples
assert!(cel_runtime::RawVec::with_base_alignment(8).is_empty());pub fn reserve(&mut self, addition: usize)
pub fn reserve(&mut self, addition: usize)
Reserves capacity for at least additional more elements. The collection may reserve more
space to guarantee amortized constant time growth. After calling reserve, capacity will be
greater than or equal to self.len() + additional. Does nothing if capacity is already
sufficient.
- Postcondition: The logical length and existing bytes remain unchanged.
- Complexity: O(
self.len()) time when growing; allocates O(new capacity- base alignment) bytes.
§Panics
Panics if the new capacity exceeds isize::MAX bytes.
§Examples
let mut bytes = cel_runtime::RawVec::with_base_alignment(8);
bytes.reserve(16);
assert!(bytes.capacity() >= 16);
assert!(bytes.is_empty());pub unsafe fn set_len(&mut self, len: usize)
pub unsafe fn set_len(&mut self, len: usize)
Sets the length of the vector.
§Safety
len must not exceed the capacity. The elements are MaybeUninit<u8>;
their payload bytes need not be initialized.
§Examples
let mut bytes = cel_runtime::RawVec::with_base_alignment_and_capacity(8, 1);
unsafe { bytes.set_len(1) };
bytes[0].write(42);
assert_eq!(unsafe { bytes[0].assume_init() }, 42);pub unsafe fn as_mut_ptr(&mut self) -> *mut MaybeUninit<u8>
pub unsafe fn as_mut_ptr(&mut self) -> *mut MaybeUninit<u8>
Returns a raw mutable pointer to the vector’s buffer, or a dangling raw pointer valid for zero sized reads if the vector didn’t allocate.
This method guarantees that for the purpose of the aliasing model, this method does not
materialize a reference to the underlying slice, and thus the returned pointer will remain
valid when mixed with other calls to Self::as_ptr, Self::as_mut_ptr. Note
that calling other methods that materialize references to the slice, or references to
specific elements you are planning on accessing through this pointer, may still invalidate
this pointer.
§Safety
The pointer is valid until the vector buffer is reallocated or the vector’s lifetime ends.
§Examples
let mut bytes = cel_runtime::RawVec::with_base_alignment_and_capacity(64, 1);
assert_eq!(unsafe { bytes.as_mut_ptr() } as usize % 64, 0);pub unsafe fn as_ptr(&self) -> *const MaybeUninit<u8>
pub unsafe fn as_ptr(&self) -> *const MaybeUninit<u8>
Returns a raw pointer to the vector’s buffer, or a dangling raw pointer valid for zero sized reads if the vector didn’t allocate.
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 Self::as_mut_ptr.
This method guarantees that for the purpose of the aliasing model, this method does not
materialize a reference to the underlying slice, and thus the returned pointer will remain
valid when mixed with other calls to Self::as_ptr, Self::as_mut_ptr. Note
that calling other methods that materialize mutable references to the slice, or mutable
references to specific elements you are planning on accessing through this pointer, as well
as writing to those elements, may still invalidate this pointer.
§Safety
The pointer is valid until the vector buffer is reallocated or the vector’s lifetime ends.
§Examples
let bytes = cel_runtime::RawVec::with_base_alignment_and_capacity(64, 1);
assert_eq!(unsafe { bytes.as_ptr() } as usize % 64, 0);pub fn truncate(&mut self, len: usize)
pub fn truncate(&mut self, len: usize)
Shortens the vector, keeping the first len elements and dropping
the rest.
If len is greater or equal to the vector’s current length, this has
no effect.
§Examples
let mut bytes = cel_runtime::RawVec::with_base_alignment_and_capacity(8, 2);
unsafe { bytes.set_len(2) };
bytes.truncate(1);
assert_eq!(bytes.len(), 1);