Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::rc::Rc;
- use std::cell::Cell;
- struct Foo {
- bar: Cell<Option<Rc<Bar>>>,
- magic: Cell<usize>,
- }
- impl Foo {
- fn foo(&self) {
- self.magic.set(self.magic.get() + 1);
- if let Some(bar) = self.bar.take() {
- bar.doit();
- self.bar.set(Some(bar));
- }
- self.magic.set(self.magic.get() + 1);
- }
- fn wee(&self) {
- assert_eq!(self.magic.get() % 2, 0);
- }
- }
- struct Bar {
- foo: Rc<Foo>,
- }
- impl Bar {
- fn doit(&self) {
- self.foo.wee();
- }
- }
- fn main() {
- let foo = Rc::new(Foo { bar: Cell::new(None), magic: Cell::new(0) });
- let bar = Rc::new(Bar { foo: foo.clone() });
- foo.bar.set(Some(bar.clone()));
- foo.foo();
- }
Add Comment
Please, Sign In to add comment