Skip to main content

RawSegment

Struct RawSegment 

Source
pub struct RawSegment { /* private fields */ }
Expand description

A segment represents a sequence of operations that can be executed.

Each operation is stored along with its data in the segment’s storage, and can manipulate values on a stack during execution. Destruction releases instruction captures in insertion order, including remaining captures when one destructor panics. Captures need not be Send or Sync, so the erased program remains thread-confined.

§Examples

See the module tutorial.

Implementations§

Source§

impl RawSegment

Source

pub fn new() -> Self

Creates a new empty segment.

§Examples

See RawSegment.

Source

pub fn raw0_<F>(&mut self, op: F)
where F: Fn(&mut RawStack) -> Result<()> + 'static,

Pushes a closure that manipulates the stack directly and returns ().

Operation errors propagate at execution after frame cleanup.

§Examples
use cel_runtime::RawSegment;
let mut body = RawSegment::new();
body.raw0_(|stack| { stack.push(7u8); Ok(()) });
assert_eq!(unsafe { body.call0::<u8>() }?, 7);
Source

pub fn raw0<R, F>(&mut self, op: F)
where F: Fn(&mut RawStack) -> Result<R> + 'static, R: 'static,

Pushes a fallible closure that manipulates the remaining stack and returns R.

Operation errors propagate at execution after frame cleanup.

§Examples

See RawSegment.

Source

pub fn push_op0<R, F>(&mut self, op: F)
where F: Fn() -> R + 'static, R: 'static,

Pushes a nullary operation that returns a value of type R.

  • Complexity: Amortized O(size of F + alignment of F) capture storage; raising capture alignment additionally moves all stored capture bytes.
§Examples

See RawSegment.

Source

pub fn push_op1<T, R, F>(&mut self, op: F, padding0: bool)
where F: Fn(T) -> R + 'static, T: 'static, R: 'static,

Pushes a unary operation that takes one argument of type T and returns a value of type R.

padding0 describes the consumed operand’s preceding alignment padding.

§Examples

See RawSegment.

Source

pub fn raw1<T, R, F>(&mut self, op: F, padding0: bool)
where F: Fn(&mut RawStack, T) -> Result<R> + 'static, T: 'static, R: 'static,

Pushes a fallible unary operation that can manipulate the remaining stack.

padding0 describes the consumed operand’s preceding alignment padding. Operation errors propagate at execution after frame cleanup.

§Examples

See RawSegment.

Source

pub fn drop1<T, F>(&mut self, op: F, padding0: bool)
where F: Fn(T) + 'static, T: 'static,

Pushes an operation that consumes one stack value without producing a result.

padding0 describes the consumed operand’s preceding alignment padding.

§Examples
use cel_runtime::RawSegment;
let mut body = RawSegment::new();
body.push_op0(|| String::from("temporary"));
body.drop1(|value: String| drop(value), false);
body.push_op0(|| true);
assert!(unsafe { body.call0::<bool>() }?);
Source

pub fn push_op2<T, U, R, F>(&mut self, op: F, padding0: bool, padding1: bool)
where F: Fn(T, U) -> R + 'static, T: 'static, U: 'static, R: 'static,

Pushes a binary operation that takes two arguments of types T and U and returns a value of type R.

Callback arguments and padding flags follow operand source order.

§Examples
use cel_runtime::RawSegment;
let mut body = RawSegment::new();
body.push_op2(|first: String, second: u8| (first, second), false, false);
assert_eq!(unsafe { body.call2::<_, _, (String, u8)>((String::from("first"), 7u8)) }?,
    (String::from("first"), 7));
Source

pub fn raw2<T, U, R, F>(&mut self, op: F, padding0: bool, padding1: bool)
where F: Fn(&mut RawStack, T, U) -> Result<R> + 'static, T: 'static, U: 'static, R: 'static,

Pushes a fallible binary operation that can manipulate the remaining stack.

Arguments and padding flags follow source order. Operation errors propagate at execution after frame cleanup.

§Examples

See RawSegment.

Source

