From 2755d39cdf40c4d43216e2413e8de4d754691f37 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Fri, 22 Apr 2022 12:12:55 +0800 Subject: [PATCH] Return early if no type casting for `call_fn`. --- src/api/call_fn.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/api/call_fn.rs b/src/api/call_fn.rs index 6ca6f59d..2e5fedc1 100644 --- a/src/api/call_fn.rs +++ b/src/api/call_fn.rs @@ -4,9 +4,10 @@ use crate::eval::{Caches, GlobalRuntimeState}; use crate::types::dynamic::Variant; use crate::{ - Dynamic, Engine, FuncArgs, Position, RhaiResult, RhaiResultOf, Scope, StaticVec, AST, ERR, + reify, Dynamic, Engine, FuncArgs, Position, RhaiResult, RhaiResultOf, Scope, StaticVec, AST, + ERR, }; -use std::any::type_name; +use std::any::{type_name, TypeId}; #[cfg(feature = "no_std")] use std::prelude::v1::*; @@ -66,6 +67,15 @@ impl Engine { let result = self.call_fn_raw(scope, ast, true, true, name, None, arg_values)?; + // Bail out early if the return type needs no cast + if TypeId::of::() == TypeId::of::() { + return Ok(reify!(result => T)); + } + if TypeId::of::() == TypeId::of::<()>() { + return Ok(reify!(() => T)); + } + + // Cast return type let typ = self.map_type_name(result.type_name()); result.try_cast().ok_or_else(|| {