Struct FunctionBindings

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

Source

pub fn new() -> Self

Creates a new empty function bindings.

Source§

impl<'f> FunctionBindings<'f>

Source

pub fn bind<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,

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

pub fn bind_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,

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

pub fn bind_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,

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

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

Finds a function overload set by name.

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

Returns Some(&FunctionOverloads) if found, None otherwise

Source

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

Finds a mutable function overload set by name.

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

Returns Some(&mut FunctionOverloads) if found, None otherwise

Source

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.

Source

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.

Source

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

Removes a function by name.

§Parameters
  • name: Function name to remove
§Returns

Returns Ok(()) if successfully removed, or an error if the function doesn’t exist

Source

pub fn clear(&mut self)

Clears all function bindings.

Trait Implementations§

Source§

impl<'f> Debug for FunctionBindings<'f>

Source§

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

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

impl<'f> Default for FunctionBindings<'f>

Source§

fn default() -> FunctionBindings<'f>

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

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