pub struct CompiledLogic {
pub root: CompiledNode,
}
Expand description
Compiled logic that can be evaluated multiple times across different data.
CompiledLogic
represents a pre-processed JSONLogic expression that has been
optimized for repeated evaluation. It’s thread-safe and can be shared across
threads using Arc
.
§Performance Benefits
- Parse once, evaluate many: Avoid repeated JSON parsing
- Static evaluation: Constant expressions are pre-computed
- OpCode dispatch: Built-in operators use fast enum dispatch
- Thread-safe sharing: Use
Arc
to share across threads
§Example
use datalogic_rs::DataLogic;
use serde_json::json;
use std::sync::Arc;
let engine = DataLogic::new();
let logic = json!({">": [{"var": "score"}, 90]});
let compiled = engine.compile(&logic).unwrap(); // Returns Arc<CompiledLogic>
// Can be shared across threads
let compiled_clone = Arc::clone(&compiled);
std::thread::spawn(move || {
let data = json!({"score": 95});
let result = engine.evaluate_owned(&compiled_clone, data);
});
Fields§
§root: CompiledNode
The root node of the compiled logic tree
Implementations§
Source§impl CompiledLogic
impl CompiledLogic
Sourcepub fn new(root: CompiledNode) -> Self
pub fn new(root: CompiledNode) -> Self
Creates a new compiled logic from a root node.
§Arguments
root
- The root node of the compiled logic tree
Sourcepub fn compile(logic: &Value) -> Result<Self>
pub fn compile(logic: &Value) -> Result<Self>
Compiles a JSON value into a compiled logic structure.
This method performs basic compilation without static evaluation.
For optimal performance, use compile_with_static_eval
instead.
§Arguments
logic
- The JSON logic expression to compile
§Returns
A compiled logic structure, or an error if compilation fails.
Sourcepub fn compile_with_static_eval(
logic: &Value,
engine: &DataLogic,
) -> Result<Self>
pub fn compile_with_static_eval( logic: &Value, engine: &DataLogic, ) -> Result<Self>
Compiles with static evaluation using the provided engine.
This method performs optimizations including:
- Static evaluation of constant expressions
- OpCode assignment for built-in operators
- Structure preservation based on engine settings
§Arguments
logic
- The JSON logic expression to compileengine
- The DataLogic engine for static evaluation
§Returns
An optimized compiled logic structure, or an error if compilation fails.
Trait Implementations§
Source§impl Clone for CompiledLogic
impl Clone for CompiledLogic
Source§fn clone(&self) -> CompiledLogic
fn clone(&self) -> CompiledLogic
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreAuto Trait Implementations§
impl Freeze for CompiledLogic
impl RefUnwindSafe for CompiledLogic
impl Send for CompiledLogic
impl Sync for CompiledLogic
impl Unpin for CompiledLogic
impl UnwindSafe for CompiledLogic
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
Mutably borrows from an owned value. Read more