1

I am writing a lexer and want to advance/backup a cursor using RAII helpers. I have a string that is indexed by the CharStreamGuard type that acts as the cursor. It is created at the top-level with new or extended from a parent guard using extend. I believe the following code models the lifetimes correctly: 'c is a sub-lifetime of 'b, but the compiler still gives

error[E0597]: `csg` does not live long enough
  --> src/lib.rs:78:48
   |
76 |     let mut csg = CharStreamGuard::new(&s);
   |         ------- binding `csg` declared here
77 |     {
78 |         let mut csg2 = CharStreamGuard::extend(&mut csg);
   |                                                ^^^^^^^^ borrowed value does not live long enough
...
82 | }
   | -
   | |
   | `csg` dropped here while still borrowed
   | borrow might be used here, when `csg` is dropped and runs the `Drop` code for type `CharStreamGuard`

error[E0499]: cannot borrow `csg` as mutable more than once at a time
  --> src/lib.rs:81:5
   |
78 |         let mut csg2 = CharStreamGuard::extend(&mut csg);
   |                                                -------- first mutable borrow occurs here
...
81 |     csg.accept();
   |     ^^^
   |     |
   |     second mutable borrow occurs here
   |     first borrow later used here

For the code:

#[derive(PartialEq)]
enum GuardStatus {
    Unset,
    Accepted,
    Rejected,
}

pub struct CharStreamGuard<'a, 'b> {
    src: &'a String,
    parent: Option<&'b mut CharStreamGuard<'a, 'b>>,
    index: usize,
    status: GuardStatus
}

impl<'a,'b> CharStreamGuard<'a,'b> {

    fn new(src: &'a String) -> CharStreamGuard<'a,'b> {
        return CharStreamGuard {
                src: src,
                parent: None,
                index: 0,
                status: GuardStatus::Unset
        };
    }

    fn extend<'c>(parent: &'c mut CharStreamGuard<'a,'b>) -> CharStreamGuard<'a,'c>
        where 'c : 'b
    {
        let index = parent.index;
        return CharStreamGuard {
                src: parent.src,
                parent: Some(parent),
                index: index,
                status: GuardStatus::Unset
        };
    }

    fn accept(&mut self) {
        self.status = GuardStatus::Accepted;
        if let Some(parent) = self.parent.as_mut() {
            parent.index = self.index;
        }
    }

    fn reject(&mut self) {
        self.status = GuardStatus::Rejected;
    }


}

impl Drop for CharStreamGuard<'_,'_> {
    fn drop(&mut self) -> () {
        if self.status == GuardStatus::Unset {
            panic!("guard was neither accpeted nor rejected!");
        }
    }
}

fn test_it() {
    let s = String::new();
    let mut csg = CharStreamGuard::new(&s);
    {
        let mut csg2 = CharStreamGuard::extend(&mut csg);
        csg2.accept();
    }
    csg.accept();
}
user1055947
  • 862
  • 3
  • 9
  • 22
  • Does this answer your question? [Why does this mutable borrow live beyond its scope?](/q/66252831/2189130) – kmdreko Aug 12 '23 at 23:52
  • Thanks @kmdreko. I read your post there and skimmed the rustonomican article on subtyping and variance. I am getting a better feel of it. – user1055947 Aug 13 '23 at 02:08

1 Answers1

0

A working solution, using Rc<RefCell<T>>:


#[derive(PartialEq)]
enum GuardStatus {
    Unset,
    Accepted,
    Rejected,
}

pub struct CharStreamGuard<'a> {
    src: &'a String,
    // parent: Option<&'b mut CharStreamGuard<'a, 'b>>,
    parent: Option<Rc<RefCell<CharStreamGuard<'a>>>>,
    index: usize,
    status: GuardStatus
}

impl<'a> CharStreamGuard<'a> {

    fn new(src: &'a String) -> CharStreamGuard<'a> {
        return CharStreamGuard {
                src: src,
                parent: None,
                index: 0,
                status: GuardStatus::Unset
        };
    }

    fn extend(parent: Rc<RefCell<CharStreamGuard<'a>>>) -> CharStreamGuard<'a>
    {
        let index = parent.borrow().index;
        let src = parent.borrow().src;
        return CharStreamGuard {
                src: src,
                parent: Some(parent),
                index: index,
                status: GuardStatus::Unset
        };
    }

    fn accept(&mut self) {
        self.status = GuardStatus::Accepted;
        if let Some(parent) = self.parent.as_mut() {
            parent.borrow_mut().index = self.index;
        }
    }

    fn reject(&mut self) {
        self.status = GuardStatus::Rejected;
    }

}

impl Drop for CharStreamGuard<'_> {
    fn drop(&mut self) -> () {
        if self.status == GuardStatus::Unset {
            panic!("guard was neither accpeted nor rejected!");
        }
    }
}

fn test_it() {
    let s = String::new();
    let mut csg = Rc::new(RefCell::new(CharStreamGuard::new(&s)));
    {
        let mut csg2 = CharStreamGuard::extend(csg.clone());
        csg2.accept();
    }
    csg.borrow_mut().accept();
}

user1055947
  • 862
  • 3
  • 9
  • 22