Module function_registry

Source
Expand description

Function registry and calling interface.

This module provides the function registry system that manages standalone functions and methods available in expressions. It also includes utilities for converting between Value types and native Rust types for function implementations.

The function registry is a thread-safe singleton that uses OnceLock and RwLock to ensure one-time initialization with all built-in functions registered while allowing multiple concurrent readers.

§Usage

Functions are typically registered using the macros provided in crate::macros and are automatically available through the global registry:

use aimx::{aimx_parse, ExpressionLike, Context};

let mut context = Context::new();
let expression = aimx_parse("sqrt(16) + abs(-5)");
let result = expression.evaluate(&mut context).unwrap();
assert_eq!(result.to_string(), "9");

Custom functions can be registered with the registry:

use aimx::{function_registry::FunctionRegistry, define_direct_function};

// Get a write lock on the registry
let mut registry = FunctionRegistry::write_lock().unwrap();
 
// Register a custom function
define_direct_function!(
    double,
    args: [f64],
    |x: f64| x * 2.0
)(&mut *registry);

// Drop the lock to allow other threads to access the registry
drop(registry);

Functions can be called as standalone functions or as methods on values:

// Standalone function call
sqrt(16)

// Method call on array
(1, 2, 3).sum()

Structs§

FunctionRegistry
Registry for managing function handlers.

Traits§

FunctionHandler
Trait for function handlers.
ValueConverter
Trait for converting Values to native types.

Functions§

convert_result_to_value
Convert a native Rust type back to a Value.
convert_value_to_type
Convert a Value to a native Rust type based on the expected type T.