pub fn push_op3<T, U, V, R, F>( &mut self, op: F, padding0: bool, padding1: bool, padding2: bool, )
where F: Fn(T, U, V) -> R + 'static, T: 'static, U: 'static, V: 'static, R: 'static,

Pushes a ternary operation that takes three arguments of types T, U, and V and returns a value of type R.

Arguments and padding flags follow source order.

§Examples
use cel_runtime::RawSegment;
let mut body = RawSegment::new();
body.push_op0(|| 1u8);
body.push_op0(|| 2u8);
body.push_op0(|| 3u8);
body.push_op3(|first: u8, second: u8, third: u8| (first, second, third), false, false, false);
assert_eq!(unsafe { body.call0::<(u8, u8, u8)>() }?, (1, 2, 3));
Source

pub fn raw3<T, U, V, R, F>( &mut self, op: F, padding0: bool, padding1: bool, padding2: bool, )
where F: Fn(&mut RawStack, T, U, V) -> Result<R> + 'static, T: 'static, U: 'static, V: 'static, R: 'static,

Pushes a fallible ternary operation that can manipulate the remaining stack.

Arguments and padding flags follow source order. Operation errors propagate at execution after frame cleanup.

§Examples

See RawSegment.

Source

pub unsafe fn call0_stack(&self, stack: &mut RawStack) -> Result<()>

Executes all operations in the segment on the supplied stack.

§Errors

Halts execution and returns an error if any operation returns an error.

§Safety

The stack’s alignment, origin, operand types, and padding must match every operation. Callback accesses must preserve the frame’s ownership. Violating these requirements can cause undefined behavior.

  • Complexity: O(n) dispatches plus callback costs for n operations.
§Examples
use cel_runtime::{RawSegment, RawStack};
let mut body = RawSegment::new();
body.push_op0(|| String::from("owned"));
let mut stack = RawStack::with_base_alignment(std::mem::align_of::<String>());
unsafe { body.call0_stack(&mut stack) }?;
assert_eq!(unsafe { stack.pop::<String>(false) }, "owned");
Source

pub unsafe fn call0<T>(&self) -> Result<T>
where T: 'static,

Executes all operations in the segment and returns the final result.

§Errors

Halts execution and returns an error if any operation returns an error.

§Safety

Operations must accept an empty initial stack and end in a live T with no preceding alignment gap. All operand types and padding must match their registered operations. Violating these requirements can cause undefined behavior.

  • Complexity: O(n) dispatches plus callback costs for n operations; allocates the maximum live stack.
§Examples

See RawSegment.

Source

pub unsafe fn call1<A: 'static, T>(&self, arg: A) -> Result<T>
where T: 'static,

Executes all operations in the segment with one argument of type A and returns the final result.

§Errors

Halts execution and returns an error if any operation returns an error.

§Safety

Operations must accept the initial A and end in a live T with no preceding alignment gap. All operand types and padding must match their registered operations. Violating these requirements can cause undefined behavior.

  • Complexity: The execution and allocation costs of Self::call0.
§Examples
use cel_runtime::RawSegment;
let mut body = RawSegment::new();
body.push_op1(|value: String| value.len(), false);
assert_eq!(unsafe { body.call1::<_, usize>(String::from("owned")) }?, 5);
Source

pub unsafe fn call2<A: 'static, B: 'static, T>(&self, arg: (A, B)) -> Result<T>
where T: 'static,

Executes all operations in the segment with two arguments of types A and B and returns the final result.

§Errors

Halts execution and returns an error if any operation returns an error.

§Safety

Operations must accept the initial source-ordered (A, B) and end in a live T with no preceding alignment gap. Operand types and padding must match every registered operation. Violating these requirements can cause undefined behavior.

  • Complexity: The execution and allocation costs of Self::call0.
§Examples

See Self::push_op2.

Trait Implementations§

Source§

impl Default for RawSegment

Source§

fn default() -> Self

Creates an empty segment.

Source§

impl Drop for RawSegment

Source§

fn drop(&mut self)

Releases instruction captures, preserving cleanup across destructor unwinding.

  • Complexity: O(n) plus destruction costs for n captures.
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

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.