Skip to main content

Segment

Struct Segment 

Source
pub struct Segment<Args, Stack = <<Args as IntoList>::Output<CNil<()>> as List>::ReverseOnto<<<Args as IntoList>::Output<CNil<()>> as List>::Empty>>
where Args: IntoList + 'static, Stack: List,
{ /* 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::<(i32,)>::new() // create a new segment that takes an i32 argument
    .op1(|x| x * 2)                 // push a unary operation that multiplies the argument by 2
    .op0(|| 10)                     // 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, )).unwrap(),         // call the segment with an argument 42
    "94");                          // the result is (42 * 2 + 10).to_string()

Implementations§

Source§

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

Source

pub fn new() -> Segment<Args>

Creates a new empty segment with no operations.

Source§

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

Source

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

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

Source

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

Pushes a fallible nullary operation that returns Result<R>.

Source

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

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

Source

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

Pushes a fallible unary operation that takes the current stack value and returns Result<R>.

Source

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

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

Trait Implementations§

Source§

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

Source§

fn default() -> Segment<Args>

Returns the “default value” for a type. Read more
Source§

impl<T> SegmentCallable<()> for Segment<(), T>
where T: StackResult + 'static, <T as List>::Tail: EmptyList + CStackListHeadLimit,

Source§

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

The result type produced by calling with Args.
Source§

fn call(&self, _args: ()) -> <Segment<(), T> as SegmentCallable<()>>::Output

Invokes the segment with the supplied arguments.
Source§

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,

Source§

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

The result type produced by calling with Args.
Source§

fn call( &self, args: (A, B), ) -> <Segment<(A, B), T> as SegmentCallable<(A, B)>>::Output

Invokes the segment with the supplied arguments.
Source§

impl<T, A> SegmentCallable<(A,)> for Segment<(A,), T>
where T: StackResult + 'static, A: 'static, <T as List>::Tail: EmptyList + CStackListHeadLimit,

Source§

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

The result type produced by calling with Args.
Source§

fn call( &self, args: (A,), ) -> <Segment<(A,), T> as SegmentCallable<(A,)>>::Output

Invokes the segment with the supplied arguments.
Source§

impl<Args, Stack> TryFrom<DynSegment> for Segment<Args, Stack>
where Args: IntoList + 'static, Stack: List + 'static + ListTypeIteratorAdvance<TypeId>, <<Args as IntoList>::Output<CNil<()>> as List>::ReverseOnto<<<Args as IntoList>::Output<CNil<()>> as List>::Empty>: ListTypeIteratorAdvance<TypeId>,

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from( value: DynSegment, ) -> Result<Segment<Args, Stack>, <Segment<Args, Stack> as TryFrom<DynSegment>>::Error>

Performs the conversion.

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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 = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.