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: usize
- }
- impl Foo {
- fn foo(&mut self) {
- self.magic +=1;
- let tempvalue = self.bar.take();
- tempvalue.unwrap().doit();
- self.bar.set(tempvalue);
- self.magic +=1;
- }
- fn wee(&self) {
- assert_eq!(self.magic %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: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