Skip to main content

RawSequence

Struct RawSequence 

pub struct RawSequence { /* private fields */ }
Expand description

A sequence that stores heterogeneous values with proper alignment.

The RawSequence provides a memory-efficient way to store heterogeneous values while maintaining proper alignment requirements for each type. It uses an internal buffer that raises its base alignment to satisfy each stored type.

The caller owns destruction of stored values, including before discarding the sequence. Zero-sized values occupy no logical bytes.

§Examples

let mut values = cel_runtime::RawSequence::new();
values.push(String::from("hello"));
assert_eq!(unsafe { values.next::<String>(0) }.0, "hello");
unsafe { values.drop_in_place::<String>(0) };

Implementations§

§

impl RawSequence

pub fn new() -> RawSequence

Creates a new empty RawSequence. The sequence initially requests 4096-byte base alignment and raises it when a stored type requires a wider alignment.

§Examples
let values = cel_runtime::RawSequence::new();
assert!(values.is_empty());

pub fn push<T>(&mut self, value: T)

Pushes a value onto the sequence.

The value is stored at an address that satisfies its alignment requirements. Automatically grows the internal buffer if needed.

  • Complexity: Amortized O(size of T + alignment of T) time and space at an existing base alignment. Raising the base alignment also moves O(n) stored bytes and replaces the buffer allocation.
§Examples
let mut values = cel_runtime::RawSequence::new();
values.push(42u32);
assert_eq!(*unsafe { values.next::<u32>(0) }.0, 42);

pub unsafe fn drop_in_place<T>(&mut self, p: usize) -> usize

Destroys the next value after p and returns the position after it.

§Safety

p must precede a live stored T, with only its alignment padding between them. The value must not be used or destroyed again.

§Examples
let mut values = cel_runtime::RawSequence::new();
values.push(String::from("owned"));
unsafe { values.drop_in_place::<String>(0) };

pub unsafe fn next<T>(&self, p: usize) -> (&T, usize)

Retrieves a reference to the next value at the specified position.

§Safety
  • The position must point to a valid value of type T
  • The caller must ensure that the value is actually of type T

Returns a tuple containing:

  • A reference to the value
  • The position immediately after the value
§Examples
let mut values = cel_runtime::RawSequence::new();
values.push(42u32);
let (value, next) = unsafe { values.next::<u32>(0) };
assert_eq!((*value, next), (42, 4));

pub fn len(&self) -> usize

Returns the number of bytes in the RawSequence, also referred to as its ‘length’.

§Examples
let mut values = cel_runtime::RawSequence::new();
values.push(42u32);
assert_eq!(values.len(), 4);

pub fn is_empty(&self) -> bool

Returns true if no logical bytes are stored, including only zero-sized values.

§Examples
let mut values = cel_runtime::RawSequence::new();
values.push(());
assert!(values.is_empty());

Trait Implementations§

§

impl Default for RawSequence

§

fn default() -> RawSequence

Creates an empty sequence.

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.