我有一个像这样的函数:
extern {
fn foo(layout: *const RawLayout) -> libc::uint8_t;
}
fn bar(layout: Layout) -> bool {
unsafe {
foo(&layout.into() as *const _) != 0
}
}
Layout
是一种可复制类型,可以转换为RawLayout
使用.into()
。
我想确保我理解了这个不安全的操作。据我理解,layout.into()
创建一个临时的RawLayout
,然后&
获取对它的引用,并且as *const _
将其转换为原始指针(*const RawLayout
)。然后调用foo()
函数并返回,最后临时的RawLayout
被丢弃。
是这样吗?还是有一些棘手的原因我不应该这样做?
unsafe
本来就需要足够的脑力。 - Shepmaster