From: Ian Jackson Date: Mon, 5 Jun 2023 16:14:45 +0000 (+0100) Subject: less cloning X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=99a7c7460b26eec2855e73b8cd9376000aaaf485;p=rust-experiments.git less cloning --- diff --git a/src/main.rs b/src/main.rs index 13d9400..1c4ea5d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ use std::sync::Arc; use downcast_rs::{DowncastSync, impl_downcast}; pub trait HasPseudoMethods: DowncastSync { - fn cast_to_instance(&self, arc: Arc, tp: TypeId) -> Option>; + fn cast_to_instance(&self, arc: &Arc, tp: TypeId) -> Option>; } impl_downcast!(sync HasPseudoMethods); /* @@ -45,11 +45,11 @@ impl HasPseudoMethods for Frog { // Note: if you wanted to be able to add more traits to Frog elsewhere, you would // need a registry of cast-to-trait objects, similar to the existing dispatch // table. I'm not sure if that's needed. - fn cast_to_instance(&self, arc: Arc, tp: TypeId) -> Option> { + fn cast_to_instance(&self, arc: &Arc, tp: TypeId) -> Option> { if tp == TypeId::of::>() { - let arc: Arc = DowncastSync::into_any_arc(arc); + let arc: Arc = DowncastSync::into_any_arc(arc.clone()); let arc: Arc = Arc::downcast::(arc).unwrap(); - let arc: Arc = arc.clone(); + let arc: Arc = arc as _; return Some(Box::new(arc)); /* } else if tp == TypeId::of::>() { let arc: Arc = self.clone(); @@ -65,7 +65,7 @@ fn main() { let erased_frog: Arc = frog as _; let with_feet: Box = erased_frog.cast_to_instance( - erased_frog.clone(), + &erased_frog, TypeId::of::>(), ).unwrap();