【发布时间】:2017-03-02 19:29:53
【问题描述】:
我正在尝试将 woothee-rust 板条箱中的函数公开给 Ruby。为此,我正在解析输入字符串并尝试将结果作为 C 结构返回。我遇到了解析器的生命周期“活得不够长”的问题。我不确定为什么解析器的生命周期必须超过函数。
#![feature(libc)]
#![feature(cstr_to_str)]
#![feature(cstr_memory)]
extern crate libc;
extern crate woothee;
use woothee::parser::{Parser,WootheeResult};
use std::ffi::{CStr,CString};
#[no_mangle]
pub extern fn parse<'a>(ua_string: *const libc::c_char) -> WootheeResult<'a> {
let input = unsafe { CStr::from_ptr(ua_string) };
let parser = Parser::new();
parser.parse(input.to_str().unwrap()).unwrap()
}
这是我得到的错误:
error: `parser` does not live long enough
--> src/lib.rs:14:5
|
14 | parser.parse(input.to_str().unwrap()).unwrap()
| ^^^^^^ does not live long enough
15 | }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the body at 11:77...
--> src/lib.rs:11:78
|
11 | pub extern fn parse<'a>(ua_string: *const libc::c_char) -> WootheeResult<'a> {
| ______________________________________________________________________________^ starting here...
12 | | let input = unsafe { CStr::from_ptr(ua_string) };
13 | | let parser = Parser::new();
14 | | parser.parse(input.to_str().unwrap()).unwrap()
15 | | }
| |_^ ...ending here
【问题讨论】: