#unsafe #traits #object #clone #clonable #sin #clonar

clonable_trait_object

clonar objectos traits sin usar unsafe

2 releases

0.1.1 Feb 7, 2024
0.1.0 Feb 7, 2024

#1 in #clonable

MIT license

3KB

clonar objectos traits sin usar unsafe


use clonable_trait_object::clonable_trait_object;

/*El primer parametro es el trait objetivo
el segundo parametro es el nombre que le daras al trait 
que hara de Clone*/ 
clonable_trait_object!(Animal,AnimalClone);
trait Animal: AnimalClone {
 fn speak(&self);
}
#[derive(Clone,Debug)]
struct Dog {
 name: String,
}
impl Dog {
 fn new(name: &str) -> Dog {
     Dog {
         name: name.to_string(),
     }
 }
}
impl Animal for Dog {
 fn speak(&self) {
     println!("{}: ruff, ruff!", self.name);
 }
}
#[derive(Clone)]
struct AnimalHouse {
 animal: Box<dyn Animal>,
}
fn main() {
 let house = AnimalHouse {
     animal: Box::new(Dog::new("Bobby")),
 };
 let house2 = house.clone();
 house2.animal.speak();
}

No runtime deps