pub struct FunctionRegistry<'f> { /* private fields */ }Expand description
Compile-time function registry.
FunctionRegistry manages function declarations and implementations during the CEL environment
compilation phase. It maintains a mapping from function names to function overloads, where each
overload set can contain multiple function implementations with different signatures.
The registry supports two types of functions:
- Function declarations: Type signatures only for compile-time checking
- Function implementations: Callable code for runtime execution
§Function Types
Functions can be registered as either:
- Global functions: Callable from any context
- Member functions: Called as methods on values (e.g.,
value.method())
§Lifetime Parameters
'f: Lifetime of function implementations, allowing closures valid within specific scopes
§Examples
use cel_cxx::{FunctionRegistry, Error};
let mut registry = FunctionRegistry::new();
// Register function implementations
fn add(a: i64, b: i64) -> Result<i64, Error> {
Ok(a + b)
}
registry.register_global("add", add)?;
// Register member functions
fn string_length(s: String) -> Result<i64, Error> {
Ok(s.len() as i64)
}
registry.register_member("length", string_length)?;
// Register function declarations (type signatures only)
registry.declare_global::<fn(String) -> Result<bool, Error>>("is_valid")?;
assert_eq!(registry.len(), 3);Implementations§
Source§impl<'f> FunctionRegistry<'f>
impl<'f> FunctionRegistry<'f>
Source§impl<'f> FunctionRegistry<'f>
impl<'f> FunctionRegistry<'f>
Sourcepub fn register<F, Fm, Args>(
&mut self,
name: impl Into<String>,
member: bool,
f: F,
) -> Result<&mut Self, Error>
pub fn register<F, Fm, Args>( &mut self, name: impl Into<String>, member: bool, f: F, ) -> Result<&mut Self, Error>
Registers a function implementation with zero-annotation type inference.
Registers a callable function that can be invoked during CEL expression evaluation. The function signature is automatically inferred from the Rust function type, eliminating the need for manual type annotations.
§Zero-Annotation Features
- Automatic type inference: Function signature extracted from Rust function type
- 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 functions with automatic type inference
use cel_cxx::FunctionRegistry;
let mut registry = FunctionRegistry::new();
// Zero-annotation function registration
registry.register("add", false, |a: i64, b: i64| a + b)?;
registry.register("greet", false, |name: &str| format!("Hello, {}!", name))?;
registry.register("is_empty", true, |s: String| s.is_empty())?;§Error handling with automatic conversion
use cel_cxx::{FunctionRegistry, Error};
let mut registry = FunctionRegistry::new();
// Functions returning Result are automatically handled
registry.register("divide", false, |a: i64, b: i64| -> Result<i64, Error> {
if b == 0 {
Err(Error::invalid_argument("Division by zero"))
} else {
Ok(a / b)
}
})?;Sourcepub fn register_member<F, Fm, Args>(
&mut self,
name: impl Into<String>,
f: F,
) -> Result<&mut Self, Error>
pub fn register_member<F, Fm, Args>( &mut self, name: impl Into<String>, f: F, ) -> Result<&mut Self, Error>
Registers a member function implementation.
Convenience method for registering member functions that can be called as methods
on values (e.g., "hello".length()).
§Examples
use cel_cxx::{FunctionRegistry, Error};
let mut registry = FunctionRegistry::new();
fn to_upper(s: String) -> Result<String, Error> {
Ok(s.to_uppercase())
}
registry.register_member("upper", to_upper)?;Sourcepub fn register_global<F, Fm, Args>(
&mut self,
name: impl Into<String>,
f: F,
) -> Result<&mut Self, Error>
pub fn register_global<F, Fm, Args>( &mut self, name: impl Into<String>, f: F, ) -> Result<&mut Self, Error>
Registers a global function implementation.
Convenience method for registering global functions that can be called from any context.
§Examples
use cel_cxx::{FunctionRegistry, Error};
let mut registry = FunctionRegistry::new();
fn max(a: i64, b: i64) -> Result<i64, Error> {
Ok(a.max(b))
}
registry.register_global("max", max)?;Sourcepub fn declare<D>(
&mut self,
name: impl Into<String>,
member: bool,
) -> Result<&mut Self, Error>where
D: FunctionDecl,
pub fn declare<D>(
&mut self,
name: impl Into<String>,
member: bool,
) -> Result<&mut Self, Error>where
D: FunctionDecl,
Declares a function signature.
Declares a function type signature for compile-time type checking without providing
an implementation. The function signature is determined by the generic parameter D
which must implement FunctionDecl.
§Type Parameters
D: Function declaration type, must implementFunctionDecl
§Parameters
name: Function namemember: Whether this is a member function (true) or global function (false)
§Returns
Returns &mut Self to support method chaining
§Examples
use cel_cxx::{FunctionRegistry, Error};
let mut registry = FunctionRegistry::new();
// Declare global function signature
registry.declare::<fn(String) -> Result<bool, Error>>("validate", false)?;
// Declare member function signature
registry.declare::<fn(String) -> Result<i64, Error>>("size", true)?;Sourcepub fn declare_member<D>(
&mut self,
name: impl Into<String>,
) -> Result<&mut Self, Error>where
D: FunctionDecl + FunctionDeclWithNonEmptyArguments,
pub fn declare_member<D>(
&mut self,
name: impl Into<String>,
) -> Result<&mut Self, Error>where
D: FunctionDecl + FunctionDeclWithNonEmptyArguments,
Declares a member function signature.
Convenience method for declaring member function signatures for compile-time type checking without providing implementations.
§Type Parameters
D: Function declaration type, must implementFunctionDeclandFunctionDeclWithNonEmptyArguments
§Parameters
name: Function name
§Returns
Returns &mut Self to support method chaining
§Examples
use cel_cxx::{FunctionRegistry, Error};
let mut registry = FunctionRegistry::new();
// Declare member function signature
registry.declare_member::<fn(String) -> i64>("hash")?;Sourcepub fn declare_global<D>(
&mut self,
name: impl Into<String>,
) -> Result<&mut Self, Error>where
D: FunctionDecl,
pub fn declare_global<D>(
&mut self,
name: impl Into<String>,
) -> Result<&mut Self, Error>where
D: FunctionDecl,
Declares a global function signature.
Convenience method for declaring global function signatures for compile-time type checking without providing implementations.
§Type Parameters
D: Function declaration type, must implementFunctionDecl
§Parameters
name: Function name
§Returns
Returns &mut Self to support method chaining
§Examples
use cel_cxx::{FunctionRegistry, Error};
let mut registry = FunctionRegistry::new();
// Declare global function signature
registry.declare_global::<fn(String, String) -> Result<String, Error>>("concat")?;Sourcepub fn find(
&self,
name: &str,
) -> Option<&FunctionOverloads<FunctionDeclOrImpl<'f>>>
pub fn find( &self, name: &str, ) -> Option<&FunctionOverloads<FunctionDeclOrImpl<'f>>>
Sourcepub fn find_mut(
&mut self,
name: &str,
) -> Option<&mut FunctionOverloads<FunctionDeclOrImpl<'f>>>
pub fn find_mut( &mut self, name: &str, ) -> Option<&mut FunctionOverloads<FunctionDeclOrImpl<'f>>>
Sourcepub fn entries(
&self,
) -> impl Iterator<Item = (&str, &FunctionOverloads<FunctionDeclOrImpl<'f>>)>
pub fn entries( &self, ) -> impl Iterator<Item = (&str, &FunctionOverloads<FunctionDeclOrImpl<'f>>)>
Returns an iterator over all function entries.
§Returns
Iterator yielding (&str, &FunctionOverloads) pairs
Sourcepub fn entries_mut(
&mut self,
) -> impl Iterator<Item = (&str, &mut FunctionOverloads<FunctionDeclOrImpl<'f>>)>
pub fn entries_mut( &mut self, ) -> impl Iterator<Item = (&str, &mut FunctionOverloads<FunctionDeclOrImpl<'f>>)>
Returns a mutable iterator over all function entries.
§Returns
Iterator yielding (&str, &mut FunctionOverloads) pairs
Trait Implementations§
Source§impl<'f> Debug for FunctionRegistry<'f>
impl<'f> Debug for FunctionRegistry<'f>
Source§impl<'f> Default for FunctionRegistry<'f>
impl<'f> Default for FunctionRegistry<'f>
Source§fn default() -> FunctionRegistry<'f>
fn default() -> FunctionRegistry<'f>
Auto Trait Implementations§
impl<'f> Freeze for FunctionRegistry<'f>
impl<'f> !RefUnwindSafe for FunctionRegistry<'f>
impl<'f> Send for FunctionRegistry<'f>
impl<'f> Sync for FunctionRegistry<'f>
impl<'f> Unpin for FunctionRegistry<'f>
impl<'f> !UnwindSafe for FunctionRegistry<'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