Open kingrongH opened 2 years ago
with specifying StrInputSlice
's lifetime to 'input
, the following code actually work.
parser!(grammar test_parser<'a>() for StrInput<'a> {
rule parse_slice() -> StrInputSlice<'input> = slice:$([_]*) {
slice
}
});
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
I think I still need a way to make the following code work as expected, so I reopen this issue.
rule parse_slice() -> StrInputSlice<'a> = slice:$([_]*) {
slice
}
While you can probably make this work if you get the lifetimes just right (see https://github.com/kevinmehall/rust-peg/discussions/295), the implicitly-added &'input
adds extra confusion here. https://github.com/kevinmehall/rust-peg/pull/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 😀