【发布时间】:2015-12-02 08:37:23
【问题描述】:
我正在为外部 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();
}
【问题讨论】:
-
我认为您出于某种原因不想继续在
Child中添加对Parent的引用?
标签: rust