From 6d11fdcd18546cdd9bafd1f81b61ecc328654dd6 Mon Sep 17 00:00:00 2001 From: J Henry Waugh Date: Tue, 11 Aug 2020 19:05:52 -0500 Subject: [PATCH 1/2] codegen: replace downcast_clone with more efficient mem::take --- codegen/src/function.rs | 19 +++++++++---------- codegen/src/module.rs | 8 ++++---- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/codegen/src/function.rs b/codegen/src/function.rs index 7b28eeeb..ae50e45a 100644 --- a/codegen/src/function.rs +++ b/codegen/src/function.rs @@ -356,16 +356,15 @@ impl ExportedFn { &syn::Type::Path(ref p) if p.path == str_type_path => { is_str_ref = true; quote_spanned!(arg_type.span()=> - args[#i] - .downcast_clone::() - .unwrap()) + std::mem::take(args[#i]) + .clone().cast::()) } _ => panic!("internal error: why wasn't this found earlier!?"), }, _ => { is_str_ref = false; quote_spanned!(arg_type.span()=> - args[#i].downcast_clone::<#arg_type>().unwrap()) + std::mem::take(args[#i]).clone().cast::<#arg_type>()) } }; @@ -768,7 +767,7 @@ mod generate_tests { format!("wrong arg count: {} != {}", args.len(), 1usize), Position::none()))); } - let arg0 = args[0usize].downcast_clone::().unwrap(); + let arg0 = std::mem::take(args[0usize]).clone().cast::(); Ok(Dynamic::from(do_something(arg0))) } @@ -808,7 +807,7 @@ mod generate_tests { format!("wrong arg count: {} != {}", args.len(), 1usize), Position::none()))); } - let arg0 = args[0usize].downcast_clone::().unwrap(); + let arg0 = std::mem::take(args[0usize]).clone().cast::(); Ok(Dynamic::from(do_something(arg0))) } @@ -845,8 +844,8 @@ mod generate_tests { format!("wrong arg count: {} != {}", args.len(), 2usize), Position::none()))); } - let arg0 = args[0usize].downcast_clone::().unwrap(); - let arg1 = args[1usize].downcast_clone::().unwrap(); + let arg0 = std::mem::take(args[0usize]).clone().cast::(); + let arg1 = std::mem::take(args[1usize]).clone().cast::(); Ok(Dynamic::from(add_together(arg0, arg1))) } @@ -891,7 +890,7 @@ mod generate_tests { format!("wrong arg count: {} != {}", args.len(), 2usize), Position::none()))); } - let arg1 = args[1usize].downcast_clone::().unwrap(); + let arg1 = std::mem::take(args[1usize]).clone().cast::(); let arg0: &mut _ = &mut args[0usize].write_lock::().unwrap(); Ok(Dynamic::from(increment(arg0, arg1))) } @@ -938,7 +937,7 @@ mod generate_tests { format!("wrong arg count: {} != {}", args.len(), 1usize), Position::none()))); } - let arg0 = args[0usize].downcast_clone::().unwrap(); + let arg0 = std::mem::take(args[0usize]).clone().cast::(); Ok(Dynamic::from(special_print(&arg0))) } diff --git a/codegen/src/module.rs b/codegen/src/module.rs index 9db6a3cd..256ca062 100644 --- a/codegen/src/module.rs +++ b/codegen/src/module.rs @@ -371,7 +371,7 @@ mod generate_tests { format!("wrong arg count: {} != {}", args.len(), 1usize), Position::none()))); } - let arg0 = args[0usize].downcast_clone::().unwrap(); + let arg0 = std::mem::take(args[0usize]).clone().cast::(); Ok(Dynamic::from(add_one_to(arg0))) } @@ -433,8 +433,8 @@ mod generate_tests { format!("wrong arg count: {} != {}", args.len(), 2usize), Position::none()))); } - let arg0 = args[0usize].downcast_clone::().unwrap(); - let arg1 = args[1usize].downcast_clone::().unwrap(); + let arg0 = std::mem::take(args[0usize]).clone().cast::(); + let arg1 = std::mem::take(args[1usize]).clone().cast::(); Ok(Dynamic::from(add_together(arg0, arg1))) } @@ -605,7 +605,7 @@ mod generate_tests { format!("wrong arg count: {} != {}", args.len(), 1usize), Position::none()))); } - let arg0 = args[0usize].downcast_clone::().unwrap(); + let arg0 = std::mem::take(args[0usize]).clone().cast::(); Ok(Dynamic::from(print_out_to(&arg0))) } From 59e3ca0e79225463e9314f96e846a076ac3494d4 Mon Sep 17 00:00:00 2001 From: J Henry Waugh Date: Tue, 11 Aug 2020 19:09:49 -0500 Subject: [PATCH 2/2] Remove Dynamic::downcast_clone --- src/any.rs | 7 ------- src/fn_register.rs | 8 ++++---- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/src/any.rs b/src/any.rs index afbfc956..c154a682 100644 --- a/src/any.rs +++ b/src/any.rs @@ -1082,13 +1082,6 @@ impl Dynamic { } } - /// Copy and return a `Dynamic` if it contains a type that can be trivially copied. - /// Returns `None` if the cast fails. - #[inline(always)] - pub fn downcast_clone(&self) -> Option { - self.downcast_ref::().map(|t| t.clone()) - } - /// Cast the `Dynamic` as the system integer type `INT` and return it. /// Returns the name of the actual type if the cast fails. #[inline(always)] diff --git a/src/fn_register.rs b/src/fn_register.rs index 9645fba9..28d33198 100644 --- a/src/fn_register.rs +++ b/src/fn_register.rs @@ -47,10 +47,10 @@ pub trait RegisterPlugin { /// fn is_varadic(&self) -> bool { false } /// /// fn call(&self, args: &mut[&mut Dynamic], pos: Position) -> Result> { - /// let x1: NUMBER = args[0].downcast_clone::().unwrap(); - /// let y1: NUMBER = args[1].downcast_clone::().unwrap(); - /// let x2: NUMBER = args[2].downcast_clone::().unwrap(); - /// let y2: NUMBER = args[3].downcast_clone::().unwrap(); + /// let x1: NUMBER = std::mem::take(args[0]).clone().cast::(); + /// let y1: NUMBER = std::mem::take(args[1]).clone().cast::(); + /// let x2: NUMBER = std::mem::take(args[2]).clone().cast::(); + /// let y2: NUMBER = std::mem::take(args[3]).clone().cast::(); /// # #[cfg(not(feature = "no_float"))] /// let square_sum = (y2 - y1).abs().powf(2.0) + (x2 -x1).abs().powf(2.0); /// # #[cfg(feature = "no_float")]