codegen: replace downcast_clone with more efficient mem::take

This commit is contained in:
J Henry Waugh 2020-08-11 19:05:52 -05:00
parent 7506aee5c6
commit 6d11fdcd18
2 changed files with 13 additions and 14 deletions

View File

@ -356,16 +356,15 @@ impl ExportedFn {
&syn::Type::Path(ref p) if p.path == str_type_path => { &syn::Type::Path(ref p) if p.path == str_type_path => {
is_str_ref = true; is_str_ref = true;
quote_spanned!(arg_type.span()=> quote_spanned!(arg_type.span()=>
args[#i] std::mem::take(args[#i])
.downcast_clone::<ImmutableString>() .clone().cast::<ImmutableString>())
.unwrap())
} }
_ => panic!("internal error: why wasn't this found earlier!?"), _ => panic!("internal error: why wasn't this found earlier!?"),
}, },
_ => { _ => {
is_str_ref = false; is_str_ref = false;
quote_spanned!(arg_type.span()=> 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: {} != {}", format!("wrong arg count: {} != {}",
args.len(), 1usize), Position::none()))); args.len(), 1usize), Position::none())));
} }
let arg0 = args[0usize].downcast_clone::<usize>().unwrap(); let arg0 = std::mem::take(args[0usize]).clone().cast::<usize>();
Ok(Dynamic::from(do_something(arg0))) Ok(Dynamic::from(do_something(arg0)))
} }
@ -808,7 +807,7 @@ mod generate_tests {
format!("wrong arg count: {} != {}", format!("wrong arg count: {} != {}",
args.len(), 1usize), Position::none()))); args.len(), 1usize), Position::none())));
} }
let arg0 = args[0usize].downcast_clone::<usize>().unwrap(); let arg0 = std::mem::take(args[0usize]).clone().cast::<usize>();
Ok(Dynamic::from(do_something(arg0))) Ok(Dynamic::from(do_something(arg0)))
} }
@ -845,8 +844,8 @@ mod generate_tests {
format!("wrong arg count: {} != {}", format!("wrong arg count: {} != {}",
args.len(), 2usize), Position::none()))); args.len(), 2usize), Position::none())));
} }
let arg0 = args[0usize].downcast_clone::<usize>().unwrap(); let arg0 = std::mem::take(args[0usize]).clone().cast::<usize>();
let arg1 = args[1usize].downcast_clone::<usize>().unwrap(); let arg1 = std::mem::take(args[1usize]).clone().cast::<usize>();
Ok(Dynamic::from(add_together(arg0, arg1))) Ok(Dynamic::from(add_together(arg0, arg1)))
} }
@ -891,7 +890,7 @@ mod generate_tests {
format!("wrong arg count: {} != {}", format!("wrong arg count: {} != {}",
args.len(), 2usize), Position::none()))); args.len(), 2usize), Position::none())));
} }
let arg1 = args[1usize].downcast_clone::<usize>().unwrap(); let arg1 = std::mem::take(args[1usize]).clone().cast::<usize>();
let arg0: &mut _ = &mut args[0usize].write_lock::<usize>().unwrap(); let arg0: &mut _ = &mut args[0usize].write_lock::<usize>().unwrap();
Ok(Dynamic::from(increment(arg0, arg1))) Ok(Dynamic::from(increment(arg0, arg1)))
} }
@ -938,7 +937,7 @@ mod generate_tests {
format!("wrong arg count: {} != {}", format!("wrong arg count: {} != {}",
args.len(), 1usize), Position::none()))); args.len(), 1usize), Position::none())));
} }
let arg0 = args[0usize].downcast_clone::<ImmutableString>().unwrap(); let arg0 = std::mem::take(args[0usize]).clone().cast::<ImmutableString>();
Ok(Dynamic::from(special_print(&arg0))) Ok(Dynamic::from(special_print(&arg0)))
} }

View File

@ -371,7 +371,7 @@ mod generate_tests {
format!("wrong arg count: {} != {}", format!("wrong arg count: {} != {}",
args.len(), 1usize), Position::none()))); args.len(), 1usize), Position::none())));
} }
let arg0 = args[0usize].downcast_clone::<INT>().unwrap(); let arg0 = std::mem::take(args[0usize]).clone().cast::<INT>();
Ok(Dynamic::from(add_one_to(arg0))) Ok(Dynamic::from(add_one_to(arg0)))
} }
@ -433,8 +433,8 @@ mod generate_tests {
format!("wrong arg count: {} != {}", format!("wrong arg count: {} != {}",
args.len(), 2usize), Position::none()))); args.len(), 2usize), Position::none())));
} }
let arg0 = args[0usize].downcast_clone::<INT>().unwrap(); let arg0 = std::mem::take(args[0usize]).clone().cast::<INT>();
let arg1 = args[1usize].downcast_clone::<INT>().unwrap(); let arg1 = std::mem::take(args[1usize]).clone().cast::<INT>();
Ok(Dynamic::from(add_together(arg0, arg1))) Ok(Dynamic::from(add_together(arg0, arg1)))
} }
@ -605,7 +605,7 @@ mod generate_tests {
format!("wrong arg count: {} != {}", format!("wrong arg count: {} != {}",
args.len(), 1usize), Position::none()))); args.len(), 1usize), Position::none())));
} }
let arg0 = args[0usize].downcast_clone::<ImmutableString>().unwrap(); let arg0 = std::mem::take(args[0usize]).clone().cast::<ImmutableString>();
Ok(Dynamic::from(print_out_to(&arg0))) Ok(Dynamic::from(print_out_to(&arg0)))
} }