From 2ba875a17728648de8155a603b1e16215e800850 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 20 Sep 2020 14:23:14 +0800 Subject: [PATCH] Use take_string for &str and String parameters. --- codegen/src/function.rs | 6 ++---- codegen/src/test/function.rs | 2 +- codegen/src/test/module.rs | 4 ++-- src/any.rs | 4 +++- tests/plugins.rs | 4 ++++ 5 files changed, 12 insertions(+), 8 deletions(-) diff --git a/codegen/src/function.rs b/codegen/src/function.rs index 82e76214..2fca6d96 100644 --- a/codegen/src/function.rs +++ b/codegen/src/function.rs @@ -658,8 +658,7 @@ impl ExportedFn { is_string = true; is_ref = true; quote_spanned!(arg_type.span()=> - mem::take(args[#i]) - .clone().cast::()) + mem::take(args[#i]).take_immutable_string().unwrap()) } _ => panic!("internal error: why wasn't this found earlier!?"), }, @@ -667,8 +666,7 @@ impl ExportedFn { is_string = true; is_ref = false; quote_spanned!(arg_type.span()=> - mem::take(args[#i]) - .clone().cast::<#arg_type>()) + mem::take(args[#i]).take_string().unwrap()) } _ => { is_string = false; diff --git a/codegen/src/test/function.rs b/codegen/src/test/function.rs index b6d8e894..f22f1fe2 100644 --- a/codegen/src/test/function.rs +++ b/codegen/src/test/function.rs @@ -493,7 +493,7 @@ mod generate_tests { ) -> Result> { debug_assert_eq!(args.len(), 1usize, "wrong arg count: {} != {}", args.len(), 1usize); - let arg0 = mem::take(args[0usize]).clone().cast::(); + let arg0 = mem::take(args[0usize]).take_immutable_string().unwrap(); Ok(Dynamic::from(special_print(&arg0))) } diff --git a/codegen/src/test/module.rs b/codegen/src/test/module.rs index a2ab7eab..80a93f28 100644 --- a/codegen/src/test/module.rs +++ b/codegen/src/test/module.rs @@ -948,7 +948,7 @@ mod generate_tests { ) -> Result> { debug_assert_eq!(args.len(), 1usize, "wrong arg count: {} != {}", args.len(), 1usize); - let arg0 = mem::take(args[0usize]).clone().cast::(); + let arg0 = mem::take(args[0usize]).take_immutable_string().unwrap(); Ok(Dynamic::from(print_out_to(&arg0))) } @@ -1012,7 +1012,7 @@ mod generate_tests { ) -> Result> { debug_assert_eq!(args.len(), 1usize, "wrong arg count: {} != {}", args.len(), 1usize); - let arg0 = mem::take(args[0usize]).clone().cast::(); + let arg0 = mem::take(args[0usize]).take_string().unwrap(); Ok(Dynamic::from(print_out_to(arg0))) } diff --git a/src/any.rs b/src/any.rs index 7b1fa184..a1e532aa 100644 --- a/src/any.rs +++ b/src/any.rs @@ -1136,6 +1136,7 @@ impl Dynamic { } /// Convert the `Dynamic` into `String` and return it. + /// If there are other references to the same string, a cloned copy is returned. /// Returns the name of the actual type if the cast fails. #[inline(always)] pub fn take_string(self) -> Result { @@ -1145,7 +1146,8 @@ impl Dynamic { /// Convert the `Dynamic` into `ImmutableString` and return it. /// Returns the name of the actual type if the cast fails. - pub(crate) fn take_immutable_string(self) -> Result { + #[inline] + pub fn take_immutable_string(self) -> Result { match self.0 { Union::Str(s) => Ok(s), Union::FnPtr(f) => Ok(f.take_data().0), diff --git a/tests/plugins.rs b/tests/plugins.rs index ce403dd6..909ad51b 100644 --- a/tests/plugins.rs +++ b/tests/plugins.rs @@ -27,6 +27,9 @@ mod test { pub fn hash(_text: String) -> INT { 42 } + pub fn hash2(_text: &str) -> INT { + 42 + } #[rhai_fn(name = "test", name = "hi")] #[inline(always)] @@ -82,6 +85,7 @@ fn test_plugins_package() -> Result<(), Box> { assert_eq!(engine.eval::("let a = [1, 2, 3]; a.foo")?, 1); assert_eq!(engine.eval::(r#"hash("hello")"#)?, 42); + assert_eq!(engine.eval::(r#"hash2("hello")"#)?, 42); assert_eq!(engine.eval::("let a = [1, 2, 3]; test(a, 2)")?, 6); assert_eq!(engine.eval::("let a = [1, 2, 3]; hi(a, 2)")?, 6); assert_eq!(engine.eval::("let a = [1, 2, 3]; test(a, 2)")?, 6);