我使用nom解析器返回Vec<PdlItem>
。我不明白为什么parsable_buffer
需要超越最后一行;或如何解决错误。 res
拥有Vec<PdlItem>
,所以我的期望是它会被移入结果中。
我对parsable_buffer
毫无用处,我错过了什么?
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)
}
}
}
错误是
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 | | }
| |_^