【问题标题】:Why does `*mut T` implement `Unwindsafe` but `&mut T` doesn't?为什么 `*mut T` 实现了 `Unwindsafe` 而 `&mut T` 没有?
【发布时间】:2020-06-17 06:08:48
【问题描述】:

documentationUnwindsafe 我们有:

&mut T 和 &RefCell<T> 等类型是安全展开的示例。一般的想法是,任何可以在catch_unwind 之间共享的可变状态默认情况下都不是安全的。这是因为在catch_unwind 之外很容易看到损坏的不变量,因为数据只是照常访问。

按照这个逻辑,在我看来*mut T 不应该是Unwindsafe。但是it turns out that it is。这是为什么呢?

【问题讨论】:

  • 我猜这是因为 *mut 本身并没有不变量,所以没有“被破坏的不变量”可以观察。

标签: rust unsafe stack-unwinding raw-pointer


【解决方案1】:

*mut T 是一个原始指针,它没有任何不变量。

可以为null,指向无效内存,Copy,可以有两个指向内存中的同一个区域。

无论如何,使用*mut T,您无能为力安全,因此没有理由不成为Unwindsafe

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 2019-09-04
    • 1970-01-01
    • 2022-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多