From 5935a889587333aa658c83629d1896469cc0e7ff Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 16 Jan 2022 22:50:39 +0800 Subject: [PATCH] Revert "Refine examples." This reverts commit 146129279c7e325d1bbea0cedc5a657f25b0734a. --- examples/arrays_and_structs.rs | 10 +++++++--- examples/custom_types_and_methods.rs | 8 ++++++-- examples/serde.rs | 23 +++++++++++++++-------- examples/strings.rs | 6 ++---- src/eval/chaining.rs | 16 ++++++++-------- src/eval/target.rs | 12 +++++------- 6 files changed, 43 insertions(+), 32 deletions(-) diff --git a/examples/arrays_and_structs.rs b/examples/arrays_and_structs.rs index c27e997d..dab0880b 100644 --- a/examples/arrays_and_structs.rs +++ b/examples/arrays_and_structs.rs @@ -1,6 +1,3 @@ -#![cfg(not(feature = "no_index"))] -#![cfg(not(feature = "no_object"))] - use rhai::{Engine, EvalAltResult}; #[derive(Debug, Clone)] @@ -17,6 +14,8 @@ impl TestStruct { } } +#[cfg(not(feature = "no_index"))] +#[cfg(not(feature = "no_object"))] fn main() -> Result<(), Box> { let mut engine = Engine::new(); @@ -47,3 +46,8 @@ fn main() -> Result<(), Box> { Ok(()) } + +#[cfg(any(feature = "no_index", feature = "no_object"))] +fn main() { + panic!("This example does not run under 'no_index' or 'no_object'.") +} diff --git a/examples/custom_types_and_methods.rs b/examples/custom_types_and_methods.rs index f420acd1..b52d0a27 100644 --- a/examples/custom_types_and_methods.rs +++ b/examples/custom_types_and_methods.rs @@ -1,5 +1,3 @@ -#![cfg(not(feature = "no_object"))] - use rhai::{Engine, EvalAltResult}; #[derive(Debug, Clone)] @@ -17,6 +15,7 @@ impl TestStruct { } } +#[cfg(not(feature = "no_object"))] fn main() -> Result<(), Box> { let mut engine = Engine::new(); @@ -37,3 +36,8 @@ fn main() -> Result<(), Box> { Ok(()) } + +#[cfg(feature = "no_object")] +fn main() { + panic!("This example does not run under 'no_object'."); +} diff --git a/examples/serde.rs b/examples/serde.rs index dd4e915f..f5554889 100644 --- a/examples/serde.rs +++ b/examples/serde.rs @@ -1,6 +1,19 @@ -#![cfg(feature = "serde")] -#![cfg(not(feature = "no_object"))] +#[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"))] +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}; @@ -75,9 +88,3 @@ mod example { println!("Deserialized to struct: {:#?}", x); } } - -fn main() { - example::ser(); - println!(); - example::de(); -} diff --git a/examples/strings.rs b/examples/strings.rs index 33af68c0..a0997da2 100644 --- a/examples/strings.rs +++ b/examples/strings.rs @@ -1,7 +1,5 @@ ///! 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}; @@ -66,10 +64,10 @@ fn main() -> Result<(), Box> { 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("!!!")); "#, )?; diff --git a/src/eval/chaining.rs b/src/eval/chaining.rs index d2589f0b..6ac788fd 100644 --- a/src/eval/chaining.rs +++ b/src/eval/chaining.rs @@ -1,7 +1,7 @@ //! Types to support chaining operations (i.e. indexing and dotting). #![cfg(any(not(feature = "no_index"), not(feature = "no_object")))] -use super::{EvalState, GlobalRuntimeState, Target}; +use super::{calc_index, EvalState, GlobalRuntimeState, Target}; use crate::ast::{Expr, OpAssignment}; use crate::types::dynamic::Union; use crate::{Dynamic, Engine, Module, Position, RhaiResult, RhaiResultOf, Scope, StaticVec, ERR}; @@ -790,7 +790,7 @@ impl Engine { .as_int() .map_err(|typ| self.make_type_mismatch_err::(typ, pos))?; let len = arr.len(); - let arr_idx = super::calc_index(len, index, true, || { + let arr_idx = calc_index(len, index, true, || { ERR::ErrorArrayBounds(len, index, pos).into() })?; @@ -804,7 +804,7 @@ impl Engine { .as_int() .map_err(|typ| self.make_type_mismatch_err::(typ, pos))?; let len = arr.len(); - let arr_idx = super::calc_index(len, index, true, || { + let arr_idx = calc_index(len, index, true, || { ERR::ErrorArrayBounds(len, index, pos).into() })?; @@ -845,10 +845,10 @@ impl Engine { let start = range.start; let end = range.end; - let start = super::calc_index(BITS, start, false, || { + let start = calc_index(BITS, start, false, || { ERR::ErrorBitFieldBounds(BITS, start, pos).into() })?; - let end = super::calc_index(BITS, end, false, || { + let end = calc_index(BITS, end, false, || { ERR::ErrorBitFieldBounds(BITS, end, pos).into() })?; @@ -870,10 +870,10 @@ impl Engine { let start = *range.start(); let end = *range.end(); - let start = super::calc_index(BITS, start, false, || { + let start = calc_index(BITS, start, false, || { ERR::ErrorBitFieldBounds(BITS, start, pos).into() })?; - let end = super::calc_index(BITS, end, false, || { + let end = calc_index(BITS, end, false, || { ERR::ErrorBitFieldBounds(BITS, end, pos).into() })?; @@ -914,7 +914,7 @@ impl Engine { const BITS: usize = std::mem::size_of::() * 8; - let bit = super::calc_index(BITS, index, true, || { + let bit = calc_index(BITS, index, true, || { ERR::ErrorBitFieldBounds(BITS, index, pos).into() })?; diff --git a/src/eval/target.rs b/src/eval/target.rs index f31647f9..4209c3fa 100644 --- a/src/eval/target.rs +++ b/src/eval/target.rs @@ -1,7 +1,7 @@ //! Type to hold a mutable reference to the target of an evaluation. use crate::types::dynamic::Variant; -use crate::{Dynamic, RhaiResultOf}; +use crate::{Dynamic, RhaiResultOf, INT}; use std::ops::{Deref, DerefMut}; #[cfg(feature = "no_std")] use std::prelude::v1::*; @@ -11,9 +11,8 @@ use std::prelude::v1::*; // Negative starting positions count from the end. // // Values going over bounds are limited to the actual length. -#[inline] -#[allow(dead_code)] -pub fn calc_offset_len(length: usize, start: crate::INT, len: crate::INT) -> (usize, usize) { +#[inline(always)] +pub fn calc_offset_len(length: usize, start: INT, len: INT) -> (usize, usize) { let start = if start < 0 { start.checked_abs().map_or(0, |positive_start| { length - usize::min(positive_start as usize, length) @@ -40,11 +39,10 @@ pub fn calc_offset_len(length: usize, start: crate::INT, len: crate::INT) -> (us // Negative starting positions count from the end. // // Values going over bounds call the provided closure to return a default value or an error. -#[inline] -#[allow(dead_code)] +#[inline(always)] pub fn calc_index( length: usize, - start: crate::INT, + start: INT, negative_count_from_end: bool, err: impl Fn() -> Result, ) -> Result {