typed-eval

Crates.iotyped-eval
lib.rstyped-eval
version0.1.0
created_at2025-09-23 06:21:49.428494+00
updated_at2025-09-23 06:21:49.428494+00
descriptionA type-safe expression evaluation engine.
homepagehttps://github.com/romamik/typed-eval-rs
repositoryhttps://github.com/romamik/typed-eval-rs
max_upload_size
id1851052
size70,106
(romamik)

documentation

README

type-eval

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.

Features

  • Compile and evaluate expression like a + b * 10
  • Type-safe: every compiled expression has a known return type
  • Field access (user.age) and function calls (greet(user.name))
  • Extensible - register types, casts, etc.

Quick example

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);
}

How it works

typed-eval compiles expressions by combining closures into a single executable function.

For example, compiling the following expression: "a + 10", generates three closures:

  • A closure to get a from the context:
lhs = |ctx| ctx.a
  • A closure to return constant 10:
rhs = |ctx| 10
  • A final closure that calls the first two, adds their results, and returns the sum.
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.

Status

This is an experimental project.

License

This project is licensed under the MIT License.

Commit count: 90

cargo fmt