From aa0594210c3e97d3548ee0437f243fab30437fa9 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Fri, 19 Mar 2021 10:30:30 +0800 Subject: [PATCH] Use RhaiResult for plugin return. --- Cargo.toml | 2 +- codegen/Cargo.toml | 4 ++-- codegen/src/function.rs | 2 +- codegen/src/test/function.rs | 16 ++++++------- codegen/src/test/module.rs | 44 ++++++++++++++++++------------------ src/plugin.rs | 2 +- 6 files changed, 35 insertions(+), 35 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1719c147..264e1b2a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 = [] diff --git a/codegen/Cargo.toml b/codegen/Cargo.toml index 7bcdb878..e4b69a7e 100644 --- a/codegen/Cargo.toml +++ b/codegen/Cargo.toml @@ -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] diff --git a/codegen/src/function.rs b/codegen/src/function.rs index 7fef7f42..e1b23264 100644 --- a/codegen/src/function.rs +++ b/codegen/src/function.rs @@ -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> { + 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 diff --git a/codegen/src/test/function.rs b/codegen/src/test/function.rs index 58c2613a..9a25b68a 100644 --- a/codegen/src/test/function.rs +++ b/codegen/src/test/function.rs @@ -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> { + 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> { + 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::(); @@ -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> { + 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::(); @@ -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> { + 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> { + 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::(); @@ -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> { + 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::(); @@ -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> { + 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> { + 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(); diff --git a/codegen/src/test/module.rs b/codegen/src/test/module.rs index 79f6f872..90cbedc2 100644 --- a/codegen/src/test/module.rs +++ b/codegen/src/test/module.rs @@ -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> { + 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> { + 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::(); @@ -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> { + 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::(); @@ -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> { + 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::(); @@ -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> { + 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::(); @@ -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> { + 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::(); @@ -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> { + 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::(); @@ -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> { + 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> { + 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> { + 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> { + 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::(); @@ -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> { + 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> { + 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> { + 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> { + 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> { + 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> { + 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> { + 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> { + 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> { + 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> { + 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> { + 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() { diff --git a/src/plugin.rs b/src/plugin.rs index d7013ef7..d33aab73 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -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>; #[cfg(not(features = "no_module"))] pub use rhai_codegen::*;