You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have wrapped str with StrInput and StrInputSlice myself, and have impl ParseSlice for StrInput, and then I can use $() symbol to get StrInputSlice in which way I can get whole input str access. This is very nice, this is also the reason I chose rust-peg over pest.
but for zero-copy reason, I also want to return sliced str from StrInput<'a> , but I got a life time error for the code below. cuz StringInputSlice is parsed by ParseSlice::parse_slice with a impilict 'input life time so that we can't return StrInputSlice with 'a lifetime
parser!(grammar test_parser<'a>()forStrInput<'a> {/// the reason I wrap str with `StrInput` and `StrInputSlice` is here. I want a way that I can access previous chars(look behind?).
rule check_previous() = slice: $(['*']+){?
if slice.start == 0{returnOk(());
}// unwrap here is oklet char_before = &slice.input[slice.start..slice.end].chars().rev().next().unwrap();
if *char_before != ' '{returnErr("should preceded by space")}Ok(())}// here we got an error, // cuz StringInputSlice is parsed by `ParseSlice::parse_slice` with a impilict `'input` life time // so that we can't return StrInputSlice with `'a` lifetime
rule parse_slice() -> StrInputSlice<'a> = slice:$([_]*){
slice
}});
Thoughts
maybe rust-peg can add a way we can use 'input lifetime.
In my case, the 'input and 'a is the same. Looks like the implicit life time 'input design is nice. if you want to use lifetime, you can just use 'input, if you don't ignore it as usual. sounds like zero cost abstract. it's elegant, thanks for the excellent work. A little more doc work about 'input lifetime use case would be much nicer~.
And it seems that the need for adding lifetime bound 'input: 'a is actually not necessary, cuz most time users will have 'input and 'a equally
kingrongH
changed the title
lifetime problem for custom input
want specify lifetime bound between `` problem for custom input
Apr 4, 2022
kingrongH
changed the title
want specify lifetime bound between `` problem for custom input
want to specify lifetime bound between 'input and custom input lifetime 'aApr 4, 2022
While you can probably make this work if you get the lifetimes just right (see #295), the implicitly-added &'input adds extra confusion here. #299 will allow you to declare grammar test_parser<'a>() for &'a StrInput<'a> {, or make StrInput impl Copy and not need the outer reference at all. Changing how the input type is declared is a breaking change to every rust-peg grammar, so this will have to wait for other changes to be released as 0.9, though.
Background
I have wrapped
str
withStrInput
andStrInputSlice
myself, and have implParseSlice
forStrInput
, and then I can use$()
symbol to getStrInputSlice
in which way I can get whole inputstr
access. This is very nice, this is also the reason I choserust-peg
overpest
.Problem is here
but for zero-copy reason, I also want to return sliced
str
fromStrInput<'a>
, but I got a life time error for the code below. cuz StringInputSlice is parsed byParseSlice::parse_slice
with a impilict'input
life time so that we can't returnStrInputSlice
with'a
lifetimeThoughts
rust-peg
can add a way we can use'input
lifetime.'input: 'a
or maybe both 😀
The text was updated successfully, but these errors were encountered: