Struct Env

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

CEL expression evaluation environment.

The Env struct represents a CEL environment that can compile expressions into programs. It encapsulates function registries, variable declarations, and type information needed for expression compilation.

§Type Parameters

  • 'f: Lifetime of functions registered in this 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")?;

§With Custom Functions

use cel_cxx::*;

let env = Env::builder()
    .register_global_function("add", |x: i64, y: i64| -> i64 { x + y })?
    .build()?;
     
let program = env.compile("add(10, 20)")?;

Implementations§

Source§

impl<'f> Env<'f>

Source

pub fn builder() -> EnvBuilder<'f, ()>

Creates a new environment builder.

This is the starting point for creating a CEL environment. The builder allows you to register functions, declare variables, and configure the environment before building it.

§Examples
use cel_cxx::*;

let builder = Env::builder();
Source§

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

Source

pub fn compile<S: AsRef<[u8]>>( &self, source: S, ) -> Result<Program<'f, Fm, Rm>, Error>

Compiles a CEL expression into a Program.

This method takes a CEL expression as a string or byte slice and compiles it into a Program that can be evaluated with different activations.

§Arguments
  • source - The CEL expression to compile
§Returns

Returns a Result containing the compiled Program or an Error if compilation fails.

§Examples
use cel_cxx::*;

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

Returns an error if:

  • The expression contains syntax errors
  • Referenced functions or variables are not declared
  • Type checking fails
Source§

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

Source

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

Available on crate feature async only.

Forces conversion to an async environment.

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

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

let sync_env = Env::builder().build()?;
let async_env = sync_env.force_async();
Source§

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

Source

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

Available on crate feature async only.

Sets the async runtime for this environment.

This method specifies which async runtime should be used for asynchronous evaluation of expressions.

§Type Parameters
  • Rt - The runtime type to use (must implement Runtime)
§Examples
use cel_cxx::*;

let env = Env::builder()
    .build()?
    .use_runtime::<Tokio>();
Source

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

Available on crate features async and tokio only.

Configures the environment to use the Tokio async runtime.

This is a convenience method for setting the runtime to Tokio. Requires the tokio feature to be enabled.

§Examples
use cel_cxx::*;

let env = Env::builder()
    .build()?
    .use_tokio();
Source

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

Available on crate features async and async-std only.

Configures the environment to use the async-std runtime.

This is a convenience method for setting the runtime to async-std. Requires the async-std feature to be enabled.

§Examples
use cel_cxx::*;

let env = Env::builder()
    .build()?
    .use_async_std();
Source

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

Available on crate features async and smol only.

Configures the environment to use the smol runtime.

This is a convenience method for setting the runtime to smol. Requires the smol feature to be enabled.

§Examples
use cel_cxx::*;

let env = Env::builder().use_smol();

Trait Implementations§

Source§

impl<'f, Fm: FnMarker, Rm: RuntimeMarker> Clone for Env<'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 Env<'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 Env<'f, Fm, Rm>

§

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

§

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

§

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

§

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

§

impl<'f, Fm = (), Rm = ()> !UnwindSafe for Env<'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