- This code compiles:
use combine_language::LanguageEnv;
use combine::stream::easy;
use combine::choice;
use combine::parser::Parser;
use crate::parser::MyParser;
use combine::parser::combinator::no_partial;
pub struct FooParser<'a> {
env: LanguageEnv<'a, easy::Stream<&'a str>>, // = unimplemented!();
}
/*
impl <'a> FooParser<'a> {
pub fn bad(&'a self) -> impl Parser<Input=easy::Stream<&'a str>, Output=String, PartialState=()> {
let mut rops = "1 2 3 4 5 6 7 8"
.split_whitespace()
.map(|s: &str| self.env.reserved_op(s))
.collect::<Vec<_>>();
let t = choice(rops.as_mut_slice())
.map(|s: &str| s.to_string()) ;
return no_partial(t);
}
}
*/
pub fn good<'a>() -> impl Parser<Input=easy::Stream<&'a str>, Output=String, PartialState=()> {
let env: LanguageEnv<'a, easy::Stream<&'a str>> = unimplemented!();
let mut rops = "1 2 3 4 5 6 7 8"
.split_whitespace()
.map(|s: &str| env.reserved_op(s))
.collect::<Vec<_>>();
let t = choice(rops.as_mut_slice())
.map(|s: &str| s.to_string()) ;
return no_partial(t);
}
- If we uncomment the
impl FooParser
part, it fails to compile Complaining:
|
21 | let t = choice(rops.as_mut_slice())
| ---- `rops` is borrowed here
...
24 | return no_partial(t);
| ^^^^^^^^^^^^^ returns a value referencing data owned by the current function
-
Here is what is confusing me: 1. the choice function does NOT want to take Vec as an argument. It actually wants a mut slice. 2. The "good" function is identical to be bad function, except that it is not part of a struct FooParser.
-
What is going on?