Question
Borrow checking for C struct field
Imagine that C API exposes opaque pointer to some data and two accessors to some field void set_string(struct foo*, const char*)
and const char* get_string(struct foo*)
and that documentation states something along the lines
The string returned by
get_string
is valid as long as the opaque pointer tofoo
is valid and no subsequentset_string
call is made. Otherwise behavior is undefined
This is simple example of the header from C that exemplifies the issue
//foo.h
struct foo;
const char* get_string(struct foo*);
void set_string(struct foo*, const char*);
I'm wondering if and how is it possible to make rust borrow checker keep an eye on that external reference and avoid potential UB after generating bindings with bindgen. I have been experimenting around with
use foo::foo;
use std::marker::PhantomData;
struct Foo<'a> {
pointer: *const foo,
_phantom: PhantomData<&'a CStr>,
}
but it seems to be a dead end.