Guest User

Untitled

a guest
Jun 18th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. use std::sync::Arc;
  2.  
  3. trait Foo {
  4. fn get_bars(&mut self) -> Vec<&mut Arc<Bar>>;
  5. // Solution!
  6. // fn do_stuff_on_bars(&mut self);
  7. }
  8.  
  9. struct Fooy {
  10. list: Vec<Arc<Bar>>
  11. }
  12. impl Foo for Fooy {
  13. fn get_bars(&mut self) -> Vec<&mut Arc<Bar>> {
  14. self.list.iter_mut().collect()
  15. }
  16. // Solution!
  17. // fn do_stuff_on_bars(&mut self) {
  18. // for mut bar in self.list.iter_mut() {
  19. // Arc::get_mut(&mut bar).unwrap().stuff();
  20. // }
  21. // }
  22. }
  23.  
  24. struct Bar {}
  25. impl Bar {
  26. fn stuff(&mut self) { /* do mut things.. */ }
  27. }
  28.  
  29. struct App<'a> {
  30. foos: Vec<Box<Foo>>,
  31. bars: Vec<&'a mut Arc<Bar>>
  32. }
  33. impl<'a> App<'a> {
  34. fn load_foo(&mut self, f: Box<Foo>) {
  35. // self.bars.push_all_as_refs_by_magic(f.get_bars());
  36. self.foos.push(f);
  37. }
  38. fn dream_about_doing_stuff(&mut self) {
  39. for mut bar in self.bars.iter_mut() {
  40. Arc::get_mut(&mut bar).unwrap().stuff();
  41. }
  42. }
  43. // Solution!
  44. // fn do_stuff(&mut self) {
  45. // for foo in self.foos.iter_mut() {
  46. // foo.do_stuff_on_bars();
  47. // }
  48. // }
  49. }
  50.  
  51. fn main() {
  52. let mut a = App {
  53. foos: Vec::new(),
  54. bars: Vec::new(),
  55. };
  56.  
  57. a.load_foo(Box::new(Fooy {
  58. list: vec![Arc::new(Bar{}), Arc::new(Bar{})],
  59. }));
  60.  
  61. a.dream_about_doing_stuff();
  62. // Solution!
  63. // a.do_stuff();
  64.  
  65. println!("Done!");
  66. }
Add Comment
Please, Sign In to add comment