Struct FunctionRegistry

Source
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>

Source

pub fn new() -> Self

Creates a new empty function registry.

Source§

impl<'f> FunctionRegistry<'f>

Source

pub fn register<F, Fm, Args>( &mut self, name: impl Into<String>, member: bool, f: F, ) -> Result<&mut Self, Error>
where F: IntoFunction<'f, Fm, Args>, Fm: FnMarker, Args: Arguments,

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 implement IntoFunction
  • Fm: Function marker (sync or async), automatically inferred
  • Args: Argument tuple type, automatically inferred from function signature
§Parameters
  • name: Function name
  • member: 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)
    }
})?;
Source

pub fn register_member<F, Fm, Args>( &mut self, name: impl Into<String>, f: F, ) -> Result<&mut Self, Error>
where F: IntoFunction<'f, Fm, Args>, Fm: FnMarker, Args: Arguments,

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)?;
Source

pub fn register_global<F, Fm, Args>( &mut self, name: impl Into<String>, f: F, ) -> Result<&mut Self, Error>
where F: IntoFunction<'f, Fm, Args>, Fm: FnMarker, Args: Arguments,

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)?;
Source

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
§Parameters
  • name: Function name
  • member: 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)?;
Source

pub fn declare_member<D>( &mut self, name: impl Into<String>, ) -> Result<&mut Self, Error>
where D: FunctionDecl,

Declares a member function signature.

Convenience method for declaring member function signatures for compile-time type checking without providing implementations.

§Type Parameters
§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")?;
Source

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
§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")?;
Source

pub fn find( &self, name: &str, ) -> Option<&FunctionOverloads<FunctionDeclOrImpl<'f>>>

Finds function overloads by name.

§Parameters
  • name: Function name to search for
§Returns

Some(&FunctionOverloads) if found, None if not found

Source

pub fn find_mut( &mut self, name: &str, ) -> Option<&mut FunctionOverloads<FunctionDeclOrImpl<'f>>>

Finds function overloads by name (mutable).

§Parameters
  • name: Function name to search for
§Returns

Some(&mut FunctionOverloads) if found, None if not found

Source

pub fn entries( &self, ) -> impl Iterator<Item = (&str, &FunctionOverloads<FunctionDeclOrImpl<'f>>)>

Returns an iterator over all function entries.

§Returns

Iterator yielding (&str, &FunctionOverloads) pairs

Source

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

Source

pub fn remove(&mut self, name: &str) -> Result<(), Error>

Removes all overloads for a function name.

§Parameters
  • name: Function name to remove
§Returns

Ok(()) if removal was successful, Err(Error) if function not found

Source

pub fn clear(&mut self)

Clears all registered functions.

Source

pub fn len(&self) -> usize

Returns the number of registered function names.

Note: This counts function names, not individual overloads.

§Returns

Number of registered function names

Source

pub fn is_empty(&self) -> bool

Returns whether the registry is empty.

§Returns

true if no functions are registered, false otherwise

Trait Implementations§

Source§

impl<'f> Debug for FunctionRegistry<'f>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'f> Default for FunctionRegistry<'f>

Source§

fn default() -> FunctionRegistry<'f>

Returns the “default value” for a type. Read more

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> 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> 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, 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