I'm writing wrapper code for an external C library, and I'm trying to convince the Rust compiler to enforce external lifetime restrictions that are not reflected in the Rust code itself. For example, one type of "opaque handle" can return a child handle that is only valid for the lifetime of the parent handle.
I experimented with std::marker::PhantomData
, but I couldn't convince the compiler to return the expected error.
In other words, I'd like the following block of code fail to compile:
struct Parent;
struct Child; // Note that there is no reference to the parent struct
impl Parent {
fn get_child( &self ) -> Child {
Child
}
}
// I'd like this to complain with "p does not live long enough"
fn test() -> Child {
let p = Parent;
p.get_child()
}
fn main() {
let c = test();
}