Refine examples.

This commit is contained in:
Stephen Chung
2022-01-16 22:45:49 +08:00
parent d843baca12
commit 146129279c
6 changed files with 32 additions and 43 deletions

View File

@@ -1,3 +1,6 @@
#![cfg(not(feature = "no_index"))]
#![cfg(not(feature = "no_object"))]
use rhai::{Engine, EvalAltResult};
#[derive(Debug, Clone)]
@@ -14,8 +17,6 @@ impl TestStruct {
}
}
#[cfg(not(feature = "no_index"))]
#[cfg(not(feature = "no_object"))]
fn main() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
@@ -46,8 +47,3 @@ fn main() -> Result<(), Box<EvalAltResult>> {
Ok(())
}
#[cfg(any(feature = "no_index", feature = "no_object"))]
fn main() {
panic!("This example does not run under 'no_index' or 'no_object'.")
}

View File

@@ -1,3 +1,5 @@
#![cfg(not(feature = "no_object"))]
use rhai::{Engine, EvalAltResult};
#[derive(Debug, Clone)]
@@ -15,7 +17,6 @@ impl TestStruct {
}
}
#[cfg(not(feature = "no_object"))]
fn main() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
@@ -36,8 +37,3 @@ fn main() -> Result<(), Box<EvalAltResult>> {
Ok(())
}
#[cfg(feature = "no_object")]
fn main() {
panic!("This example does not run under 'no_object'.");
}

View File

@@ -1,19 +1,6 @@
#[cfg(any(not(feature = "serde"), feature = "no_object"))]
fn main() {
println!("This example requires the 'serde' feature to run.");
println!("Try: cargo run --features serde --example serde");
}
#![cfg(feature = "serde")]
#![cfg(not(feature = "no_object"))]
#[cfg(feature = "serde")]
#[cfg(not(feature = "no_object"))]
fn main() {
example::ser();
println!();
example::de();
}
#[cfg(feature = "serde")]
#[cfg(not(feature = "no_object"))]
mod example {
use rhai::serde::{from_dynamic, to_dynamic};
use rhai::{Dynamic, Engine, Map};
@@ -88,3 +75,9 @@ mod example {
println!("Deserialized to struct: {:#?}", x);
}
}
fn main() {
example::ser();
println!();
example::de();
}

View File

@@ -1,5 +1,7 @@
///! This example registers a variety of functions that operate on strings.
///! Remember to use `ImmutableString` or `&str` instead of `String` as parameters.
#![cfg(not(feature = "no_object"))]
use rhai::{Engine, EvalAltResult, ImmutableString, Scope};
use std::io::{stdin, stdout, Write};
@@ -64,10 +66,10 @@ fn main() -> Result<(), Box<EvalAltResult>> {
engine.run_with_scope(
&mut scope,
r#"
display("Length", x.len());
display("Length", x.len);
x.trim();
display("Trimmed", x);
display("Trimmed Length", x.len());
display("Trimmed Length", x.len);
display("Index of \"!!!\"", x.index_of("!!!"));
"#,
)?;