pub struct FunctionBindings<'f> { /* private fields */ }Expand description
Runtime function bindings.
FunctionBindings manages function implementations during CEL expression evaluation.
Unlike FunctionRegistry which is used at compile time, FunctionBindings provides
actual callable function implementations for runtime execution.
Each function name can have multiple overloads with different signatures, and each overload can be either a global function or a member function.
§Lifetime Parameters
'f: Lifetime of function implementations, allowing closures valid within specific scopes
§Examples
use cel_cxx::{FunctionBindings, Error};
let mut bindings = FunctionBindings::new();
// Bind global functions
fn add(a: i64, b: i64) -> Result<i64, Error> {
Ok(a + b)
}
bindings.bind_global("add", add)?;
// Bind member functions
fn string_length(s: String) -> Result<i64, Error> {
Ok(s.len() as i64)
}
bindings.bind_member("length", string_length)?;
// Bind closures
let multiplier = 10;
let closure = move |x: i64| -> Result<i64, Error> {
Ok(x * multiplier)
};
bindings.bind_global("multiply_by_ten", closure)?;Implementations§
Source§impl<'f> FunctionBindings<'f>
impl<'f> FunctionBindings<'f>
Source§impl<'f> FunctionBindings<'f>
impl<'f> FunctionBindings<'f>
Sourcepub fn bind<F, Fm, Args>(
&mut self,
name: impl Into<String>,
member: bool,
f: F,
) -> Result<&mut Self, Error>
pub fn bind<F, Fm, Args>( &mut self, name: impl Into<String>, member: bool, f: F, ) -> Result<&mut Self, Error>
Binds a function implementation with zero-annotation type inference.
Binds a callable function that can be invoked during CEL expression evaluation. The function signature is automatically inferred from the Rust function type, enabling seamless runtime function binding without manual type annotations.
§Zero-Annotation Features
- Automatic type inference: Function signature extracted from Rust function type
- Runtime binding: Functions are immediately available for CEL evaluation
- Lifetime handling: Safe conversion of borrowed arguments like
&str - Error conversion: Automatic conversion of
Result<T, E>return types - Async support: Seamless handling of both sync and async functions
§Type Parameters
F: Function type, must implementIntoFunctionFm: Function marker (sync or async), automatically inferredArgs: Argument tuple type, automatically inferred from function signature
§Parameters
name: Function namemember: Whether this is a member function (true) or global function (false)f: Function implementation
§Returns
Returns &mut Self to support method chaining
§Examples
§Basic function binding with automatic type inference
use cel_cxx::FunctionBindings;
let mut bindings = FunctionBindings::new();
// Zero-annotation function binding
bindings.bind("multiply", false, |a: i64, b: i64| a * b)?;
bindings.bind("upper", true, |s: String| s.to_uppercase())?;
bindings.bind("contains", true, |s: &str, substr: &str| s.contains(substr))?;§Runtime function binding with closures
use cel_cxx::FunctionBindings;
let mut bindings = FunctionBindings::new();
let multiplier = 10;
// Bind closure that captures environment
bindings.bind("scale", false, move |x: i64| x * multiplier)?;Sourcepub fn bind_member<F, Fm, Args>(
&mut self,
name: impl Into<String>,
f: F,
) -> Result<&mut Self, Error>
pub fn bind_member<F, Fm, Args>( &mut self, name: impl Into<String>, f: F, ) -> Result<&mut Self, Error>
Binds a member function implementation.
Convenience method for binding member functions that can be called as methods
on values (e.g., "hello".upper()).
§Examples
use cel_cxx::{FunctionBindings, Error};
let mut bindings = FunctionBindings::new();
fn reverse(s: String) -> Result<String, Error> {
Ok(s.chars().rev().collect())
}
bindings.bind_member("reverse", reverse)?;Sourcepub fn bind_global<F, Fm, Args>(
&mut self,
name: impl Into<String>,
f: F,
) -> Result<&mut Self, Error>
pub fn bind_global<F, Fm, Args>( &mut self, name: impl Into<String>, f: F, ) -> Result<&mut Self, Error>
Binds a global function implementation.
Convenience method for binding global functions that can be called from any context.
§Examples
use cel_cxx::{FunctionBindings, Error};
let mut bindings = FunctionBindings::new();
fn min(a: i64, b: i64) -> Result<i64, Error> {
Ok(a.min(b))
}
bindings.bind_global("min", min)?;Sourcepub fn entries(
&self,
) -> impl Iterator<Item = (&str, &FunctionOverloads<Function<'f>>)>
pub fn entries( &self, ) -> impl Iterator<Item = (&str, &FunctionOverloads<Function<'f>>)>
Returns an iterator over all function entries.
The iterator yields (name, overloads) pairs for all bound functions.
Sourcepub fn entries_mut(
&mut self,
) -> impl Iterator<Item = (&str, &mut FunctionOverloads<Function<'f>>)>
pub fn entries_mut( &mut self, ) -> impl Iterator<Item = (&str, &mut FunctionOverloads<Function<'f>>)>
Returns a mutable iterator over all function entries.
The iterator yields (name, overloads) pairs and allows modifying the overloads.
Trait Implementations§
Source§impl<'f> Debug for FunctionBindings<'f>
impl<'f> Debug for FunctionBindings<'f>
Source§impl<'f> Default for FunctionBindings<'f>
impl<'f> Default for FunctionBindings<'f>
Source§fn default() -> FunctionBindings<'f>
fn default() -> FunctionBindings<'f>
Auto Trait Implementations§
impl<'f> Freeze for FunctionBindings<'f>
impl<'f> !RefUnwindSafe for FunctionBindings<'f>
impl<'f> Send for FunctionBindings<'f>
impl<'f> Sync for FunctionBindings<'f>
impl<'f> Unpin for FunctionBindings<'f>
impl<'f> !UnwindSafe for FunctionBindings<'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