diff --git a/RELEASES.md b/RELEASES.md index 4913a137..22339cef 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -10,6 +10,12 @@ New features ------------ * `switch` statement. +* `Engine::register_module` to register a module as a sub-module in the global namespace. + +Enhancements +------------ + +* New constant `Dynamic::UNIT`. Version 0.19.5 diff --git a/doc/src/engine/custom-op.md b/doc/src/engine/custom-op.md index fa2b1487..e2a118e9 100644 --- a/doc/src/engine/custom-op.md +++ b/doc/src/engine/custom-op.md @@ -94,7 +94,7 @@ The following _precedence table_ shows the built-in precedence of standard Rhai | ------------------- | :-------------------------------------------------------------------------------------: | :----------------: | | Assignments | `=`, `+=`, `-=`, `*=`, `/=`, `~=`, `%=`,
`<<=`, `>>=`, `&=`, \|=, `^=` | 0 | | Logic and bit masks | \|\|, \|, `^` | 30 | -| Logic and bit masks | `&`, `&&` | 60 | +| Logic and bit masks | `&&`, `&` | 60 | | Comparisons | `==`, `!=` | 90 | | | `in` | 110 | | Comparisons | `>`, `>=`, `<`, `<=` | 130 | diff --git a/doc/src/engine/custom-syntax.md b/doc/src/engine/custom-syntax.md index ee086ec9..c9b77c90 100644 --- a/doc/src/engine/custom-syntax.md +++ b/doc/src/engine/custom-syntax.md @@ -216,7 +216,7 @@ fn implementation_func( } } - Ok(().into()) + Ok(Dynamic::UNIT) } // Register the custom syntax (sample): exec |x| -> { x += 1 } while x < 0; diff --git a/doc/src/patterns/control.md b/doc/src/patterns/control.md index 8743d260..25d73231 100644 --- a/doc/src/patterns/control.md +++ b/doc/src/patterns/control.md @@ -118,7 +118,7 @@ engine.register_result_fn("bunny_set_speed", move |speed: i64| return Err("Bunny is not yet going!".into()); } - Ok(().into()) + Ok(Dynamic::UNIT) ); ``` diff --git a/doc/src/patterns/events.md b/doc/src/patterns/events.md index 9334e726..0312e42e 100644 --- a/doc/src/patterns/events.md +++ b/doc/src/patterns/events.md @@ -137,7 +137,7 @@ impl Handler { // Default implementation of 'update' event handler self.scope.set_value("state2", SomeType::new(42)); // Turn function-not-found into a success - Ok(().into()) + Ok(Dynamic::UNIT) } _ => Err(err.into()) }) diff --git a/doc/src/patterns/singleton.md b/doc/src/patterns/singleton.md index 877180df..6f59f70a 100644 --- a/doc/src/patterns/singleton.md +++ b/doc/src/patterns/singleton.md @@ -131,7 +131,7 @@ pub mod bunny_api { Err("Bunny is not yet going!".into()) } else { b.borrow_mut().set_speed(speed); - Ok(().into()) + Ok(Dynamic::UNIT) } } pub fn turn_left(bunny: &mut SharedBunny) { diff --git a/doc/src/rust/packages/index.md b/doc/src/rust/packages/index.md index 020097cc..fe6fc36f 100644 --- a/doc/src/rust/packages/index.md +++ b/doc/src/rust/packages/index.md @@ -10,7 +10,7 @@ packages to be used. Packages typically contain Rust functions that are callable within a Rhai script. All functions registered in a package is loaded under the _global namespace_ -(i.e. they're available without module qualifiers). +(i.e. they're available without namespace qualifiers). Once a package is created (e.g. via `Package::new`), it can be _shared_ (via `Package::get`) among multiple instances of [`Engine`], even across threads (under [`sync`]). diff --git a/doc/src/rust/register-raw.md b/doc/src/rust/register-raw.md index c277436c..4d326e78 100644 --- a/doc/src/rust/register-raw.md +++ b/doc/src/rust/register-raw.md @@ -45,7 +45,7 @@ engine.register_raw_fn( *x += y; // perform the action - Ok(().into()) // must be 'Result>' + Ok(Dynamic::UNIT) // must be 'Result>' } ); diff --git a/doc/src/safety/progress.md b/doc/src/safety/progress.md index 39e274df..44260b43 100644 --- a/doc/src/safety/progress.md +++ b/doc/src/safety/progress.md @@ -37,7 +37,7 @@ wrapping this value. The termination token is commonly used to provide information on the _reason_ or _source_ behind the termination decision. -If the termination token is not needed, simply return `Some(().into())` to terminate the script +If the termination token is not needed, simply return `Some(Dynamic::UNIT)` to terminate the script run with [`()`] as the token. diff --git a/src/dynamic.rs b/src/dynamic.rs index 29c9aef9..592ca677 100644 --- a/src/dynamic.rs +++ b/src/dynamic.rs @@ -532,11 +532,13 @@ impl Clone for Dynamic { impl Default for Dynamic { #[inline(always)] fn default() -> Self { - Self(Union::Unit(())) + Self::UNIT } } impl Dynamic { + pub const UNIT: Dynamic = Self(Union::Unit(())); + /// Create a `Dynamic` from any type. A `Dynamic` value is simply returned as is. /// /// # Safety diff --git a/src/fn_call.rs b/src/fn_call.rs index 99991bea..7373c285 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -651,7 +651,7 @@ impl Engine { let script = script.trim(); if script.is_empty() { - return Ok(().into()); + return Ok(Dynamic::UNIT); } // Check for stack overflow diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index c3554529..41119a31 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -54,7 +54,7 @@ macro_rules! gen_array_functions { list.resize(len as usize, Dynamic::from(item)); } - Ok(().into()) + Ok(Dynamic::UNIT) } } })* } @@ -367,7 +367,7 @@ mod array_functions { list: &mut Array, reducer: FnPtr, ) -> Result> { - let mut result: Dynamic = ().into(); + let mut result: Dynamic = Dynamic::UNIT; for (i, item) in list.iter().enumerate() { result = reducer @@ -434,7 +434,7 @@ mod array_functions { list: &mut Array, reducer: FnPtr, ) -> Result> { - let mut result: Dynamic = ().into(); + let mut result: Dynamic = Dynamic::UNIT; for (i, item) in list.iter().enumerate().rev() { result = reducer @@ -529,7 +529,7 @@ mod array_functions { }) }); - Ok(().into()) + Ok(Dynamic::UNIT) } #[rhai_fn(return_raw)] pub fn drain( diff --git a/src/packages/string_more.rs b/src/packages/string_more.rs index 6025ad3f..fbf588c5 100644 --- a/src/packages/string_more.rs +++ b/src/packages/string_more.rs @@ -284,7 +284,7 @@ mod string_functions { } } - Ok(().into()) + Ok(Dynamic::UNIT) } #[rhai_fn(name = "pad", return_raw)] pub fn pad_with_string( @@ -328,7 +328,7 @@ mod string_functions { } } - Ok(().into()) + Ok(Dynamic::UNIT) } #[cfg(not(feature = "no_index"))] diff --git a/src/packages/time_basic.rs b/src/packages/time_basic.rs index f55288f2..e419905b 100644 --- a/src/packages/time_basic.rs +++ b/src/packages/time_basic.rs @@ -150,7 +150,7 @@ mod time_functions { #[rhai_fn(return_raw, name = "+=")] pub fn add_assign(x: &mut Instant, seconds: FLOAT) -> Result> { *x = add_impl(*x, seconds)?; - Ok(().into()) + Ok(Dynamic::UNIT) } #[rhai_fn(return_raw, name = "-")] pub fn subtract(x: Instant, seconds: FLOAT) -> Result> { @@ -162,7 +162,7 @@ mod time_functions { seconds: FLOAT, ) -> Result> { *x = subtract_impl(*x, seconds)?; - Ok(().into()) + Ok(Dynamic::UNIT) } } @@ -204,7 +204,7 @@ mod time_functions { #[rhai_fn(return_raw, name = "+=")] pub fn add_assign(x: &mut Instant, seconds: INT) -> Result> { *x = add_impl(*x, seconds)?; - Ok(().into()) + Ok(Dynamic::UNIT) } #[rhai_fn(return_raw, name = "-")] pub fn subtract(x: Instant, seconds: INT) -> Result> { @@ -213,7 +213,7 @@ mod time_functions { #[rhai_fn(return_raw, name = "-=")] pub fn subtract_assign(x: &mut Instant, seconds: INT) -> Result> { *x = subtract_impl(*x, seconds)?; - Ok(().into()) + Ok(Dynamic::UNIT) } #[rhai_fn(name = "==")] diff --git a/src/parser.rs b/src/parser.rs index bae5dbe4..2a04f708 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -957,7 +957,7 @@ fn parse_primary( let var_name_def = IdentX::new(state.get_interned_string(s), settings.pos); Expr::Variable(Box::new((None, None, 0, var_name_def))) } - // Module qualification + // Namespace qualification #[cfg(not(feature = "no_module"))] Token::Identifier(s) if *next_token == Token::DoubleColon => { // Once the identifier consumed we must enable next variables capturing diff --git a/src/serde_impl/ser.rs b/src/serde_impl/ser.rs index 4074577c..ba559859 100644 --- a/src/serde_impl/ser.rs +++ b/src/serde_impl/ser.rs @@ -247,7 +247,7 @@ impl Serializer for &mut DynamicSerializer { } fn serialize_none(self) -> Result> { - Ok(().into()) + Ok(Dynamic::UNIT) } fn serialize_some( @@ -258,7 +258,7 @@ impl Serializer for &mut DynamicSerializer { } fn serialize_unit(self) -> Result> { - Ok(().into()) + Ok(Dynamic::UNIT) } fn serialize_unit_struct(self, _name: &'static str) -> Result> { diff --git a/tests/syntax.rs b/tests/syntax.rs index d6a875cc..70272cef 100644 --- a/tests/syntax.rs +++ b/tests/syntax.rs @@ -1,4 +1,4 @@ -use rhai::{Engine, EvalAltResult, LexError, ParseError, ParseErrorType, INT, NO_POS}; +use rhai::{Dynamic, Engine, EvalAltResult, LexError, ParseError, ParseErrorType, INT, NO_POS}; #[test] fn test_custom_syntax() -> Result<(), Box> { @@ -48,7 +48,7 @@ fn test_custom_syntax() -> Result<(), Box> { } } - Ok(().into()) + Ok(Dynamic::UNIT) }, )?; @@ -65,7 +65,7 @@ fn test_custom_syntax() -> Result<(), Box> { // The first symbol must be an identifier assert_eq!( *engine - .register_custom_syntax(&["!"], 0, |_, _| Ok(().into())) + .register_custom_syntax(&["!"], 0, |_, _| Ok(Dynamic::UNIT)) .expect_err("should error") .0, ParseErrorType::BadInput(LexError::ImproperSymbol(