Use run and i64 instead of eval and INT for examples.

This commit is contained in:
Stephen Chung 2022-01-11 22:12:46 +08:00
parent 6048c5804b
commit f0e9d4a557
7 changed files with 25 additions and 25 deletions

View File

@ -1,8 +1,8 @@
use rhai::{Engine, EvalAltResult, INT};
use rhai::{Engine, EvalAltResult};
#[derive(Debug, Clone)]
struct TestStruct {
x: INT,
x: i64,
}
impl TestStruct {

View File

@ -1,8 +1,8 @@
use rhai::{Engine, EvalAltResult, INT};
use rhai::{Engine, EvalAltResult};
#[derive(Debug, Clone)]
struct TestStruct {
x: INT,
x: i64,
}
impl TestStruct {

View File

@ -1,11 +1,11 @@
use rhai::{Engine, EvalAltResult, INT};
use rhai::{Engine, EvalAltResult};
fn main() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
engine.run(r#"print("hello, world!")"#)?;
let result = engine.eval::<INT>("40 + 2")?;
let result = engine.eval::<i64>("40 + 2")?;
println!("Answer: {}", result); // prints 42

View File

@ -1,20 +1,20 @@
use rhai::{Engine, EvalAltResult, Scope, INT};
use rhai::{Engine, EvalAltResult, Scope};
fn main() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
let mut scope = Scope::new();
engine.eval_with_scope::<()>(&mut scope, "let x = 4 + 5")?;
engine.run_with_scope(&mut scope, "let x = 4 + 5")?;
println!("x = {}", scope.get_value::<INT>("x").unwrap());
println!("x = {}", scope.get_value::<i64>("x").unwrap());
for _ in 0..10 {
let result = engine.eval_with_scope::<INT>(&mut scope, "x += 1; x")?;
let result = engine.eval_with_scope::<i64>(&mut scope, "x += 1; x")?;
println!("result: {}", result);
}
println!("x = {}", scope.get_value::<INT>("x").unwrap());
println!("x = {}", scope.get_value::<i64>("x").unwrap());
Ok(())
}

View File

@ -1,6 +1,6 @@
use rhai::{Engine, EvalAltResult, INT};
use rhai::{Engine, EvalAltResult};
fn add(x: INT, y: INT) -> INT {
fn add(x: i64, y: i64) -> i64 {
x + y
}
@ -9,7 +9,7 @@ fn main() -> Result<(), Box<EvalAltResult>> {
engine.register_fn("add", add);
let result = engine.eval::<INT>("add(40, 2)")?;
let result = engine.eval::<i64>("add(40, 2)")?;
println!("Answer: {}", result); // prints 42

View File

@ -1,6 +1,6 @@
///! This example registers a variety of functions that operate on strings.
///! Remember to use `ImmutableString` or `&str` instead of `String` as parameters.
use rhai::{Engine, EvalAltResult, ImmutableString, Scope, INT};
use rhai::{Engine, EvalAltResult, ImmutableString, Scope};
use std::io::{stdin, stdout, Write};
/// Trim whitespace from a string. The original string argument is changed.
@ -15,26 +15,26 @@ fn trim_string(s: &mut ImmutableString) {
/// This version simply counts the number of _bytes_ in the UTF-8 representation.
///
/// This version uses `&str`.
fn count_string_bytes(s: &str) -> INT {
s.len() as INT
fn count_string_bytes(s: &str) -> i64 {
s.len() as i64
}
/// This version uses `ImmutableString` and `&str`.
fn find_substring(s: ImmutableString, sub: &str) -> INT {
s.find(sub).map(|x| x as INT).unwrap_or(-1)
fn find_substring(s: ImmutableString, sub: &str) -> i64 {
s.find(sub).map(|x| x as i64).unwrap_or(-1)
}
fn main() -> Result<(), Box<EvalAltResult>> {
// Create a `raw` Engine with no built-in string functions.
let mut engine = Engine::new_raw();
// Register string functions
engine
// Register string functions
.register_fn("trim", trim_string)
.register_fn("len", count_string_bytes)
.register_fn("index_of", find_substring)
// Register string functions using closures
.register_fn("display", |label: &str, value: INT| {
.register_fn("display", |label: &str, value: i64| {
println!("{}: {}", label, value)
})
.register_fn("display", |label: ImmutableString, value: &str| {

View File

@ -1,4 +1,4 @@
use rhai::{Engine, INT};
use rhai::Engine;
#[cfg(feature = "sync")]
use std::sync::Mutex;
@ -23,12 +23,12 @@ fn main() {
#[cfg(not(feature = "sync"))]
engine
.register_fn("get", move || rx_script.recv().unwrap_or_default())
.register_fn("put", move |v: INT| tx_script.send(v).unwrap());
.register_fn("put", move |v: i64| tx_script.send(v).unwrap());
#[cfg(feature = "sync")]
engine
.register_fn("get", move || rx_script.lock().unwrap().recv().unwrap())
.register_fn("put", move |v: INT| {
.register_fn("put", move |v: i64| {
tx_script.lock().unwrap().send(v).unwrap()
});
@ -54,7 +54,7 @@ fn main() {
println!("Starting main loop...");
let mut value: INT = 0;
let mut value: i64 = 0;
while value < 10 {
println!("Value: {}", value);