Struct Program

Source
pub struct Program<'f, Fm: FnMarker = (), Rm: RuntimeMarker = ()> { /* private fields */ }
Expand description

Compiled CEL program ready for evaluation.

A Program represents a compiled CEL expression that can be evaluated multiple times with different variable bindings (activations). Programs are created by compiling CEL expressions using an Env.

§Type Parameters

  • 'f: Lifetime of functions registered in the environment
  • Fm: Function marker type indicating sync/async function support
  • Rm: Runtime marker type indicating the async runtime (if any)

§Examples

§Basic Usage

use cel_cxx::*;

let env = Env::builder()
    .declare_variable::<String>("name")?
    .build()?;
     
let program = env.compile("'Hello, ' + name")?;

let activation = Activation::new()
    .bind_variable("name", "World")?;
     
let result = program.evaluate(activation)?;

§Type Information

use cel_cxx::*;

let env = Env::builder().build()?;
let program = env.compile("42")?;

// Check the return type
println!("Return type: {:?}", program.return_type());

Implementations§

Source§

impl<'f, Fm: FnMarker, Rm: RuntimeMarker> Program<'f, Fm, Rm>

Source

pub fn return_type(&self) -> &ValueType

Returns the return type of this program.

This method returns the CEL type that this program will produce when evaluated. The type is determined during compilation based on the expression and the declared variables and functions.

§Returns

A reference to the ValueType that this program returns.

§Examples
use cel_cxx::*;

let env = Env::builder().build()?;
let program = env.compile("42")?;

println!("Return type: {:?}", program.return_type());
// Output: Return type: Int
Source

pub fn evaluate<'a, A, Afm>( &self, activation: A, ) -> <<Afm as FnMarkerAggr<Fm>>::Output as FnResult<'f, Result<Value, Error>>>::Output
where A: ActivationInterface<'f, Afm> + 'a, Afm: FnMarkerAggr<Fm>, <Afm as FnMarkerAggr<Fm>>::Output: FnResult<'f, Result<Value, Error>>, EvalDispatcher<<Afm as FnMarkerAggr<Fm>>::Output, Rm>: EvalDispatch<'f, A, Afm, Output = <<Afm as FnMarkerAggr<Fm>>::Output as FnResult<'f, Result<Value, Error>>>::Output>, 'f: 'a,

Evaluates the program with the given activation.

This method evaluates the compiled CEL expression using the variable and function bindings provided in the activation. The return type of this method depends on the program and activation markers:

  • For synchronous programs: Returns Result<Value, Error>
  • For asynchronous programs: Returns BoxFuture<Result<Value, Error>>
§Arguments
  • activation - The activation containing variable and function bindings
§Type Parameters
  • A - The activation type
  • Afm - The activation’s function marker type
§Examples
§Synchronous Evaluation
use cel_cxx::*;

let env = Env::builder()
    .declare_variable::<i64>("x")?
    .build()?;
     
let program = env.compile("x * 2")?;

let activation = Activation::new()
    .bind_variable("x", 21i64)?;
     
let result = program.evaluate(activation)?;
// result == Value::Int(42)
§With Empty Activation
use cel_cxx::*;

let env = Env::builder().build()?;
let program = env.compile("1 + 2 * 3")?;

let result = program.evaluate(())?;
// result == Value::Int(7)
Source§

impl<'f, Rm: RuntimeMarker> Program<'f, (), Rm>

Source

pub fn force_async(self) -> Program<'f, Async, Rm>

Available on crate feature async only.

Forces conversion to an async program.

This method converts a synchronous program to an asynchronous one, allowing it to work with async functions and evaluation.

§Examples
use cel_cxx::*;

let sync_program = Env::builder().build()?.compile("42")?;
let async_program = sync_program.force_async();
Source§

impl<'f, Fm: FnMarker> Program<'f, Fm, ()>

Source

pub fn use_runtime<Rt: Runtime>(self) -> Program<'f, Fm, Rt>

Available on crate feature async only.

Configures this program to use a specific async runtime.

This method allows you to specify which async runtime should be used for evaluating this program when it contains async functions.

§Type Parameters
  • Rt - The runtime type to use
§Examples
use cel_cxx::*;
use cel_cxx::r#async::Tokio;

let env = Env::builder().build()?;
let program = env.compile("42")?;

let async_program = program.use_runtime::<Tokio>();
Source

pub fn use_tokio(self) -> Program<'f, Fm, Tokio>

Available on crate features async and tokio only.

Configures this program to use the Tokio async runtime.

This is a convenience method equivalent to use_runtime::<Tokio>().

§Examples
use cel_cxx::*;

let env = Env::builder().build()?;
let program = env.compile("42")?;

let tokio_program = program.use_tokio();
Source

pub fn use_async_std(self) -> Program<'f, Fm, AsyncStd>

Available on crate features async and async-std only.

Configures this program to use the async-std runtime.

This is a convenience method equivalent to use_runtime::<AsyncStd>().

§Examples
use cel_cxx::*;

let env = Env::builder().build()?;
let program = env.compile("42")?;

let async_std_program = program.use_async_std();
Source

pub fn use_smol(self) -> Program<'f, Fm, Smol>

Available on crate features async and smol only.

Configures this program to use the smol runtime.

This is a convenience method equivalent to use_runtime::<Smol>().

§Examples
use cel_cxx::*;

let env = Env::builder().build()?;
let program = env.compile("42")?;

let smol_program = program.use_smol();

Trait Implementations§

Source§

impl<'f, Fm: FnMarker, Rm: RuntimeMarker> Clone for Program<'f, Fm, Rm>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'f, Fm: FnMarker, Rm: RuntimeMarker> Debug for Program<'f, Fm, Rm>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'f, Fm, Rm> Freeze for Program<'f, Fm, Rm>

§

impl<'f, Fm = (), Rm = ()> !RefUnwindSafe for Program<'f, Fm, Rm>

§

impl<'f, Fm, Rm> Send for Program<'f, Fm, Rm>
where Fm: Send, Rm: Send,

§

impl<'f, Fm, Rm> Sync for Program<'f, Fm, Rm>
where Fm: Sync, Rm: Sync,

§

impl<'f, Fm, Rm> Unpin for Program<'f, Fm, Rm>
where Fm: Unpin, Rm: Unpin,

§

impl<'f, Fm = (), Rm = ()> !UnwindSafe for Program<'f, Fm, Rm>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more