Use take_string for &str and String parameters.

This commit is contained in:
Stephen Chung 2020-09-20 14:23:14 +08:00
parent bf245a798b
commit 2ba875a177
5 changed files with 12 additions and 8 deletions

View File

@ -658,8 +658,7 @@ impl ExportedFn {
is_string = true; is_string = true;
is_ref = true; is_ref = true;
quote_spanned!(arg_type.span()=> quote_spanned!(arg_type.span()=>
mem::take(args[#i]) mem::take(args[#i]).take_immutable_string().unwrap())
.clone().cast::<ImmutableString>())
} }
_ => panic!("internal error: why wasn't this found earlier!?"), _ => panic!("internal error: why wasn't this found earlier!?"),
}, },
@ -667,8 +666,7 @@ impl ExportedFn {
is_string = true; is_string = true;
is_ref = false; is_ref = false;
quote_spanned!(arg_type.span()=> quote_spanned!(arg_type.span()=>
mem::take(args[#i]) mem::take(args[#i]).take_string().unwrap())
.clone().cast::<#arg_type>())
} }
_ => { _ => {
is_string = false; is_string = false;

View File

@ -493,7 +493,7 @@ mod generate_tests {
) -> Result<Dynamic, Box<EvalAltResult>> { ) -> Result<Dynamic, Box<EvalAltResult>> {
debug_assert_eq!(args.len(), 1usize, debug_assert_eq!(args.len(), 1usize,
"wrong arg count: {} != {}", args.len(), 1usize); "wrong arg count: {} != {}", args.len(), 1usize);
let arg0 = mem::take(args[0usize]).clone().cast::<ImmutableString>(); let arg0 = mem::take(args[0usize]).take_immutable_string().unwrap();
Ok(Dynamic::from(special_print(&arg0))) Ok(Dynamic::from(special_print(&arg0)))
} }

View File

@ -948,7 +948,7 @@ mod generate_tests {
) -> Result<Dynamic, Box<EvalAltResult>> { ) -> Result<Dynamic, Box<EvalAltResult>> {
debug_assert_eq!(args.len(), 1usize, debug_assert_eq!(args.len(), 1usize,
"wrong arg count: {} != {}", args.len(), 1usize); "wrong arg count: {} != {}", args.len(), 1usize);
let arg0 = mem::take(args[0usize]).clone().cast::<ImmutableString>(); let arg0 = mem::take(args[0usize]).take_immutable_string().unwrap();
Ok(Dynamic::from(print_out_to(&arg0))) Ok(Dynamic::from(print_out_to(&arg0)))
} }
@ -1012,7 +1012,7 @@ mod generate_tests {
) -> Result<Dynamic, Box<EvalAltResult>> { ) -> Result<Dynamic, Box<EvalAltResult>> {
debug_assert_eq!(args.len(), 1usize, debug_assert_eq!(args.len(), 1usize,
"wrong arg count: {} != {}", args.len(), 1usize); "wrong arg count: {} != {}", args.len(), 1usize);
let arg0 = mem::take(args[0usize]).clone().cast::<String>(); let arg0 = mem::take(args[0usize]).take_string().unwrap();
Ok(Dynamic::from(print_out_to(arg0))) Ok(Dynamic::from(print_out_to(arg0)))
} }

View File

@ -1136,6 +1136,7 @@ impl Dynamic {
} }
/// Convert the `Dynamic` into `String` and return it. /// 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. /// Returns the name of the actual type if the cast fails.
#[inline(always)] #[inline(always)]
pub fn take_string(self) -> Result<String, &'static str> { pub fn take_string(self) -> Result<String, &'static str> {
@ -1145,7 +1146,8 @@ impl Dynamic {
/// Convert the `Dynamic` into `ImmutableString` and return it. /// Convert the `Dynamic` into `ImmutableString` and return it.
/// Returns the name of the actual type if the cast fails. /// Returns the name of the actual type if the cast fails.
pub(crate) fn take_immutable_string(self) -> Result<ImmutableString, &'static str> { #[inline]
pub fn take_immutable_string(self) -> Result<ImmutableString, &'static str> {
match self.0 { match self.0 {
Union::Str(s) => Ok(s), Union::Str(s) => Ok(s),
Union::FnPtr(f) => Ok(f.take_data().0), Union::FnPtr(f) => Ok(f.take_data().0),

View File

@ -27,6 +27,9 @@ mod test {
pub fn hash(_text: String) -> INT { pub fn hash(_text: String) -> INT {
42 42
} }
pub fn hash2(_text: &str) -> INT {
42
}
#[rhai_fn(name = "test", name = "hi")] #[rhai_fn(name = "test", name = "hi")]
#[inline(always)] #[inline(always)]
@ -82,6 +85,7 @@ fn test_plugins_package() -> Result<(), Box<EvalAltResult>> {
assert_eq!(engine.eval::<INT>("let a = [1, 2, 3]; a.foo")?, 1); assert_eq!(engine.eval::<INT>("let a = [1, 2, 3]; a.foo")?, 1);
assert_eq!(engine.eval::<INT>(r#"hash("hello")"#)?, 42); assert_eq!(engine.eval::<INT>(r#"hash("hello")"#)?, 42);
assert_eq!(engine.eval::<INT>(r#"hash2("hello")"#)?, 42);
assert_eq!(engine.eval::<INT>("let a = [1, 2, 3]; test(a, 2)")?, 6); assert_eq!(engine.eval::<INT>("let a = [1, 2, 3]; test(a, 2)")?, 6);
assert_eq!(engine.eval::<INT>("let a = [1, 2, 3]; hi(a, 2)")?, 6); assert_eq!(engine.eval::<INT>("let a = [1, 2, 3]; hi(a, 2)")?, 6);
assert_eq!(engine.eval::<INT>("let a = [1, 2, 3]; test(a, 2)")?, 6); assert_eq!(engine.eval::<INT>("let a = [1, 2, 3]; test(a, 2)")?, 6);