pub struct VariableBindings<'f> { /* private fields */ }Expand description
Runtime variable bindings.
VariableBindings manages variable bindings during CEL expression evaluation.
It maps variable names to concrete values or value providers.
Unlike compile-time VariableRegistry, VariableBindings provides actual variable values
at runtime and supports two types of bindings:
- Value bindings: Directly stored variable values
- Provider bindings: Function-computed variable values through lazy evaluation
§Lifetime Parameters
'f: Lifetime of function providers, allowing closures valid within specific scopes
§Examples
use cel_cxx::VariableBindings;
let mut bindings = VariableBindings::new();
// Bind static values
bindings.bind("name", "Alice")?;
bindings.bind("age", 30i64)?;
// Bind dynamic value providers
bindings.bind_provider("current_time", || {
std::time::SystemTime::now()
})?;
assert_eq!(bindings.len(), 3);Implementations§
Source§impl<'f> VariableBindings<'f>
impl<'f> VariableBindings<'f>
Source§impl<'f> VariableBindings<'f>
impl<'f> VariableBindings<'f>
Sourcepub fn bind<T>(
&mut self,
name: impl Into<String>,
value: T,
) -> Result<&mut Self, Error>where
T: IntoValue + TypedValue,
pub fn bind<T>(
&mut self,
name: impl Into<String>,
value: T,
) -> Result<&mut Self, Error>where
T: IntoValue + TypedValue,
Binds a variable value.
Binds a variable name to a concrete value. The value must implement IntoValue and TypedValue traits.
§Type Parameters
T: Value type, must implementIntoValue + TypedValue
§Parameters
name: Variable namevalue: Variable value
§Returns
Returns &mut Self to support method chaining
§Examples
use cel_cxx::VariableBindings;
let mut bindings = VariableBindings::new();
bindings
.bind("user_id", 123i64)?
.bind("is_admin", true)?
.bind("username", "alice")?;Sourcepub fn bind_with_value(
&mut self,
name: impl Into<String>,
value_type: ValueType,
value: Value,
) -> Result<&mut Self, Error>
pub fn bind_with_value( &mut self, name: impl Into<String>, value_type: ValueType, value: Value, ) -> Result<&mut Self, Error>
Binds a variable with an explicit type and pre-converted value.
Unlike bind which infers the type from the value, this method
takes the type and value separately. This is useful for types like protobuf
messages where the concrete type name is not known at compile time.
Sourcepub fn bind_provider<F, Fm>(
&mut self,
name: impl Into<String>,
provider: F,
) -> Result<&mut Self, Error>where
F: IntoFunction<'f, Fm>,
Fm: FnMarker,
pub fn bind_provider<F, Fm>(
&mut self,
name: impl Into<String>,
provider: F,
) -> Result<&mut Self, Error>where
F: IntoFunction<'f, Fm>,
Fm: FnMarker,
Binds a variable to a value provider.
Binds a variable name to a provider function that computes the value dynamically. This enables lazy evaluation and allows variables to have values computed at runtime.
§Type Parameters
F: Provider function type, must implementIntoFunctionFm: Function marker type (sync/async)
§Parameters
name: Variable nameprovider: Provider function that computes the variable value
§Returns
Returns &mut Self to support method chaining
§Examples
use cel_cxx::VariableBindings;
let mut bindings = VariableBindings::new();
bindings.bind_provider("current_time", || -> i64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs() as i64
})?;Sourcepub fn find(&self, name: &str) -> Option<&VariableBinding<'f>>
pub fn find(&self, name: &str) -> Option<&VariableBinding<'f>>
Sourcepub fn find_mut(&mut self, name: &str) -> Option<&mut VariableBinding<'f>>
pub fn find_mut(&mut self, name: &str) -> Option<&mut VariableBinding<'f>>
Sourcepub fn entries(&self) -> impl Iterator<Item = (&str, &VariableBinding<'f>)>
pub fn entries(&self) -> impl Iterator<Item = (&str, &VariableBinding<'f>)>
Returns an iterator over all variable bindings.
The iterator yields (name, binding) pairs for all bound variables.
§Returns
Iterator yielding (&str, &VariableBinding) pairs
Sourcepub fn entries_mut(
&mut self,
) -> impl Iterator<Item = (&str, &mut VariableBinding<'f>)>
pub fn entries_mut( &mut self, ) -> impl Iterator<Item = (&str, &mut VariableBinding<'f>)>
Returns a mutable iterator over all variable bindings.
The iterator yields (name, binding) pairs and allows modifying the bindings.
§Returns
Iterator yielding (&str, &mut VariableBinding) pairs
Trait Implementations§
Source§impl<'f> Debug for VariableBindings<'f>
impl<'f> Debug for VariableBindings<'f>
Source§impl<'f> Default for VariableBindings<'f>
impl<'f> Default for VariableBindings<'f>
Source§fn default() -> VariableBindings<'f>
fn default() -> VariableBindings<'f>
Auto Trait Implementations§
impl<'f> !RefUnwindSafe for VariableBindings<'f>
impl<'f> !UnwindSafe for VariableBindings<'f>
impl<'f> Freeze for VariableBindings<'f>
impl<'f> Send for VariableBindings<'f>
impl<'f> Sync for VariableBindings<'f>
impl<'f> Unpin for VariableBindings<'f>
impl<'f> UnsafeUnpin for VariableBindings<'f>
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> 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