我正在为外部C库编写包装器代码,并且我试图说服Rust编译器强制执行外部生命周期限制,这些限制未反映在Rust代码本身中.例如,一种类型的"不透明句柄"可以返回仅在父句柄的生命周期内有效的子句柄.
我试验过std::marker::PhantomData,但我无法说服编译器返回预期的错误.
换句话说,我想以下代码块无法编译:
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();
}
Run Code Online (Sandbox Code Playgroud) rust ×1