Simplify Dynamic::from for better inlining.

This commit is contained in:
Stephen Chung
2022-01-02 20:47:03 +08:00
parent 809feaf58d
commit 29f1328087
4 changed files with 77 additions and 83 deletions

View File

@@ -5,7 +5,7 @@
use super::call::FnCallArgs;
use super::callable_function::CallableFunction;
use super::native::{FnAny, SendSync};
use crate::r#unsafe::unsafe_try_cast;
use crate::r#unsafe::unsafe_cast;
use crate::tokenizer::Position;
use crate::types::dynamic::{DynamicWriteLock, Variant};
use crate::{Dynamic, NativeCallContext, RhaiResultOf, ERR};
@@ -50,8 +50,7 @@ pub fn by_value<T: Variant + Clone>(data: &mut Dynamic) -> T {
ref_t.clone()
} else if TypeId::of::<T>() == TypeId::of::<String>() {
// If T is `String`, data must be `ImmutableString`, so map directly to it
let value = mem::take(data).into_string().expect("`ImmutableString`");
unsafe_try_cast(value).expect("checked")
unsafe_cast(mem::take(data).into_string().expect("`ImmutableString`"))
} else {
// 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.