clonable_trait_object 0.1.1

clonar objectos traits sin usar unsafe
Documentation

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();
}