Better String parameter error message.

This commit is contained in:
Stephen Chung 2020-06-08 10:26:32 +08:00
parent 5fb4b04cb0
commit ead366aac8
3 changed files with 10 additions and 6 deletions

View File

@ -708,7 +708,11 @@ impl Engine {
"{} ({})", "{} ({})",
fn_name, fn_name,
args.iter() args.iter()
.map(|name| self.map_type_name(name.type_name())) .map(|name| if name.is::<ImmutableString>() {
"&str | ImmutableString"
} else {
self.map_type_name(name.type_name())
})
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(", ") .join(", ")
), ),

View File

@ -101,10 +101,10 @@ pub fn by_ref<T: Variant + Clone>(data: &mut Dynamic) -> &mut T {
#[inline(always)] #[inline(always)]
pub fn by_value<T: Variant + Clone>(data: &mut Dynamic) -> T { pub fn by_value<T: Variant + Clone>(data: &mut Dynamic) -> T {
if TypeId::of::<T>() == TypeId::of::<&str>() { if TypeId::of::<T>() == TypeId::of::<&str>() {
// &str parameters are mapped to the underlying ImmutableString // If T is &str, data must be ImmutableString, so map directly to it
let r = data.as_str().unwrap(); let ref_str = data.as_str().unwrap();
let x = unsafe { mem::transmute::<_, &T>(&r) }; let ref_T = unsafe { mem::transmute::<_, &T>(&ref_str) };
x.clone() ref_T.clone()
} else { } else {
// We consume the argument and then replace it with () - the argument is not supposed to be used again. // We consume the argument and then replace it with () - the argument is not supposed to be used again.
// This way, we avoid having to clone the argument again, because it is already a clone when passed here. // This way, we avoid having to clone the argument again, because it is already a clone when passed here.

View File

@ -168,7 +168,7 @@ fn test_string_fn() -> Result<(), Box<EvalAltResult>> {
assert!(matches!( assert!(matches!(
*engine.eval::<INT>(r#"foo3("hello")"#).expect_err("should error"), *engine.eval::<INT>(r#"foo3("hello")"#).expect_err("should error"),
EvalAltResult::ErrorFunctionNotFound(ref x, _) if x == "foo3 (string)" EvalAltResult::ErrorFunctionNotFound(ref x, _) if x == "foo3 (&str | ImmutableString)"
)); ));
Ok(()) Ok(())