Skip to main content

RawVec

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

pub fn with_base_alignment(base_alignment: usize) -> RawVec

Creates a new RawVec with base alignment.

  • Precondition: base_alignment is 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

Creates a new RawVec with base alignment and initial capacity.

  • Precondition: base_alignment is 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

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

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

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)

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)

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>

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>

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)

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);

Trait Implementations§

§

impl Debug for RawVec

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl<I> Index<I> for RawVec
where I: SliceIndex<[MaybeUninit<u8>]>,

§

fn index(&self, index: I) -> &<RawVec as Index<I>>::Output

Borrows bytes at an index within the logical length.

§

type Output = <I as SliceIndex<[MaybeUninit<u8>]>>::Output

The returned type after indexing.
§

impl<I> IndexMut<I> for RawVec
where I: SliceIndex<[MaybeUninit<u8>]>,

§

fn index_mut(&mut self, index: I) -> &mut <RawVec as Index<I>>::Output

Mutably borrows bytes at an index within the logical length.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.