|
|
|
|
|
by simias
2303 days ago
|
|
I assume the parent meant that there was no compiler magic at play, as far as I know UnsafeCell is written in pure Rust, just using unsafe code: https://doc.rust-lang.org/src/core/cell.rs.html#1486-1488 So basically if Rust's stdlib didn't provide it, you could reimplement it yourself from scratch. EDIT: actually, reading the comments, it looks like I'm wrong about that: > If you have a reference `&SomeStruct`, then normally in Rust all fields of `SomeStruct` are immutable. The compiler makes optimizations based on the knowledge that `&T` is not mutably aliased or mutated, and that `&mut T` is unique. `UnsafeCell<T>` is the only core language feature to work around the restriction that `&T` may not be mutated. |
|
[1]: https://doc.rust-lang.org/beta/unstable-book/language-featur...