Struct Segment
pub struct Segment<Args, Stack = <<Args as IntoList>::Output<CNil<()>> as List>::ReverseOnto<<<Args as IntoList>::Output<CNil<()>> as List>::Empty>>{ /* private fields */ }Expand description
A type-safe segment that represents a sequence of operations.
The segment takes input arguments of type Args and maintains a type stack Stack that tracks
the types of values produced by operations.
§Type Parameters
Args: The input argument types, must implementIntoListStack: The type stack tracking operation results, defaults to the reverse ofArgs
§Examples
use cel_runtime::segment::*;
assert_eq!(Segment::<(f64,)>::new() // create a new segment that takes an f64 argument
.op1(|x| x * 2.0) // push a unary operation that doubles the argument
.op0(|| 10.0) // push a nullary operation that returns 10
.op2(|x, y| x + y) // push a binary operation that adds two arguments
.op1(|x| x.to_string()) // push a unary operation returning a string of the argument
.call((42.0, )).unwrap(), // call the segment with an argument 42
"94"); // the result is (42 * 2 + 10).to_string()§Thread confinement
Captures need not implement Send or Sync; the erased program remains
thread-confined even when its argument and result types are thread-safe.
use cel_runtime::{Segment, SegmentCallable};
let captured = std::rc::Rc::new(7usize);
let body = Segment::<()>::new().op0(move || *captured);
std::thread::spawn(move || body.call(()));Implementations§
§impl<Args, Stack> Segment<Args, Stack>where
Args: IntoList + 'static,
Stack: StackResult + 'static,
<Stack as List>::Tail: CStackListHeadLimit,
impl<Args, Stack> Segment<Args, Stack>where
Args: IntoList + 'static,
Stack: StackResult + 'static,
<Stack as List>::Tail: CStackListHeadLimit,
pub fn op0<R, F>(self, op: F) -> Segment<Args, <Stack as List>::Push<R>>where
F: Fn() -> R + 'static,
R: 'static,
pub fn op0<R, F>(self, op: F) -> Segment<Args, <Stack as List>::Push<R>>where
F: Fn() -> R + 'static,
R: 'static,
pub fn op0r<R, F>(self, op: F) -> Segment<Args, <Stack as List>::Push<R>>
pub fn op0r<R, F>(self, op: F) -> Segment<Args, <Stack as List>::Push<R>>
Pushes a fallible nullary operation that returns Result<R>.
Execution returns the operation’s error through SegmentCallable::call.
- Complexity: The capture-storage insertion cost of
Self::op0.
§Examples
use cel_runtime::{Segment, SegmentCallable};
let body = Segment::<()>::new().op0r(|| Ok(String::from("owned")));
assert_eq!(body.call(())?, "owned");pub fn op1<R, F>(
self,
op: F,
) -> Segment<Args, CStackList<R, <Stack as List>::Tail>>
pub fn op1<R, F>( self, op: F, ) -> Segment<Args, CStackList<R, <Stack as List>::Tail>>
pub fn op1r<R, F>(
self,
op: F,
) -> Segment<Args, CStackList<R, <Stack as List>::Tail>>
pub fn op1r<R, F>( self, op: F, ) -> Segment<Args, CStackList<R, <Stack as List>::Tail>>
Pushes a fallible unary operation that takes the current stack value and returns Result<R>.
Execution returns the operation’s error through SegmentCallable::call.
- Complexity: The capture-storage insertion cost of
Self::op0.
§Examples
use cel_runtime::{Segment, SegmentCallable};
let body = Segment::<(i32,)>::new().op1r(|value| {
value.checked_mul(2).ok_or_else(|| anyhow::anyhow!("overflow"))
});
assert_eq!(body.call((21,))?, 42);
assert!(body.call((i32::MAX,)).is_err());Trait Implementations§
§impl<T> SegmentCallable<()> for Segment<(), T>
impl<T> SegmentCallable<()> for Segment<(), T>
§fn call(&self, _args: ()) -> <Segment<(), T> as SegmentCallable<()>>::Output
fn call(&self, _args: ()) -> <Segment<(), T> as SegmentCallable<()>>::Output
Executes the zero-argument body and returns its single result.
§Errors
Propagates body errors after frame cleanup.
- Complexity: The execution and storage costs of
SegmentCallable::call.
§impl<T, A, B> SegmentCallable<(A, B)> for Segment<(A, B), T>where
T: StackResult + 'static,
A: 'static,
B: 'static,
<T as List>::Tail: EmptyList + CStackListHeadLimit,
impl<T, A, B> SegmentCallable<(A, B)> for Segment<(A, B), T>where
T: StackResult + 'static,
A: 'static,
B: 'static,
<T as List>::Tail: EmptyList + CStackListHeadLimit,
§fn call(
&self,
args: (A, B),
) -> <Segment<(A, B), T> as SegmentCallable<(A, B)>>::Output
fn call( &self, args: (A, B), ) -> <Segment<(A, B), T> as SegmentCallable<(A, B)>>::Output
Consumes source-ordered arguments and executes the body to its single result.
§Errors
Propagates body errors after frame cleanup.
- Complexity: The execution and storage costs of
SegmentCallable::call.
§impl<T, A> SegmentCallable<(A,)> for Segment<(A,), T>
impl<T, A> SegmentCallable<(A,)> for Segment<(A,), T>
§fn call(
&self,
args: (A,),
) -> <Segment<(A,), T> as SegmentCallable<(A,)>>::Output
fn call( &self, args: (A,), ) -> <Segment<(A,), T> as SegmentCallable<(A,)>>::Output
Consumes one argument and executes the body to its single result.
§Errors
Propagates body errors after frame cleanup.
- Complexity: The execution and storage costs of
SegmentCallable::call.
§impl<Args, Stack> TryFrom<DynSegment> for Segment<Args, Stack>
impl<Args, Stack> TryFrom<DynSegment> for Segment<Args, Stack>
§fn try_from(
value: DynSegment,
) -> Result<Segment<Args, Stack>, <Segment<Args, Stack> as TryFrom<DynSegment>>::Error>
fn try_from( value: DynSegment, ) -> Result<Segment<Args, Stack>, <Segment<Args, Stack> as TryFrom<DynSegment>>::Error>
Binds a dynamic preloaded opaque-leaf frame to its exact static signature.
§Errors
Rejects different argument/result types, non-opaque descriptors, borrowed positional reads, and nonzero ambient stack origins.
- Complexity: O(a + s) metadata comparisons for a arguments and s result fields.