Struct RawSegment
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§
§impl RawSegment
impl RawSegment
pub fn new() -> RawSegment
pub fn new() -> RawSegment
pub fn raw0_<F>(&mut self, op: F)
pub fn raw0_<F>(&mut self, op: F)
Pushes a closure that manipulates the stack directly and returns ().
Operation errors propagate at execution after frame cleanup.
- Complexity: The capture insertion cost of
Self::push_op0.
§Examples
use cel_runtime::RawSegment;
let mut body = RawSegment::new();
body.raw0_(|stack| { stack.push(7u8); Ok(()) });
assert_eq!(unsafe { body.call0::<u8>() }?, 7);pub fn raw0<R, F>(&mut self, op: F)
pub fn raw0<R, F>(&mut self, op: F)
Pushes a fallible closure that manipulates the remaining stack and returns R.
Operation errors propagate at execution after frame cleanup.
- Complexity: The capture insertion cost of
Self::push_op0.
§Examples
See RawSegment.
pub fn push_op0<R, F>(&mut self, op: F)where
F: Fn() -> R + 'static,
R: 'static,
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 ofF) capture storage; raising capture alignment additionally moves all stored capture bytes.
§Examples
See RawSegment.
pub fn push_op1<T, R, F>(&mut self, op: F, padding0: bool)where
F: Fn(T) -> R + 'static,
T: 'static,
R: 'static,
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.
- Complexity: The capture insertion cost of
Self::push_op0.
§Examples
See RawSegment.
pub fn raw1<T, R, F>(&mut self, op: F, padding0: bool)
pub fn raw1<T, R, F>(&mut self, op: F, padding0: bool)
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.
- Complexity: The capture insertion cost of
Self::push_op0.
§Examples
See RawSegment.
pub fn drop1<T, F>(&mut self, op: F, padding0: bool)where
F: Fn(T) + 'static,
T: 'static,
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.
- Complexity: The capture insertion cost of
Self::push_op0.
§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>() }?);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,
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.
- Complexity: The capture insertion cost of
Self::push_op0.
§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));pub fn raw2<T, U, R, F>(&mut self, op: F, padding0: bool, padding1: bool)
pub fn raw2<T, U, R, F>(&mut self, op: F, padding0: bool, padding1: bool)
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.
- Complexity: The capture insertion cost of
Self::push_op0.
§Examples
See RawSegment.
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,
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.
- Complexity: The capture insertion cost of
Self::push_op0.
§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));pub fn raw3<T, U, V, R, F>(
&mut self,
op: F,
padding0: bool,
padding1: bool,
padding2: bool,
)
pub fn raw3<T, U, V, R, F>( &mut self, op: F, padding0: bool, padding1: bool, padding2: bool, )
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.
- Complexity: The capture insertion cost of
Self::push_op0.
§Examples
See RawSegment.
pub unsafe fn call0_stack(&self, stack: &mut RawStack) -> Result<(), Error>
pub unsafe fn call0_stack(&self, stack: &mut RawStack) -> Result<(), Error>
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");pub unsafe fn call0<T>(&self) -> Result<T, Error>where
T: 'static,
pub unsafe fn call0<T>(&self) -> Result<T, Error>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.
pub unsafe fn call1<A, T>(&self, arg: A) -> Result<T, Error>where
A: 'static,
T: 'static,
pub unsafe fn call1<A, T>(&self, arg: A) -> Result<T, Error>where
A: 'static,
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);pub unsafe fn call2<A, B, T>(&self, arg: (A, B)) -> Result<T, Error>where
A: 'static,
B: 'static,
T: 'static,
pub unsafe fn call2<A, B, T>(&self, arg: (A, B)) -> Result<T, Error>where
A: 'static,
B: 'static,
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§
§impl Default for RawSegment
impl Default for RawSegment
§fn default() -> RawSegment
fn default() -> RawSegment
Creates an empty segment.