I'm using a nom parser to return a Vec<PdlItem>
. I don't understand why the parsable_buffer
needs to live beyond the last line; or how to get around the error. res
owns the Vec<PdlItem>
so my expectation is that it would be moved into the result.
I have no use for parsable_buffer
, what have I missed?
pub enum PdlError {
ParseError,
}
#[derive(Debug, PartialEq, Clone)]
pub enum PdlItem<'a> {
FOO,
STR(&'a str, Option<&'a str>, &'a str),
NUM(&'a str, Option<&'a str>, f32),
VNUM(&'a str, Option<&'a str>, Vec<f32>),
VSTR(&'a str, Option<&'a str>, Vec<&'a str>),
}
pub fn pdl_to_tokens(buffer: &[u8]) -> Result<Vec<PdlItem>, PdlError> {
// Remove comments
let parsable_buffer: Vec<u8> = remove_comments(buffer);
let res: IResult<&[u8], Vec<PdlItem>> = parse_pdl(&parsable_buffer);
match res {
IResult::Done(_, o) => {
println!("Parsed ok {}", o.len());
Ok(o.to_owned())
} // Also tried .clone()
IResult::Incomplete(i) => {
println!("{:?}", i);
Err(PdlError::ParseError)
}
IResult::Error(e) => {
println!("Error {}", e);
Err(PdlError::ParseError)
}
}
}
The error is
error[E0597]: `parsable_buffer` does not live long enough
--> src\pdl_serialiser.rs:24:26
|
24 | let res = parse_pdl(&parsable_buffer);
| ^^^^^^^^^^^^^^^ does not live long enough
...
31 | }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the anonymous lifetime #1 defined on the function body at 21:1...
--> src\pdl_serialiser.rs:21:1
|
21 | / pub fn pdl_to_tokens(buffer : &[u8]) -> Result<Vec<PdlItem>,PdlError> {
22 | | // Remove comments
23 | | let parsable_buffer = remove_comments(buffer);
24 | | let res = parse_pdl(&parsable_buffer);
... |
30 | | }
31 | | }
| |_^