Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
587 views
in Technique[技术] by (71.8m points)

reference - Extending borrow lifetimes in rust

I'm trying to parse a series to tokentrees, but when I try to implement my parsing trait I get an error related to reference lifetimes. I thought creating a boxed version would move around any issues with reference counts or lifetimes. The code is as follows.

impl Parse for TokenTree {
    fn parse(&mut self) -> Tree {
        match self.clone() {
            TtDelimited(_, y) => {
                let mut y2 = box (*y).clone();
                match y2.delim {
                    token::DelimToken::Paren => y2.parse(),
                    _ => panic!("not done yet"),
                }
            }
            TtToken(_, t) => E(t),
            _ => panic!("not done yet"),
        }
    }
}

the errors I get make the issue clear, but I can't find any information on solving this particular issue.

35:51 error: `*y2` does not live long enough
                     token::DelimToken::Paren => y2.parse(),
                                                       ^~
42:6 note: reference must be valid for the anonymous lifetime #1 defined on the block at 30:31...
 fn parse(&mut self) -> Tree{ 
     match self.clone(){
         TtDelimited(_, y) => {
             let mut y2 = box () (*y).clone();
             match y2.delim{
                 token::DelimToken::Paren => y2.parse(),
       ...
 38:14 note: ...but borrowed value is only valid for the block at 32:33
         TtDelimited(_, y) => {
             let mut y2 = box () (*y).clone();
             match y2.delim{
                 token::DelimToken::Paren => y2.parse(),
                 _ => panic!("not done yet"),
             }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In this code:

{
    let mut y2 = box (*y).clone();
    match y2.delim {
        token::DelimToken::Paren => y2.parse(),
        _ => panic!("not done yet"),
    }
}

You create y2, which will only live until the block exits. You didn't include your trait, but my guess is that parse returns a reference to the object itself, something like:

fn parse(&self) -> &str

The reference can only last as long as the object, otherwise it'd point to invalid data.

Edit: How it could maybe, possibly work

You have to track the lifetime of the string you are tokenizing and tie your tokens lifetime to that input:

enum Token<'a> {
  Identifier(&'a str),
  Whitespace(&'a str),
}

trait Parser {
    // Important! The lifetime of the output is tied to the parameter `input`,
    // *not* to the structure that implements this! 
    fn parse<'a>(&self, input: &'a str) -> Token<'a>;
}

struct BasicParser;

impl Parser for BasicParser {
    fn parse<'a>(&self, input: &'a str) -> Token<'a> {
        Token::Identifier(input)
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...