I am trying to expose a function from the woothee-rust
crate to Ruby. To do this, I am parsing an input string and attempting to return the result as a C struct. I run into an issue where the lifetime of the parser "does not live long enough". I am not sure why the parser's lifetime must live past the function.
#![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()
}
Here is the error I get:
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