Skip to main content

Segment

Struct Segment 

Source
pub struct Segment<Args: IntoList + 'static, Stack: List = ReverseList<<Args as IntoList>::Output<CNil<()>>>> { /* 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 implement IntoList
  • Stack: The type stack tracking operation results, defaults to the reverse of Args

§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§

Source§

impl<Args: IntoList + 'static> Segment<Args>

Source

pub fn new() -> Segment<Args>

Creates a new empty segment with no operations.

§Examples
use cel_runtime::{Segment, SegmentCallable};
assert_eq!(Segment::<(String,)>::new().call((String::from("owned"),))?, "owned");
Source§

impl<Args: IntoList + 'static, Stack> Segment<Args, Stack>
where Stack: StackResult + 'static, Stack::Tail: CStackListHeadLimit,

Source

pub fn op0<R, F>(self, op: F) -> Segment<Args, Stack::Push<R>>
where F: Fn() -> R + 'static, R: 'static,

Pushes a nullary operation that takes no arguments and returns a value of type R.

  • Complexity: Amortized O(size of F + alignment of F) capture storage; raising capture alignment also relocates the existing stored capture bytes.
§Examples

See Segment.

Source

pub fn op0r<R, F>(self, op: F) -> Segment<Args, Stack::Push<R>>
where F: Fn() -> Result<R> + 'static, R: 'static,

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

pub fn op1<R, F>(self, op: F) -> Segment<Args, CStackList<R, Stack::Tail>>
where Stack: CStackListHeadPadded, F: Fn(Stack::Head) -> R + 'static, R: 'static,

Pushes a unary operation that takes the current stack value and returns a new value.

  • Complexity: The capture-storage insertion cost of Self::op0.
§Examples

See Segment.

Source

pub fn op1r<R, F>(self, op: F) -> Segment<Args, CStackList<R, Stack::Tail>>
where Stack: CStackListHeadPadded, F: Fn(Stack::Head) -> Result<R> + 'static, R: 'static,

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

pub fn op2<R, F>( self, op: F, ) -> Segment<Args, <<Stack::Tail as List>::Tail as List>::Push<R>>
where Stack: CStackListHeadPadded, Stack::Tail: CStackListHeadPadded, F: Fn(<Stack::Tail as List>::Head, Stack::Head) -> R + 'static, R: 'static,

Pushes a binary operation that takes the top two stack values and returns a new value.

The earlier source operand becomes the callback’s first argument.

  • Complexity: The capture-storage insertion cost of Self::op0.
§Examples

See Segment.

Trait Implementations§

Source§

impl<Args: IntoList + 'static> Default for Segment<Args>

Source§

fn default() -> Self

Constructs an instruction-free segment with the declared initial argument stack.

Source§

impl<T: StackResult + 'static> SegmentCallable<()> for Segment<(), T>

Source§

fn call(&self, _args: ()) -> Self::Output

Executes the zero-argument body and returns its single result.

§Errors

Propagates body errors after frame cleanup.

Source§

type Output = Result<<T as List>::Head, Error>

The result type produced by calling with Args.
Source§

impl<T: StackResult + 'static, A: 'static, B: 'static> SegmentCallable<(A, B)> for Segment<(A, B), T>

Source§

fn call(&self, args: (A, B)) -> Self::Output

Consumes source-ordered arguments and executes the body to its single result.

§Errors

Propagates body errors after frame cleanup.

Source§

type Output = Result<<T as List>::Head, Error>

The result type produced by calling with Args.
Source§

impl<T: StackResult + 'static, A: 'static> SegmentCallable<(A,)> for Segment<(A,), T>

Source§

fn call(&self, args: (A,)) -> Self::Output

Consumes one argument and executes the body to its single result.

§Errors

Propagates body errors after frame cleanup.

Source§

type Output = Result<<T as List>::Head, Error>

The result type produced by calling with Args.
Source§

impl<Args: IntoList + 'static, Stack> TryFrom<DynSegment> for Segment<Args, Stack>

Source§

fn try_from(value: DynSegment) -> Result<Self, Self::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.
Source§

type Error = Error

The type returned in the event of a conversion error.

Auto Trait Implementations§

§

impl<Args, Stack = <<Args as IntoList>::Output<CNil<()>> as List>::ReverseOnto<<<Args as IntoList>::Output<CNil<()>> as List>::Empty>> !Send for Segment<Args, Stack>

§

impl<Args, Stack = <<Args as IntoList>::Output<CNil<()>> as List>::ReverseOnto<<<Args as IntoList>::Output<CNil<()>> as List>::Empty>> !Sync for Segment<Args, Stack>

§

impl<Args, Stack> Freeze for Segment<Args, Stack>

§

impl<Args, Stack> RefUnwindSafe for Segment<Args, Stack>
where Args: RefUnwindSafe, Stack: RefUnwindSafe,

§

impl<Args, Stack> Unpin for Segment<Args, Stack>
where Args: Unpin, Stack: Unpin,

§

impl<Args, Stack> UnsafeUnpin for Segment<Args, Stack>

§

impl<Args, Stack> UnwindSafe for Segment<Args, Stack>
where Args: UnwindSafe, Stack: UnwindSafe,

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.