Skip to content

Commit 0000bb3

Browse files
authored
Add safety comment for MaybeUninit UnsafeCell (#1620)
Closes #896
1 parent 175ad69 commit 0000bb3

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/util.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,19 @@ impl<I: invariant::Validity> ValidityVariance<I> for Invariant {
110110
unsafe impl<T, I: Invariants> TransparentWrapper<I> for MaybeUninit<T> {
111111
type Inner = T;
112112

113-
// SAFETY: Per [1], `MaybeUninit<T>` has `UnsafeCell`s covering the same
114-
// byte ranges as `Inner = T`.
113+
// SAFETY: `MaybeUninit<T>` has `UnsafeCell`s covering the same byte ranges
114+
// as `Inner = T`. This is not explicitly documented, but it can be
115+
// inferred. Per [1] in the preceding safety comment, `MaybeUninit<T>` has
116+
// the same size as `T`. Further, note the signature of
117+
// `MaybeUninit::assume_init_ref` [2]:
118+
//
119+
// pub unsafe fn assume_init_ref(&self) -> &T
120+
//
121+
// If the argument `&MaybeUninit<T>` and the returned `&T` had `UnsafeCell`s
122+
// at different offsets, this would be unsound. Its existence is proof that
123+
// this is not the case.
115124
//
116-
// [1] TODO(#896): Write a safety proof before the next stable release.
125+
// [2] https://doc.rust-lang.org/1.81.0/std/mem/union.MaybeUninit.html#method.assume_init_ref
117126
type UnsafeCellVariance = Covariant;
118127
// SAFETY: Per [1], `MaybeUninit<T>` has the same layout as `T`, and thus
119128
// has the same alignment as `T`.
@@ -280,7 +289,7 @@ unsafe impl<T: ?Sized, I: Invariants> TransparentWrapper<I> for UnsafeCell<T> {
280289
// subsequent sentence in the documentation makes it clear that this is the
281290
// intention.
282291
//
283-
// [1] Per https://doc.rust-lang.org/std/cell/struct.UnsafeCell.html#memory-layout:
292+
// [1] Per https://doc.rust-lang.org/1.81.0/core/cell/struct.UnsafeCell.html#memory-layout:
284293
//
285294
// `UnsafeCell<T>` has the same in-memory representation as its inner type
286295
// `T`. A consequence of this guarantee is that it is possible to convert

0 commit comments

Comments
 (0)