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 environmentFm
: Function marker type indicating sync/async function supportRm
: 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>
impl<'f, Fm: FnMarker, Rm: RuntimeMarker> Program<'f, Fm, Rm>
Sourcepub fn return_type(&self) -> &ValueType
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
Sourcepub fn evaluate<'a, A, Afm>(
&self,
activation: A,
) -> <<Afm as FnMarkerAggr<Fm>>::Output as FnResult<'f, Result<Value, Error>>>::Outputwhere
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,
pub fn evaluate<'a, A, Afm>(
&self,
activation: A,
) -> <<Afm as FnMarkerAggr<Fm>>::Output as FnResult<'f, Result<Value, Error>>>::Outputwhere
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 typeAfm
- 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>
impl<'f, Rm: RuntimeMarker> Program<'f, (), Rm>
Sourcepub fn force_async(self) -> Program<'f, Async, Rm>
Available on crate feature async
only.
pub fn force_async(self) -> Program<'f, Async, Rm>
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, ()>
impl<'f, Fm: FnMarker> Program<'f, Fm, ()>
Sourcepub fn use_runtime<Rt: Runtime>(self) -> Program<'f, Fm, Rt>
Available on crate feature async
only.
pub fn use_runtime<Rt: Runtime>(self) -> Program<'f, Fm, Rt>
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>();
Sourcepub fn use_tokio(self) -> Program<'f, Fm, Tokio>
Available on crate features async
and tokio
only.
pub fn use_tokio(self) -> Program<'f, Fm, Tokio>
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();
Sourcepub fn use_async_std(self) -> Program<'f, Fm, AsyncStd>
Available on crate features async
and async-std
only.
pub fn use_async_std(self) -> Program<'f, Fm, AsyncStd>
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();
Sourcepub fn use_smol(self) -> Program<'f, Fm, Smol>
Available on crate features async
and smol
only.
pub fn use_smol(self) -> Program<'f, Fm, Smol>
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§
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>
impl<'f, Fm, Rm> Sync for Program<'f, Fm, Rm>
impl<'f, Fm, Rm> Unpin for Program<'f, Fm, Rm>
impl<'f, Fm = (), Rm = ()> !UnwindSafe for Program<'f, Fm, Rm>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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