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_ref = true;
quote_spanned!(arg_type.span()=>
mem::take(args[#i])
.clone().cast::<ImmutableString>())
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;

View File

@ -493,7 +493,7 @@ mod generate_tests {
) -> Result<Dynamic, Box<EvalAltResult>> {
debug_assert_eq!(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)))
}

View File

@ -948,7 +948,7 @@ mod generate_tests {
) -> Result<Dynamic, Box<EvalAltResult>> {
debug_assert_eq!(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)))
}
@ -1012,7 +1012,7 @@ mod generate_tests {
) -> Result<Dynamic, Box<EvalAltResult>> {
debug_assert_eq!(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)))
}

View File

@ -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<String, &'static str> {
@ -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<ImmutableString, &'static str> {
#[inline]
pub fn take_immutable_string(self) -> Result<ImmutableString, &'static str> {
match self.0 {
Union::Str(s) => Ok(s),
Union::FnPtr(f) => Ok(f.take_data().0),

View File

@ -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<EvalAltResult>> {
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#"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]; hi(a, 2)")?, 6);
assert_eq!(engine.eval::<INT>("let a = [1, 2, 3]; test(a, 2)")?, 6);