Minimize usage of downcast_ref.

This commit is contained in:
Stephen Chung 2020-07-28 19:11:46 +08:00
parent 37ea24afe9
commit 411539f3be
2 changed files with 13 additions and 13 deletions

View File

@ -930,8 +930,8 @@ pub fn run_builtin_binary_op(
} }
if args_type == TypeId::of::<INT>() { if args_type == TypeId::of::<INT>() {
let x = *x.downcast_ref::<INT>().unwrap(); let x = x.clone().cast::<INT>();
let y = *y.downcast_ref::<INT>().unwrap(); let y = y.clone().cast::<INT>();
#[cfg(not(feature = "unchecked"))] #[cfg(not(feature = "unchecked"))]
match op { match op {
@ -972,8 +972,8 @@ pub fn run_builtin_binary_op(
_ => (), _ => (),
} }
} else if args_type == TypeId::of::<bool>() { } else if args_type == TypeId::of::<bool>() {
let x = *x.downcast_ref::<bool>().unwrap(); let x = x.clone().cast::<bool>();
let y = *y.downcast_ref::<bool>().unwrap(); let y = y.clone().cast::<bool>();
match op { match op {
"&" => return Ok(Some((x && y).into())), "&" => return Ok(Some((x && y).into())),
@ -998,8 +998,8 @@ pub fn run_builtin_binary_op(
_ => (), _ => (),
} }
} else if args_type == TypeId::of::<char>() { } else if args_type == TypeId::of::<char>() {
let x = *x.downcast_ref::<char>().unwrap(); let x = x.clone().cast::<char>();
let y = *y.downcast_ref::<char>().unwrap(); let y = y.clone().cast::<char>();
match op { match op {
"==" => return Ok(Some((x == y).into())), "==" => return Ok(Some((x == y).into())),
@ -1020,8 +1020,8 @@ pub fn run_builtin_binary_op(
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
if args_type == TypeId::of::<FLOAT>() { if args_type == TypeId::of::<FLOAT>() {
let x = *x.downcast_ref::<FLOAT>().unwrap(); let x = x.clone().cast::<FLOAT>();
let y = *y.downcast_ref::<FLOAT>().unwrap(); let y = y.clone().cast::<FLOAT>();
match op { match op {
"+" => return Ok(Some((x + y).into())), "+" => return Ok(Some((x + y).into())),
@ -1059,7 +1059,7 @@ pub fn run_builtin_op_assignment(
if args_type == TypeId::of::<INT>() { if args_type == TypeId::of::<INT>() {
let x = x.downcast_mut::<INT>().unwrap(); let x = x.downcast_mut::<INT>().unwrap();
let y = *y.downcast_ref::<INT>().unwrap(); let y = y.clone().cast::<INT>();
#[cfg(not(feature = "unchecked"))] #[cfg(not(feature = "unchecked"))]
match op { match op {
@ -1095,7 +1095,7 @@ pub fn run_builtin_op_assignment(
} }
} else if args_type == TypeId::of::<bool>() { } else if args_type == TypeId::of::<bool>() {
let x = x.downcast_mut::<bool>().unwrap(); let x = x.downcast_mut::<bool>().unwrap();
let y = *y.downcast_ref::<bool>().unwrap(); let y = y.clone().cast::<bool>();
match op { match op {
"&=" => return Ok(Some(*x = *x && y)), "&=" => return Ok(Some(*x = *x && y)),
@ -1115,7 +1115,7 @@ pub fn run_builtin_op_assignment(
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
if args_type == TypeId::of::<FLOAT>() { if args_type == TypeId::of::<FLOAT>() {
let x = x.downcast_mut::<FLOAT>().unwrap(); let x = x.downcast_mut::<FLOAT>().unwrap();
let y = *y.downcast_ref::<FLOAT>().unwrap(); let y = y.clone().cast::<FLOAT>();
match op { match op {
"+=" => return Ok(Some(*x += y)), "+=" => return Ok(Some(*x += y)),

View File

@ -15,7 +15,7 @@ use crate::stdlib::{
any::TypeId, any::TypeId,
boxed::Box, boxed::Box,
fmt::Display, fmt::Display,
format, format, mem,
string::{String, ToString}, string::{String, ToString},
vec::Vec, vec::Vec,
}; };
@ -242,7 +242,7 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str
} }
if len > 0 { if len > 0 {
let ch = *args[2].downcast_ref::< char>().unwrap(); let ch = mem::take(args[2]).cast::<char>();
let s = args[0].downcast_mut::<ImmutableString>().unwrap(); let s = args[0].downcast_mut::<ImmutableString>().unwrap();
let orig_len = s.chars().count(); let orig_len = s.chars().count();