Hi, I'm writing Wrapper for a C API and I try to make a safe wrapper for a type that contain pointers by adding lifetime constraints.
For example I have the following type in C:
#[repr(C)]
#[derive(Copy, Clone)]
pub struct ast_statement {
pub type_: ast_statement_type_t,
pub __bindgen_anon_1: ast_statement__bindgen_ty_1,
}
#[repr(C)]
#[derive(Copy, Clone)]
pub union ast_statement__bindgen_ty_1 {
pub rule: *const ast_rule_t,
pub definition: *const ast_definition_t,
_bindgen_union_align: u64,
}
My wrapper type should have the lifetime constraint that the object cannot outlive the rule to which it points.
#[derive(Clone)]
pub struct AstStatement<'a> {
data: ast_statement_t,
phantom: PhantomData<&'a u32>,
}
An AstStatement
can be created from a Rule
using the following function:
pub struct Rule(ast_rule);
impl Rule {
pub fn ast_statement(&self) -> AstStatement {
AstStatement {
data: ast_statement_t {
type_: ast_statement_type_ast_statement_type_rule,
__bindgen_anon_1: ast_statement__bindgen_ty_1 {
rule: &self.0 as *const ast_rule,
},
},
phantom: PhantomData,
}
}
I'm not sure wheather I'm doing it right. Please correct me if I'm doing something stupid.
Also I would like to know how i can test if the lifetime behavior is like expected.