Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trait Animal {
- fn speak(&self);
- }
- struct Dog {
- name: String,
- }
- impl Dog {
- fn new(name: &str) -> Dog {
- return Dog {
- name: name.to_string(),
- };
- }
- }
- impl Animal for Dog {
- fn speak(&self) {
- println!{"{}: ruff, ruff!", self.name};
- }
- }
- struct AnimalHouse {
- animal: Box<Animal>,
- }
- fn main() {
- let house = AnimalHouse {
- animal: Box::new(Dog::new("Bobby")),
- };
- house.animal.speak();
- }
- fn main() {
- let house = AnimalHouse {
- animal: Box::new(Dog::new("Bobby")),
- };
- let house2 = house.clone();
- house2.animal.speak();
- }
- trait Animal: Clone {
- /* ... */
- }
- trait Animal: AnimalClone {
- fn speak(&self);
- }
- // Splitting AnimalClone into its own trait allows us to provide a blanket
- // implementation for all compatible types, without having to implement the
- // rest of Animal. In this case, we implement it for all types that have
- // 'static lifetime (*i.e.* they don't contain non-'static pointers), and
- // implement both Animal and Clone. Don't ask me how the compiler resolves
- // implementing AnimalClone for Animal when Animal requires AnimalClone; I
- // have *no* idea why this works.
- trait AnimalClone {
- fn clone_box(&self) -> Box<Animal>;
- }
- impl<T> AnimalClone for T
- where
- T: 'static + Animal + Clone,
- {
- fn clone_box(&self) -> Box<Animal> {
- Box::new(self.clone())
- }
- }
- // We can now implement Clone manually by forwarding to clone_box.
- impl Clone for Box<Animal> {
- fn clone(&self) -> Box<Animal> {
- self.clone_box()
- }
- }
- #[derive(Clone)]
- 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<Animal>,
- }
- fn main() {
- let house = AnimalHouse {
- animal: Box::new(Dog::new("Bobby")),
- };
- let house2 = house.clone();
- house2.animal.speak();
- }
- #[macro_use] extern crate objekt;
- trait Animal: objekt::Clone {
- fn speak(&self);
- }
- clone_trait_object!(Animal);
- #[derive(Clone)]
- struct Dog {
- name: String,
- }
- impl Dog {
- fn new(name: &str) -> Dog {
- Dog { name: name.to_owned() }
- }
- }
- impl Animal for Dog {
- fn speak(&self) {
- println!{"{}: ruff, ruff!", self.name};
- }
- }
- #[derive(Clone)]
- struct AnimalHouse {
- animal: Box<Animal>,
- }
- fn main() {
- let house = AnimalHouse {
- animal: Box::new(Dog::new("Bobby")),
- };
- let house2 = house.clone();
- house2.animal.speak();
- }
- #[derive(Clone)]
- struct AnimalHouse {
- animal: Rc<Animal>,
- }
- fn main() {
- let house = AnimalHouse { animal: Rc::new(Dog::new("Bobby")) };
- let house2 = house.clone();
- house2.animal.speak();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement