Simplified function registration to not require explicit coercion step. Simplified eval to take &str instead of String
This commit is contained in:
@@ -21,10 +21,10 @@ fn main() {
|
||||
|
||||
engine.register_type::<TestStruct>();
|
||||
|
||||
&(TestStruct::update as fn(&mut TestStruct)->()).register(&mut engine, "update");
|
||||
&(TestStruct::new as fn()->TestStruct).register(&mut engine, "new_ts");
|
||||
engine.register_fn("update", TestStruct::update);
|
||||
engine.register_fn("new_ts", TestStruct::new);
|
||||
|
||||
if let Ok(result) = engine.eval("var x = new_ts(); x.update(); x".to_string()).unwrap().downcast::<TestStruct>() {
|
||||
if let Ok(result) = engine.eval("var x = new_ts(); x.update(); x").unwrap().downcast::<TestStruct>() {
|
||||
println!("result: {}", result.x); // prints 1001
|
||||
}
|
||||
}
|
@@ -4,7 +4,7 @@ use rhai::Engine;
|
||||
fn main() {
|
||||
let mut engine = Engine::new();
|
||||
|
||||
if let Ok(result) = engine.eval("40 + 2".to_string()).unwrap().downcast::<i32>() {
|
||||
if let Ok(result) = engine.eval("40 + 2").unwrap().downcast::<i32>() {
|
||||
println!("Answer: {}", *result); // prints 42
|
||||
}
|
||||
}
|
@@ -5,9 +5,9 @@ fn main() {
|
||||
let mut engine = Engine::new();
|
||||
let mut scope: Scope = Vec::new();
|
||||
|
||||
if let Ok(_) = engine.eval_with_scope(&mut scope, "var x = 4 + 5".to_string()) { } else { assert!(false); }
|
||||
if let Ok(_) = engine.eval_with_scope(&mut scope, "var x = 4 + 5") { } else { assert!(false); }
|
||||
|
||||
if let Ok(result) = engine.eval_with_scope(&mut scope, "x".to_string()).unwrap().downcast::<i32>() {
|
||||
if let Ok(result) = engine.eval_with_scope(&mut scope, "x").unwrap().downcast::<i32>() {
|
||||
println!("result: {}", result);
|
||||
}
|
||||
}
|
||||
|
@@ -14,20 +14,20 @@ fn main() {
|
||||
for fname in env::args().skip(1) {
|
||||
let mut engine = Engine::new();
|
||||
|
||||
&(showit as fn(x: &mut i32)->()).register(&mut engine, "print");
|
||||
&(showit as fn(x: &mut i64)->()).register(&mut engine, "print");
|
||||
&(showit as fn(x: &mut u32)->()).register(&mut engine, "print");
|
||||
&(showit as fn(x: &mut u64)->()).register(&mut engine, "print");
|
||||
&(showit as fn(x: &mut f32)->()).register(&mut engine, "print");
|
||||
&(showit as fn(x: &mut f64)->()).register(&mut engine, "print");
|
||||
&(showit as fn(x: &mut bool)->()).register(&mut engine, "print");
|
||||
&(showit as fn(x: &mut String)->()).register(&mut engine, "print");
|
||||
engine.register_fn("print", showit as fn(x: &mut i32)->());
|
||||
engine.register_fn("print", showit as fn(x: &mut i64)->());
|
||||
engine.register_fn("print", showit as fn(x: &mut u32)->());
|
||||
engine.register_fn("print", showit as fn(x: &mut u64)->());
|
||||
engine.register_fn("print", showit as fn(x: &mut f32)->());
|
||||
engine.register_fn("print", showit as fn(x: &mut f64)->());
|
||||
engine.register_fn("print", showit as fn(x: &mut bool)->());
|
||||
engine.register_fn("print", showit as fn(x: &mut String)->());
|
||||
|
||||
if let Ok(mut f) = File::open(fname.clone()) {
|
||||
let mut contents = String::new();
|
||||
|
||||
if let Ok(_) = f.read_to_string(&mut contents) {
|
||||
match engine.eval(contents) {
|
||||
match engine.eval(&contents) {
|
||||
Ok(_) => (),
|
||||
Err(e) => {println!("Error: {:?}", e)}
|
||||
}
|
||||
|
@@ -8,9 +8,9 @@ fn add(x: i32, y: i32) -> i32 {
|
||||
fn main() {
|
||||
let mut engine = Engine::new();
|
||||
|
||||
&(add as fn(x: i32, y: i32)->i32).register(&mut engine, "add");
|
||||
engine.register_fn("add", add);
|
||||
|
||||
if let Ok(result) = engine.eval("add(40, 2)".to_string()).unwrap().downcast::<i32>() {
|
||||
if let Ok(result) = engine.eval("add(40, 2)").unwrap().downcast::<i32>() {
|
||||
println!("Answer: {}", *result); // prints 42
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user