Refine examples.
This commit is contained in:
parent
dc4e52e795
commit
db9dcd1bcc
@ -1,38 +1,40 @@
|
||||
use rhai::{Engine, RegisterFn, INT};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Debug, Clone)]
|
||||
struct TestStruct {
|
||||
x: INT,
|
||||
}
|
||||
|
||||
impl TestStruct {
|
||||
fn update(&mut self) {
|
||||
pub fn update(&mut self) {
|
||||
self.x += 1000;
|
||||
}
|
||||
|
||||
fn new() -> Self {
|
||||
pub fn new() -> Self {
|
||||
Self { x: 1 }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
fn main() {
|
||||
fn main() -> Result<(), Box<EvalAltResult>> {
|
||||
let mut engine = Engine::new();
|
||||
|
||||
engine
|
||||
.register_type::<TestStruct>()
|
||||
.register_fn("update", TestStruct::update)
|
||||
.register_fn("new_ts", TestStruct::new);
|
||||
.register_fn("new_ts", TestStruct::new)
|
||||
.register_fn("update", TestStruct::update);
|
||||
|
||||
println!(
|
||||
"{:?}",
|
||||
engine.eval::<TestStruct>("let x = new_ts(); x.update(); x")
|
||||
engine.eval::<TestStruct>("let x = new_ts(); x.update(); x")?
|
||||
);
|
||||
println!(
|
||||
"{:?}",
|
||||
engine.eval::<TestStruct>("let x = [new_ts()]; x[0].update(); x[0]")
|
||||
engine.eval::<TestStruct>("let x = [new_ts()]; x[0].update(); x[0]")?
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "no_index", feature = "no_object"))]
|
||||
|
@ -1,16 +1,16 @@
|
||||
use rhai::{Engine, EvalAltResult, RegisterFn, INT};
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Debug, Clone)]
|
||||
struct TestStruct {
|
||||
x: INT,
|
||||
}
|
||||
|
||||
impl TestStruct {
|
||||
fn update(&mut self) {
|
||||
pub fn update(&mut self) {
|
||||
self.x += 1000;
|
||||
}
|
||||
|
||||
fn new() -> Self {
|
||||
pub fn new() -> Self {
|
||||
Self { x: 1 }
|
||||
}
|
||||
}
|
||||
@ -21,8 +21,8 @@ fn main() -> Result<(), Box<EvalAltResult>> {
|
||||
|
||||
engine
|
||||
.register_type::<TestStruct>()
|
||||
.register_fn("update", TestStruct::update)
|
||||
.register_fn("new_ts", TestStruct::new);
|
||||
.register_fn("new_ts", TestStruct::new)
|
||||
.register_fn("update", TestStruct::update);
|
||||
|
||||
let result = engine.eval::<TestStruct>("let x = new_ts(); x.update(); x")?;
|
||||
|
||||
|
@ -67,7 +67,7 @@ fn main() {
|
||||
print_help();
|
||||
|
||||
'main_loop: loop {
|
||||
print!("rhai> ");
|
||||
print!("rhai-repl> ");
|
||||
stdout().flush().expect("couldn't flush stdout");
|
||||
|
||||
input.clear();
|
||||
@ -126,12 +126,12 @@ fn main() {
|
||||
}
|
||||
"astu" => {
|
||||
// print the last un-optimized AST
|
||||
println!("{:#?}\n", &ast_u);
|
||||
println!("{:#?}\n", ast_u);
|
||||
continue;
|
||||
}
|
||||
"ast" => {
|
||||
// print the last AST
|
||||
println!("{:#?}\n", &ast);
|
||||
println!("{:#?}\n", ast);
|
||||
continue;
|
||||
}
|
||||
"functions" => {
|
||||
|
@ -17,13 +17,13 @@ mod example {
|
||||
use rhai::{Dynamic, Engine, Map};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
struct Point {
|
||||
x: f64,
|
||||
y: f64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
struct MyStruct {
|
||||
a: i64,
|
||||
b: Vec<String>,
|
||||
@ -71,6 +71,18 @@ mod example {
|
||||
// Convert the 'Dynamic' object map into 'MyStruct'
|
||||
let x: MyStruct = from_dynamic(&result).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
x,
|
||||
MyStruct {
|
||||
a: 42,
|
||||
b: vec!["hello".into(), "world".into()],
|
||||
c: true,
|
||||
d: Point {
|
||||
x: 123.456,
|
||||
y: 999.0,
|
||||
},
|
||||
}
|
||||
);
|
||||
println!("Deserialized to struct: {:#?}", x);
|
||||
}
|
||||
}
|
||||
|
@ -69,7 +69,7 @@ fn main() -> Result<(), Box<EvalAltResult>> {
|
||||
display("Trimmed", x);
|
||||
display("Trimmed Length", x.len());
|
||||
display("Index of \"!!!\"", x.index_of("!!!"));
|
||||
"#,
|
||||
"#,
|
||||
)?;
|
||||
|
||||
println!();
|
||||
|
Loading…
Reference in New Issue
Block a user