Add Dynamic::UNIT.
This commit is contained in:
parent
937b45a187
commit
b75964e383
@ -10,6 +10,12 @@ New features
|
|||||||
------------
|
------------
|
||||||
|
|
||||||
* `switch` statement.
|
* `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
|
Version 0.19.5
|
||||||
|
@ -94,7 +94,7 @@ The following _precedence table_ shows the built-in precedence of standard Rhai
|
|||||||
| ------------------- | :-------------------------------------------------------------------------------------: | :----------------: |
|
| ------------------- | :-------------------------------------------------------------------------------------: | :----------------: |
|
||||||
| Assignments | `=`, `+=`, `-=`, `*=`, `/=`, `~=`, `%=`,<br/>`<<=`, `>>=`, `&=`, <code>\|=</code>, `^=` | 0 |
|
| Assignments | `=`, `+=`, `-=`, `*=`, `/=`, `~=`, `%=`,<br/>`<<=`, `>>=`, `&=`, <code>\|=</code>, `^=` | 0 |
|
||||||
| Logic and bit masks | <code>\|\|</code>, <code>\|</code>, `^` | 30 |
|
| Logic and bit masks | <code>\|\|</code>, <code>\|</code>, `^` | 30 |
|
||||||
| Logic and bit masks | `&`, `&&` | 60 |
|
| Logic and bit masks | `&&`, `&` | 60 |
|
||||||
| Comparisons | `==`, `!=` | 90 |
|
| Comparisons | `==`, `!=` | 90 |
|
||||||
| | `in` | 110 |
|
| | `in` | 110 |
|
||||||
| Comparisons | `>`, `>=`, `<`, `<=` | 130 |
|
| Comparisons | `>`, `>=`, `<`, `<=` | 130 |
|
||||||
|
@ -216,7 +216,7 @@ fn implementation_func(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(().into())
|
Ok(Dynamic::UNIT)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register the custom syntax (sample): exec |x| -> { x += 1 } while x < 0;
|
// Register the custom syntax (sample): exec |x| -> { x += 1 } while x < 0;
|
||||||
|
@ -118,7 +118,7 @@ engine.register_result_fn("bunny_set_speed", move |speed: i64|
|
|||||||
return Err("Bunny is not yet going!".into());
|
return Err("Bunny is not yet going!".into());
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(().into())
|
Ok(Dynamic::UNIT)
|
||||||
);
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -137,7 +137,7 @@ impl Handler {
|
|||||||
// Default implementation of 'update' event handler
|
// Default implementation of 'update' event handler
|
||||||
self.scope.set_value("state2", SomeType::new(42));
|
self.scope.set_value("state2", SomeType::new(42));
|
||||||
// Turn function-not-found into a success
|
// Turn function-not-found into a success
|
||||||
Ok(().into())
|
Ok(Dynamic::UNIT)
|
||||||
}
|
}
|
||||||
_ => Err(err.into())
|
_ => Err(err.into())
|
||||||
})
|
})
|
||||||
|
@ -131,7 +131,7 @@ pub mod bunny_api {
|
|||||||
Err("Bunny is not yet going!".into())
|
Err("Bunny is not yet going!".into())
|
||||||
} else {
|
} else {
|
||||||
b.borrow_mut().set_speed(speed);
|
b.borrow_mut().set_speed(speed);
|
||||||
Ok(().into())
|
Ok(Dynamic::UNIT)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn turn_left(bunny: &mut SharedBunny) {
|
pub fn turn_left(bunny: &mut SharedBunny) {
|
||||||
|
@ -10,7 +10,7 @@ packages to be used.
|
|||||||
|
|
||||||
Packages typically contain Rust functions that are callable within a Rhai script.
|
Packages typically contain Rust functions that are callable within a Rhai script.
|
||||||
All functions registered in a package is loaded under the _global namespace_
|
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`)
|
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`]).
|
among multiple instances of [`Engine`], even across threads (under [`sync`]).
|
||||||
|
@ -45,7 +45,7 @@ engine.register_raw_fn(
|
|||||||
|
|
||||||
*x += y; // perform the action
|
*x += y; // perform the action
|
||||||
|
|
||||||
Ok(().into()) // must be 'Result<Dynamic, Box<EvalAltResult>>'
|
Ok(Dynamic::UNIT) // must be 'Result<Dynamic, Box<EvalAltResult>>'
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ wrapping this value.
|
|||||||
The termination token is commonly used to provide information on the _reason_ or _source_
|
The termination token is commonly used to provide information on the _reason_ or _source_
|
||||||
behind the termination decision.
|
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.
|
run with [`()`] as the token.
|
||||||
|
|
||||||
|
|
||||||
|
@ -532,11 +532,13 @@ impl Clone for Dynamic {
|
|||||||
impl Default for Dynamic {
|
impl Default for Dynamic {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self(Union::Unit(()))
|
Self::UNIT
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Dynamic {
|
impl Dynamic {
|
||||||
|
pub const UNIT: Dynamic = Self(Union::Unit(()));
|
||||||
|
|
||||||
/// Create a `Dynamic` from any type. A `Dynamic` value is simply returned as is.
|
/// Create a `Dynamic` from any type. A `Dynamic` value is simply returned as is.
|
||||||
///
|
///
|
||||||
/// # Safety
|
/// # Safety
|
||||||
|
@ -651,7 +651,7 @@ impl Engine {
|
|||||||
|
|
||||||
let script = script.trim();
|
let script = script.trim();
|
||||||
if script.is_empty() {
|
if script.is_empty() {
|
||||||
return Ok(().into());
|
return Ok(Dynamic::UNIT);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for stack overflow
|
// Check for stack overflow
|
||||||
|
@ -54,7 +54,7 @@ macro_rules! gen_array_functions {
|
|||||||
list.resize(len as usize, Dynamic::from(item));
|
list.resize(len as usize, Dynamic::from(item));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(().into())
|
Ok(Dynamic::UNIT)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})* }
|
})* }
|
||||||
@ -367,7 +367,7 @@ mod array_functions {
|
|||||||
list: &mut Array,
|
list: &mut Array,
|
||||||
reducer: FnPtr,
|
reducer: FnPtr,
|
||||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||||
let mut result: Dynamic = ().into();
|
let mut result: Dynamic = Dynamic::UNIT;
|
||||||
|
|
||||||
for (i, item) in list.iter().enumerate() {
|
for (i, item) in list.iter().enumerate() {
|
||||||
result = reducer
|
result = reducer
|
||||||
@ -434,7 +434,7 @@ mod array_functions {
|
|||||||
list: &mut Array,
|
list: &mut Array,
|
||||||
reducer: FnPtr,
|
reducer: FnPtr,
|
||||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||||
let mut result: Dynamic = ().into();
|
let mut result: Dynamic = Dynamic::UNIT;
|
||||||
|
|
||||||
for (i, item) in list.iter().enumerate().rev() {
|
for (i, item) in list.iter().enumerate().rev() {
|
||||||
result = reducer
|
result = reducer
|
||||||
@ -529,7 +529,7 @@ mod array_functions {
|
|||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
Ok(().into())
|
Ok(Dynamic::UNIT)
|
||||||
}
|
}
|
||||||
#[rhai_fn(return_raw)]
|
#[rhai_fn(return_raw)]
|
||||||
pub fn drain(
|
pub fn drain(
|
||||||
|
@ -284,7 +284,7 @@ mod string_functions {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(().into())
|
Ok(Dynamic::UNIT)
|
||||||
}
|
}
|
||||||
#[rhai_fn(name = "pad", return_raw)]
|
#[rhai_fn(name = "pad", return_raw)]
|
||||||
pub fn pad_with_string(
|
pub fn pad_with_string(
|
||||||
@ -328,7 +328,7 @@ mod string_functions {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(().into())
|
Ok(Dynamic::UNIT)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "no_index"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
|
@ -150,7 +150,7 @@ mod time_functions {
|
|||||||
#[rhai_fn(return_raw, name = "+=")]
|
#[rhai_fn(return_raw, name = "+=")]
|
||||||
pub fn add_assign(x: &mut Instant, seconds: FLOAT) -> Result<Dynamic, Box<EvalAltResult>> {
|
pub fn add_assign(x: &mut Instant, seconds: FLOAT) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||||
*x = add_impl(*x, seconds)?;
|
*x = add_impl(*x, seconds)?;
|
||||||
Ok(().into())
|
Ok(Dynamic::UNIT)
|
||||||
}
|
}
|
||||||
#[rhai_fn(return_raw, name = "-")]
|
#[rhai_fn(return_raw, name = "-")]
|
||||||
pub fn subtract(x: Instant, seconds: FLOAT) -> Result<Dynamic, Box<EvalAltResult>> {
|
pub fn subtract(x: Instant, seconds: FLOAT) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||||
@ -162,7 +162,7 @@ mod time_functions {
|
|||||||
seconds: FLOAT,
|
seconds: FLOAT,
|
||||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||||
*x = subtract_impl(*x, seconds)?;
|
*x = subtract_impl(*x, seconds)?;
|
||||||
Ok(().into())
|
Ok(Dynamic::UNIT)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -204,7 +204,7 @@ mod time_functions {
|
|||||||
#[rhai_fn(return_raw, name = "+=")]
|
#[rhai_fn(return_raw, name = "+=")]
|
||||||
pub fn add_assign(x: &mut Instant, seconds: INT) -> Result<Dynamic, Box<EvalAltResult>> {
|
pub fn add_assign(x: &mut Instant, seconds: INT) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||||
*x = add_impl(*x, seconds)?;
|
*x = add_impl(*x, seconds)?;
|
||||||
Ok(().into())
|
Ok(Dynamic::UNIT)
|
||||||
}
|
}
|
||||||
#[rhai_fn(return_raw, name = "-")]
|
#[rhai_fn(return_raw, name = "-")]
|
||||||
pub fn subtract(x: Instant, seconds: INT) -> Result<Dynamic, Box<EvalAltResult>> {
|
pub fn subtract(x: Instant, seconds: INT) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||||
@ -213,7 +213,7 @@ mod time_functions {
|
|||||||
#[rhai_fn(return_raw, name = "-=")]
|
#[rhai_fn(return_raw, name = "-=")]
|
||||||
pub fn subtract_assign(x: &mut Instant, seconds: INT) -> Result<Dynamic, Box<EvalAltResult>> {
|
pub fn subtract_assign(x: &mut Instant, seconds: INT) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||||
*x = subtract_impl(*x, seconds)?;
|
*x = subtract_impl(*x, seconds)?;
|
||||||
Ok(().into())
|
Ok(Dynamic::UNIT)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rhai_fn(name = "==")]
|
#[rhai_fn(name = "==")]
|
||||||
|
@ -957,7 +957,7 @@ fn parse_primary(
|
|||||||
let var_name_def = IdentX::new(state.get_interned_string(s), settings.pos);
|
let var_name_def = IdentX::new(state.get_interned_string(s), settings.pos);
|
||||||
Expr::Variable(Box::new((None, None, 0, var_name_def)))
|
Expr::Variable(Box::new((None, None, 0, var_name_def)))
|
||||||
}
|
}
|
||||||
// Module qualification
|
// Namespace qualification
|
||||||
#[cfg(not(feature = "no_module"))]
|
#[cfg(not(feature = "no_module"))]
|
||||||
Token::Identifier(s) if *next_token == Token::DoubleColon => {
|
Token::Identifier(s) if *next_token == Token::DoubleColon => {
|
||||||
// Once the identifier consumed we must enable next variables capturing
|
// Once the identifier consumed we must enable next variables capturing
|
||||||
|
@ -247,7 +247,7 @@ impl Serializer for &mut DynamicSerializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_none(self) -> Result<Self::Ok, Box<EvalAltResult>> {
|
fn serialize_none(self) -> Result<Self::Ok, Box<EvalAltResult>> {
|
||||||
Ok(().into())
|
Ok(Dynamic::UNIT)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_some<T: ?Sized + Serialize>(
|
fn serialize_some<T: ?Sized + Serialize>(
|
||||||
@ -258,7 +258,7 @@ impl Serializer for &mut DynamicSerializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_unit(self) -> Result<Self::Ok, Box<EvalAltResult>> {
|
fn serialize_unit(self) -> Result<Self::Ok, Box<EvalAltResult>> {
|
||||||
Ok(().into())
|
Ok(Dynamic::UNIT)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Box<EvalAltResult>> {
|
fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Box<EvalAltResult>> {
|
||||||
|
@ -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]
|
#[test]
|
||||||
fn test_custom_syntax() -> Result<(), Box<EvalAltResult>> {
|
fn test_custom_syntax() -> Result<(), Box<EvalAltResult>> {
|
||||||
@ -48,7 +48,7 @@ fn test_custom_syntax() -> Result<(), Box<EvalAltResult>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(().into())
|
Ok(Dynamic::UNIT)
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
@ -65,7 +65,7 @@ fn test_custom_syntax() -> Result<(), Box<EvalAltResult>> {
|
|||||||
// The first symbol must be an identifier
|
// The first symbol must be an identifier
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
*engine
|
*engine
|
||||||
.register_custom_syntax(&["!"], 0, |_, _| Ok(().into()))
|
.register_custom_syntax(&["!"], 0, |_, _| Ok(Dynamic::UNIT))
|
||||||
.expect_err("should error")
|
.expect_err("should error")
|
||||||
.0,
|
.0,
|
||||||
ParseErrorType::BadInput(LexError::ImproperSymbol(
|
ParseErrorType::BadInput(LexError::ImproperSymbol(
|
||||||
|
Loading…
Reference in New Issue
Block a user