Guest User

Untitled

a guest
Dec 14th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. use std::rc::Rc;
  2. use std::cell::Cell;
  3.  
  4. struct Foo {
  5. bar: Cell<Option<Rc<Bar>>>,
  6. magic: Cell<usize>,
  7. }
  8.  
  9. impl Foo {
  10. fn foo(&self) {
  11. self.magic.set(self.magic.get() + 1);
  12. if let Some(bar) = self.bar.take() {
  13. bar.doit();
  14. self.bar.set(Some(bar));
  15. }
  16. self.magic.set(self.magic.get() + 1);
  17. }
  18.  
  19. fn wee(&self) {
  20. assert_eq!(self.magic.get() % 2, 0);
  21. }
  22. }
  23.  
  24. struct Bar {
  25. foo: Rc<Foo>,
  26. }
  27.  
  28. impl Bar {
  29. fn doit(&self) {
  30. self.foo.wee();
  31. }
  32. }
  33.  
  34. fn main() {
  35. let foo = Rc::new(Foo { bar: Cell::new(None), magic: Cell::new(0) });
  36. let bar = Rc::new(Bar { foo: foo.clone() });
  37. foo.bar.set(Some(bar.clone()));
  38. foo.foo();
  39. }
Add Comment
Please, Sign In to add comment