Use RhaiResult for plugin return.

This commit is contained in:
Stephen Chung 2021-03-19 10:30:30 +08:00
parent 1200ffcd2b
commit aa0594210c
6 changed files with 35 additions and 35 deletions

View File

@ -19,7 +19,7 @@ categories = ["no-std", "embedded", "wasm", "parser-implementations"]
smallvec = { version = "1.6", default-features = false, features = ["union"] }
ahash = { version = "0.6", default-features = false }
num-traits = { version = "0.2", default_features = false }
rhai_codegen = { version = "0.3.3", path = "codegen" }
rhai_codegen = { version = "0.3.4", path = "codegen" }
[features]
default = []

View File

@ -1,6 +1,6 @@
[package]
name = "rhai_codegen"
version = "0.3.3"
version = "0.3.4"
edition = "2018"
authors = ["jhwgh1968"]
description = "Procedural macro support package for Rhai, a scripting language for Rust"
@ -12,7 +12,7 @@ license = "MIT OR Apache-2.0"
proc-macro = true
[dev-dependencies]
rhai = { path = ".." }
rhai = { path = "..", version = "0.19.15" }
trybuild = "1"
[dependencies]

View File

@ -908,7 +908,7 @@ impl ExportedFn {
let type_name = syn::Ident::new(on_type_name, proc_macro2::Span::call_site());
quote! {
impl PluginFunction for #type_name {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), #arg_count, "wrong arg count: {} != {}", args.len(), #arg_count);
#(#unpack_statements)*
#return_expr

View File

@ -277,7 +277,7 @@ mod generate_tests {
use super::*;
struct Token();
impl PluginFunction for Token {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 0usize,
"wrong arg count: {} != {}", args.len(), 0usize);
Ok(Dynamic::from(do_nothing()))
@ -331,7 +331,7 @@ mod generate_tests {
use super::*;
struct Token();
impl PluginFunction for Token {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 1usize,
"wrong arg count: {} != {}", args.len(), 1usize);
let arg0 = mem::take(args[0usize]).cast::<usize>();
@ -386,7 +386,7 @@ mod generate_tests {
use super::*;
struct Token();
impl PluginFunction for Token {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 1usize,
"wrong arg count: {} != {}", args.len(), 1usize);
let arg0 = mem::take(args[0usize]).cast::<usize>();
@ -444,7 +444,7 @@ mod generate_tests {
use super::*;
struct Token();
impl PluginFunction for Token {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 0usize,
"wrong arg count: {} != {}", args.len(), 0usize);
Ok(return_dynamic())
@ -494,7 +494,7 @@ mod generate_tests {
let expected_tokens = quote! {
impl PluginFunction for TestStruct {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 1usize,
"wrong arg count: {} != {}", args.len(), 1usize);
let arg0 = mem::take(args[0usize]).cast::<usize>();
@ -532,7 +532,7 @@ mod generate_tests {
use super::*;
struct Token();
impl PluginFunction for Token {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 2usize,
"wrong arg count: {} != {}", args.len(), 2usize);
let arg0 = mem::take(args[0usize]).cast::<usize>();
@ -589,7 +589,7 @@ mod generate_tests {
use super::*;
struct Token();
impl PluginFunction for Token {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 2usize,
"wrong arg count: {} != {}", args.len(), 2usize);
if args[0usize].is_read_only() {
@ -652,7 +652,7 @@ mod generate_tests {
use super::*;
struct Token();
impl PluginFunction for Token {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 1usize,
"wrong arg count: {} != {}", args.len(), 1usize);
let arg0 = mem::take(args[0usize]).take_immutable_string().unwrap();

View File

@ -304,7 +304,7 @@ mod generate_tests {
#[allow(non_camel_case_types)]
struct get_mystic_number_token();
impl PluginFunction for get_mystic_number_token {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 0usize,
"wrong arg count: {} != {}", args.len(), 0usize);
Ok(Dynamic::from(get_mystic_number()))
@ -378,7 +378,7 @@ mod generate_tests {
#[allow(non_camel_case_types)]
struct add_one_to_token();
impl PluginFunction for add_one_to_token {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 1usize,
"wrong arg count: {} != {}", args.len(), 1usize);
let arg0 = mem::take(args[0usize]).cast::<INT>();
@ -452,7 +452,7 @@ mod generate_tests {
#[allow(non_camel_case_types)]
struct add_one_to_token();
impl PluginFunction for add_one_to_token {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 1usize,
"wrong arg count: {} != {}", args.len(), 1usize);
let arg0 = mem::take(args[0usize]).cast::<INT>();
@ -540,7 +540,7 @@ mod generate_tests {
#[allow(non_camel_case_types)]
struct add_one_to_token();
impl PluginFunction for add_one_to_token {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 1usize,
"wrong arg count: {} != {}", args.len(), 1usize);
let arg0 = mem::take(args[0usize]).cast::<INT>();
@ -578,7 +578,7 @@ mod generate_tests {
#[allow(non_camel_case_types)]
struct add_n_to_token();
impl PluginFunction for add_n_to_token {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 2usize,
"wrong arg count: {} != {}", args.len(), 2usize);
let arg0 = mem::take(args[0usize]).cast::<INT>();
@ -655,7 +655,7 @@ mod generate_tests {
#[allow(non_camel_case_types)]
struct add_together_token();
impl PluginFunction for add_together_token {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 2usize,
"wrong arg count: {} != {}", args.len(), 2usize);
let arg0 = mem::take(args[0usize]).cast::<INT>();
@ -739,7 +739,7 @@ mod generate_tests {
#[allow(non_camel_case_types)]
struct add_together_token();
impl PluginFunction for add_together_token {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 2usize,
"wrong arg count: {} != {}", args.len(), 2usize);
let arg0 = mem::take(args[0usize]).cast::<INT>();
@ -997,7 +997,7 @@ mod generate_tests {
#[allow(non_camel_case_types)]
struct get_mystic_number_token();
impl PluginFunction for get_mystic_number_token {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 0usize,
"wrong arg count: {} != {}", args.len(), 0usize);
Ok(Dynamic::from(get_mystic_number()))
@ -1102,7 +1102,7 @@ mod generate_tests {
#[allow(non_camel_case_types)]
struct print_out_to_token();
impl PluginFunction for print_out_to_token {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 1usize,
"wrong arg count: {} != {}", args.len(), 1usize);
let arg0 = mem::take(args[0usize]).take_immutable_string().unwrap();
@ -1177,7 +1177,7 @@ mod generate_tests {
#[allow(non_camel_case_types)]
struct print_out_to_token();
impl PluginFunction for print_out_to_token {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 1usize,
"wrong arg count: {} != {}", args.len(), 1usize);
let arg0 = mem::take(args[0usize]).take_string().unwrap();
@ -1253,7 +1253,7 @@ mod generate_tests {
#[allow(non_camel_case_types)]
struct foo_token();
impl PluginFunction for foo_token {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 2usize,
"wrong arg count: {} != {}", args.len(), 2usize);
let arg1 = mem::take(args[1usize]).cast::<INT>();
@ -1329,7 +1329,7 @@ mod generate_tests {
#[allow(non_camel_case_types)]
struct increment_token();
impl PluginFunction for increment_token {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 1usize,
"wrong arg count: {} != {}", args.len(), 1usize);
if args[0usize].is_read_only() {
@ -1412,7 +1412,7 @@ mod generate_tests {
#[allow(non_camel_case_types)]
struct increment_token();
impl PluginFunction for increment_token {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 1usize,
"wrong arg count: {} != {}", args.len(), 1usize);
if args[0usize].is_read_only() {
@ -1516,7 +1516,7 @@ mod generate_tests {
#[allow(non_camel_case_types)]
struct increment_token();
impl PluginFunction for increment_token {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 1usize,
"wrong arg count: {} != {}", args.len(), 1usize);
if args[0usize].is_read_only() {
@ -1619,7 +1619,7 @@ mod generate_tests {
#[allow(non_camel_case_types)]
struct int_foo_token();
impl PluginFunction for int_foo_token {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 1usize,
"wrong arg count: {} != {}", args.len(), 1usize);
if args[0usize].is_read_only() {
@ -1701,7 +1701,7 @@ mod generate_tests {
#[allow(non_camel_case_types)]
struct int_foo_token();
impl PluginFunction for int_foo_token {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 1usize,
"wrong arg count: {} != {}", args.len(), 1usize);
if args[0usize].is_read_only() {
@ -1782,7 +1782,7 @@ mod generate_tests {
#[allow(non_camel_case_types)]
struct int_foo_token();
impl PluginFunction for int_foo_token {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 2usize,
"wrong arg count: {} != {}", args.len(), 2usize);
if args[0usize].is_read_only() {
@ -1867,7 +1867,7 @@ mod generate_tests {
#[allow(non_camel_case_types)]
struct int_foo_token();
impl PluginFunction for int_foo_token {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 2usize,
"wrong arg count: {} != {}", args.len(), 2usize);
if args[0usize].is_read_only() {
@ -1951,7 +1951,7 @@ mod generate_tests {
#[allow(non_camel_case_types)]
struct get_by_index_token();
impl PluginFunction for get_by_index_token {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 2usize,
"wrong arg count: {} != {}", args.len(), 2usize);
if args[0usize].is_read_only() {
@ -2041,7 +2041,7 @@ mod generate_tests {
#[allow(non_camel_case_types)]
struct get_by_index_token();
impl PluginFunction for get_by_index_token {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 2usize,
"wrong arg count: {} != {}", args.len(), 2usize);
if args[0usize].is_read_only() {
@ -2127,7 +2127,7 @@ mod generate_tests {
#[allow(non_camel_case_types)]
struct set_by_index_token();
impl PluginFunction for set_by_index_token {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 3usize,
"wrong arg count: {} != {}", args.len(), 3usize);
if args[0usize].is_read_only() {
@ -2221,7 +2221,7 @@ mod generate_tests {
#[allow(non_camel_case_types)]
struct set_by_index_token();
impl PluginFunction for set_by_index_token {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> Result<Dynamic, Box<EvalAltResult>> {
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
debug_assert_eq!(args.len(), 3usize,
"wrong arg count: {} != {}", args.len(), 3usize);
if args[0usize].is_read_only() {

View File

@ -2,11 +2,11 @@
pub use crate::fn_native::{CallableFunction, FnCallArgs};
pub use crate::stdlib::{any::TypeId, boxed::Box, format, mem, string::ToString, vec as new_vec};
use crate::RhaiResult;
pub use crate::{
Dynamic, Engine, EvalAltResult, FnAccess, FnNamespace, ImmutableString, Module,
NativeCallContext, Position,
};
pub type RhaiResult = Result<Dynamic, Box<EvalAltResult>>;
#[cfg(not(features = "no_module"))]
pub use rhai_codegen::*;