Guest User

Untitled

a guest
Apr 24th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. use std::cell::RefCell;
  2. use std::rc::Rc;
  3. //use std::ops::{Deref, DerefMut};
  4. use std::clone::Clone;
  5.  
  6. struct SimContext {
  7. modules: Vec<Module<ModuleInterface>>
  8. }
  9.  
  10.  
  11. impl SimContext {
  12. fn new() -> Self {
  13. SimContext {
  14. modules: Vec::new()
  15. }
  16. }
  17. }
  18.  
  19. trait ModuleInterface {
  20. fn get_submodules(&mut self) -> Vec<Module<ModuleInterface>> {
  21. Vec::new()
  22. }
  23. }
  24.  
  25. trait Elaborate: ModuleInterface + Sized {
  26. fn elaborate() -> Module<Self>;
  27. }
  28.  
  29.  
  30. #[derive(Debug)]
  31. struct Module<T>(Rc<RefCell<T>>) where T: ModuleInterface + ?Sized;
  32.  
  33. impl<T> Module<T> where T: ModuleInterface + Sized {
  34. fn new(t: T) -> Self {
  35. Module(Rc::new(RefCell::new(t)))
  36. }
  37. }
  38.  
  39. impl<T> Clone for Module<T> where T: ModuleInterface + ?Sized {
  40. fn clone(&self) -> Module<T> {
  41. Module(self.0.clone())
  42. }
  43. }
  44.  
  45. impl<T> ModuleInterface for Module<T> where T: ModuleInterface {
  46. fn get_submodules(&mut self) -> Vec<Module<ModuleInterface>> {
  47. (*self.0).borrow_mut().get_submodules()
  48. }
  49. }
  50.  
  51. impl<T> From<Module<T>> for Module<ModuleInterface> where T: ModuleInterface + 'static {
  52. fn from(t: Module<T>) -> Module<ModuleInterface> {
  53. Module(t.0)
  54. }
  55. }
  56.  
  57. ///// ----- Tests
  58.  
  59. struct T2 {
  60. }
  61. impl ModuleInterface for T2 {}
  62. impl Elaborate for T2 {
  63. fn elaborate() -> Module<Self> {
  64. Module::new(T2 {})
  65. }
  66. }
  67.  
  68.  
  69. struct T1 {
  70. sub: Module<T2>,
  71. id: usize,
  72. }
  73.  
  74. impl ModuleInterface for T1 {
  75. fn get_submodules(&mut self) -> Vec<Module<ModuleInterface>> {
  76. let mut local = Vec::new();
  77. {
  78. let mut vec = self.sub.get_submodules();
  79. local.append(&mut vec);
  80. local.push(self.sub.clone().into());
  81. }
  82. local
  83. }
  84. }
  85. impl Elaborate for T1 {
  86. fn elaborate() -> Module<Self> {
  87. let m = T1 {
  88. sub: Elaborate::elaborate(),
  89. id: 0
  90. };
  91. Module::new(m)
  92. }
  93. }
  94.  
  95. fn main() {
  96.  
  97. let mut simctx = SimContext::new();
  98. let mut b = T1::elaborate();
  99. println!("{}", b.get_submodules().len());
  100. simctx.modules.append(&mut b.get_submodules());
  101. simctx.modules.push(b.clone().into());
  102. println!("{}", (*b.0).borrow().id);
  103. println!("{}", (*b.0).borrow().id);
  104.  
  105. println!("{}", simctx.modules.len());
  106.  
  107. }
  108.  
  109.  
  110. //////////////// ---------------------------------------------------------------
  111. /*
  112. trait Dispatcher {
  113. fn dispatch(&self);
  114. }
  115.  
  116. struct MethodDispatcher<T> {
  117. object: Rc<RefCell<T>>,
  118. method: Box<Fn (&mut T)>
  119. }
  120.  
  121. impl<T> Dispatcher for MethodDispatcher<T> {
  122.  
  123. fn dispatch(&self) {
  124. let mut object = self.object.borrow_mut();
  125. (*self.method)(&mut object);
  126. }
  127. }
  128. */
Add Comment
Please, Sign In to add comment