Guest User

Untitled

a guest
Dec 10th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. use std::rc::{Rc, Weak};
  2. use std::cell::RefCell;
  3. use std::fmt;
  4.  
  5. type DepReference = Rc<RefCell<Box<Dependency>>>;
  6. type DepWeak = Weak<RefCell<Box<Dependency>>>;
  7.  
  8. trait Dependency {
  9. fn do_it(&mut self, i: &mut i32);
  10. fn name(&self) -> String;
  11. }
  12.  
  13. impl fmt::Debug for Dependency {
  14. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  15. write!(f, "Dependency<{}>", self.name())
  16. }
  17. }
  18.  
  19. struct Dependent {
  20. data: i32,
  21. dependency: DepWeak
  22. }
  23.  
  24. impl fmt::Debug for Dependent {
  25. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  26. write!(f, "Dependent{{ data: {}, dependency: {:?} }}",
  27. self.data, self.dependency)
  28. }
  29. }
  30.  
  31. impl Dependent {
  32. fn new(i: i32, dependency: DepWeak) -> Self {
  33. Dependent {
  34. data: i,
  35. dependency
  36. }
  37. }
  38.  
  39. fn do_it(&mut self) {
  40. let rc: DepReference = self.dependency.upgrade().unwrap();
  41. (*rc).borrow_mut().do_it(&mut self.data);
  42. }
  43. }
  44.  
  45. impl Drop for Dependent {
  46. fn drop(&mut self) {
  47. println!("Drop {:?}!", self);
  48. }
  49. }
  50.  
  51. struct DependencyA {
  52. info: i32
  53. }
  54.  
  55. struct DependencyB {
  56. info: i32
  57. }
  58.  
  59. impl Dependency for DependencyA {
  60. fn do_it(&mut self, i: &mut i32) {
  61. self.info += 1;
  62. *i += self.info;
  63. }
  64. fn name(&self) -> String {
  65. "DependencyA".to_string()
  66. }
  67. }
  68.  
  69. impl Drop for DependencyA {
  70. fn drop(&mut self) {
  71. println!("Drop DependencyA!");
  72. }
  73. }
  74.  
  75. impl Dependency for DependencyB {
  76. fn do_it(&mut self, i: &mut i32) {
  77. self.info += 7;
  78. *i += self.info;
  79. }
  80. fn name(&self) -> String {
  81. "DependencyB".to_string()
  82. }
  83. }
  84.  
  85. impl Drop for DependencyB {
  86. fn drop(&mut self) {
  87. println!("Drop DependencyB!");
  88. }
  89. }
  90.  
  91. struct Container {
  92. data: Vec<DepReference>,
  93. content: Vec<Dependent>
  94. }
  95.  
  96. impl Container {
  97. fn new() -> Container {
  98. let data: Vec<DepReference> = Vec::new();
  99. let content: Vec<Dependent> = Vec::new();
  100. Container { data, content }
  101. }
  102.  
  103. fn add(&mut self, d: Box<Dependency>, i: i32) {
  104. let dp = Rc::new(RefCell::new(d));
  105. self.content.push(Dependent::new(i, Rc::downgrade(&dp)));
  106. self.data.push(dp);
  107. }
  108.  
  109. fn do_it(&mut self) {
  110. for item in &mut self.content {
  111. item.do_it();
  112. }
  113. }
  114. }
  115.  
  116. impl Drop for Container {
  117. fn drop(&mut self) {
  118. println!("Drop Container!");
  119. }
  120. }
  121.  
  122.  
  123. fn main() {
  124. let mut c = Container::new();
  125. c.add(Box::new(DependencyA{ info: 666 }), 25);
  126. c.add(Box::new(DependencyB{ info: 777 }), 37);
  127. c.do_it();
  128. }
Add Comment
Please, Sign In to add comment