Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. trait Animal {
  2. fn speak(&self);
  3. }
  4.  
  5. struct Dog {
  6. name: String,
  7. }
  8.  
  9. impl Dog {
  10. fn new(name: &str) -> Dog {
  11. return Dog {
  12. name: name.to_string(),
  13. };
  14. }
  15. }
  16.  
  17. impl Animal for Dog {
  18. fn speak(&self) {
  19. println!{"{}: ruff, ruff!", self.name};
  20. }
  21. }
  22.  
  23. struct AnimalHouse {
  24. animal: Box<Animal>,
  25. }
  26.  
  27. fn main() {
  28. let house = AnimalHouse {
  29. animal: Box::new(Dog::new("Bobby")),
  30. };
  31. house.animal.speak();
  32. }
  33.  
  34. fn main() {
  35. let house = AnimalHouse {
  36. animal: Box::new(Dog::new("Bobby")),
  37. };
  38. let house2 = house.clone();
  39. house2.animal.speak();
  40. }
  41.  
  42. trait Animal: Clone {
  43. /* ... */
  44. }
  45.  
  46. trait Animal: AnimalClone {
  47. fn speak(&self);
  48. }
  49.  
  50. // Splitting AnimalClone into its own trait allows us to provide a blanket
  51. // implementation for all compatible types, without having to implement the
  52. // rest of Animal. In this case, we implement it for all types that have
  53. // 'static lifetime (*i.e.* they don't contain non-'static pointers), and
  54. // implement both Animal and Clone. Don't ask me how the compiler resolves
  55. // implementing AnimalClone for Animal when Animal requires AnimalClone; I
  56. // have *no* idea why this works.
  57. trait AnimalClone {
  58. fn clone_box(&self) -> Box<Animal>;
  59. }
  60.  
  61. impl<T> AnimalClone for T
  62. where
  63. T: 'static + Animal + Clone,
  64. {
  65. fn clone_box(&self) -> Box<Animal> {
  66. Box::new(self.clone())
  67. }
  68. }
  69.  
  70. // We can now implement Clone manually by forwarding to clone_box.
  71. impl Clone for Box<Animal> {
  72. fn clone(&self) -> Box<Animal> {
  73. self.clone_box()
  74. }
  75. }
  76.  
  77. #[derive(Clone)]
  78. struct Dog {
  79. name: String,
  80. }
  81.  
  82. impl Dog {
  83. fn new(name: &str) -> Dog {
  84. Dog {
  85. name: name.to_string(),
  86. }
  87. }
  88. }
  89.  
  90. impl Animal for Dog {
  91. fn speak(&self) {
  92. println!("{}: ruff, ruff!", self.name);
  93. }
  94. }
  95.  
  96. #[derive(Clone)]
  97. struct AnimalHouse {
  98. animal: Box<Animal>,
  99. }
  100.  
  101. fn main() {
  102. let house = AnimalHouse {
  103. animal: Box::new(Dog::new("Bobby")),
  104. };
  105. let house2 = house.clone();
  106. house2.animal.speak();
  107. }
  108.  
  109. #[macro_use] extern crate objekt;
  110.  
  111. trait Animal: objekt::Clone {
  112. fn speak(&self);
  113. }
  114.  
  115. clone_trait_object!(Animal);
  116.  
  117. #[derive(Clone)]
  118. struct Dog {
  119. name: String,
  120. }
  121.  
  122. impl Dog {
  123. fn new(name: &str) -> Dog {
  124. Dog { name: name.to_owned() }
  125. }
  126. }
  127.  
  128. impl Animal for Dog {
  129. fn speak(&self) {
  130. println!{"{}: ruff, ruff!", self.name};
  131. }
  132. }
  133.  
  134. #[derive(Clone)]
  135. struct AnimalHouse {
  136. animal: Box<Animal>,
  137. }
  138.  
  139. fn main() {
  140. let house = AnimalHouse {
  141. animal: Box::new(Dog::new("Bobby")),
  142. };
  143. let house2 = house.clone();
  144. house2.animal.speak();
  145. }
  146.  
  147. #[derive(Clone)]
  148. struct AnimalHouse {
  149. animal: Rc<Animal>,
  150. }
  151.  
  152. fn main() {
  153. let house = AnimalHouse { animal: Rc::new(Dog::new("Bobby")) };
  154. let house2 = house.clone();
  155. house2.animal.speak();
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement