Crates.io | typed-eval |
lib.rs | typed-eval |
version | 0.1.0 |
created_at | 2025-09-23 06:21:49.428494+00 |
updated_at | 2025-09-23 06:21:49.428494+00 |
description | A type-safe expression evaluation engine. |
homepage | https://github.com/romamik/typed-eval-rs |
repository | https://github.com/romamik/typed-eval-rs |
max_upload_size | |
id | 1851052 |
size | 70,106 |
typed-eval is a type-safe expression evaluation engine for Rust. It lets you compile and execute dynamic expressions against a strongly typed context objects with full type checking at compile time.
a + b * 10
user.age
) and function calls (greet(user.name)
)use std::borrow::Cow;
use typed_eval::{Compiler, EvalType, eval_type_methods};
#[derive(EvalType)]
struct User {
name: String,
age: i64,
}
#[eval_type_methods]
impl User {
fn greet(&self) -> Cow<'_, str> {
format!("Hello, {}", self.name).into()
}
}
#[derive(EvalType)]
#[typed_eval(no_methods)]
struct Context {
user: User,
}
fn main() {
let compiler = Compiler::new().unwrap();
let greet_user = compiler.compile::<String>("user.greet()").unwrap();
let double_age = compiler.compile::<i64>("user.age * 2").unwrap();
let context = Context {
user: User {
name: "Bob".into(),
age: 45,
},
};
assert_eq!(greet_user(&context), "Hello, Bob");
assert_eq!(double_age(&context), 90);
}
typed-eval compiles expressions by combining closures into a single executable function.
For example, compiling the following expression: "a + 10"
, generates three closures:
a
from the context:lhs = |ctx| ctx.a
rhs = |ctx| 10
result = |ctx| lhs(ctx) + rhs(ctx)
This means the compiled function executes directly on the context with minimal overhead: no AST walking, no bytecode, etc.
This is an experimental project.
This project is licensed under the MIT License.