Jonathan Turner

Rhai 0.2 release

I’ve just updated Rhai to 0.2. This release focuses on improving the interaction between Rhai and Rust. The result is a cleaner, easier-to-use API. This new API does mean some API breakages, so you’ll need to update your code to work with 0.2.

With this release, Rhai also now has arrays as a built-in type.

You can upgrade/install Rhai from cargo.

API improvements

Rhai 0.2 has a lot of small improvements that give it a much more natural feel in Rust. The main three areas are function registration, getters/setter helpers, and simpler eval.

Function registration

Function registration now works through method calls on the engine, and we can infer the expected function type. The end result is generally much cleaner.

[Before]

&(add as fn(x: i32, y: i32)->i32).register(&mut engine, "add");

[Now]

engine.register_fn("add", add);

Getter/setter helpers

To help cut down on repetition, Rhai 0.2 has a few helpers for working with getters and setters.

[Before]

&(TestStruct::get_x as fn(&mut TestStruct)->i32).register(&mut engine, "get$x");
&(TestStruct::set_x as fn(&mut TestStruct, i32)->()).register(&mut engine, "set$x");

[Now]

engine.register_get_set("x", TestStruct::get_x, TestStruct::set_x);

Simpler eval

Eval will now take the expected return type as a type parameter, letting you cut out a few steps. We’ve also moved to using &str rather than String for eval inputs.

[Before]

if let Ok(result) = engine.eval("40 + 2".to_string()).unwrap().downcast::<i32>() {}

[Now]

if let Ok(result) = engine.eval::<i32>("40 + 2") {}

New feature: arrays

In addition to the API improvements, Rhai now has a built-in array type.

var y = [1, 2, 3]; 
y[1] = 5; 

print(y[1]);

Thanks!

Thanks for the feedback on Rhai’s first release. Also, big thanks to @nikomatsakis, who gave me a few key pointers on how improve the API.

Looking forward to hearing from you.