Guest User

Untitled

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