Skip to content

Commit 0795819

Browse files
committed
Add TODOs related to #429 and quotes to safety comments.
1 parent 4292ecf commit 0795819

File tree

1 file changed

+56
-26
lines changed

1 file changed

+56
-26
lines changed

src/lib.rs

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -656,12 +656,14 @@ safety_comment! {
656656
/// `[u8]` and `[T]` repsectively. `str` has different bit validity than
657657
/// `[u8]`, but that doesn't affect the soundness of this impl.
658658
///
659-
/// [1] Per https://doc.rust-lang.org/nightly/core/mem/struct.ManuallyDrop.html:
660-
///
661659
/// `ManuallyDrop<T>` is guaranteed to have the same layout and bit
662660
/// validity as `T`
663661
///
664-
/// TODO(#429): Once this text (added in
662+
/// [1] Per https://doc.rust-lang.org/nightly/core/mem/struct.ManuallyDrop.html:
663+
///
664+
/// TODO(#429):
665+
/// - Add quotes from docs.
666+
/// - Once [1] (added in
665667
/// https://github.com/rust-lang/rust/pull/115522) is available on stable,
666668
/// quote the stable docs instead of the nightly docs.
667669
unsafe_impl_known_layout!(#[repr([u8])] str);
@@ -771,6 +773,8 @@ pub unsafe trait FromZeroes {
771773
// as required by `u8`.
772774
// - Since `Self: FromZeroes`, the all-zeroes instance is a valid
773775
// instance of `Self.`
776+
//
777+
// TODO(#429): Add references to docs and quotes.
774778
unsafe { ptr::write_bytes(slf.cast::<u8>(), 0, len) };
775779
}
776780

@@ -1170,6 +1174,8 @@ pub unsafe trait AsBytes {
11701174
// - The total size of the resulting slice is no larger than
11711175
// `isize::MAX` because no allocation produced by safe code can be
11721176
// larger than `isize::MAX`.
1177+
//
1178+
// TODO(#429): Add references to docs and quotes.
11731179
unsafe { slice::from_raw_parts(slf.cast::<u8>(), len) }
11741180
}
11751181

@@ -1205,6 +1211,8 @@ pub unsafe trait AsBytes {
12051211
// - The total size of the resulting slice is no larger than
12061212
// `isize::MAX` because no allocation produced by safe code can be
12071213
// larger than `isize::MAX`.
1214+
//
1215+
// TODO(#429): Add references to docs and quotes.
12081216
unsafe { slice::from_raw_parts_mut(slf.cast::<u8>(), len) }
12091217
}
12101218

@@ -1301,6 +1309,8 @@ safety_comment! {
13011309
/// - The only value >= 1 for which 1 is an integer multiple is 1
13021310
/// Therefore, the only possible alignment for `u8` and `i8` is 1.
13031311
///
1312+
/// TODO(#429): Add quotes from documentation.
1313+
///
13041314
/// [1] TODO(https://github.com/rust-lang/reference/issues/1291): Once the
13051315
/// reference explicitly guarantees these properties, cite it.
13061316
/// [2] https://doc.rust-lang.org/reference/type-layout.html#primitive-data-layout
@@ -1327,7 +1337,9 @@ safety_comment! {
13271337
/// - `AsBytes`: the `{f32,f64}::to_bits` methods' documentation [4,5]
13281338
/// states that they are currently equivalent to `transmute`. [3]
13291339
///
1330-
/// TODO: Make these arguments more precisely in terms of the documentation.
1340+
/// TODO(#429):
1341+
/// - Make these arguments more precisely in terms of the documentation.
1342+
/// - Add quotes from documentation.
13311343
///
13321344
/// [1] https://doc.rust-lang.org/nightly/std/primitive.f32.html#method.from_bits
13331345
/// [2] https://doc.rust-lang.org/nightly/std/primitive.f64.html#method.from_bits
@@ -1341,11 +1353,12 @@ safety_comment! {
13411353

13421354
safety_comment! {
13431355
/// SAFETY:
1344-
/// - `FromZeroes`: Per the reference [1], 0x00 is a valid bit pattern for
1345-
/// `bool`.
1346-
/// - `AsBytes`: Per the reference [1], `bool` always has a size of 1 with
1347-
/// valid bit patterns 0x01 and 0x00, so the only byte of the bool is
1348-
/// always initialized
1356+
/// - `FromZeroes`: Valid since "[t]he value false has the bit pattern
1357+
/// 0x00" [1].
1358+
/// - `AsBytes`: Since "the boolean type has a size and alignment of 1 each"
1359+
/// and "The value false has the bit pattern 0x00 and the value true has
1360+
/// the bit pattern 0x01" [1]. Thus, the only byte of the bool is always
1361+
/// initialized.
13491362
/// - `Unaligned`: Per the reference [1], "[a]n object with the boolean type
13501363
/// has a size and alignment of 1 each."
13511364
///
@@ -1355,24 +1368,28 @@ safety_comment! {
13551368
}
13561369
safety_comment! {
13571370
/// SAFETY:
1358-
/// - `FromZeroes`: Per the reference [1], 0x0000 is a valid bit pattern for
1359-
/// `char`.
1360-
/// - `AsBytes`: `char` is represented as a 32-bit unsigned word (`u32`)
1361-
/// [1], which is `AsBytes`. Note that unlike `u32`, not all bit patterns
1362-
/// are valid for `char`.
1371+
/// - `FromZeroes`: Per reference [1], "[a] value of type char is a Unicode
1372+
/// scalar value (i.e. a code point that is not a surrogate), represented
1373+
/// as a 32-bit unsigned word in the 0x0000 to 0xD7FF or 0xE000 to
1374+
/// 0x10FFFF range" which contains 0x0000.
1375+
/// - `AsBytes`: `char` is per reference [1] "represented as a 32-bit
1376+
/// unsigned word" (`u32`) which is `AsBytes`. Note that unlike `u32`, not
1377+
/// all bit patterns are valid for `char`.
13631378
///
13641379
/// [1] https://doc.rust-lang.org/reference/types/textual.html
13651380
unsafe_impl!(char: FromZeroes, AsBytes);
13661381
}
13671382
safety_comment! {
13681383
/// SAFETY:
1369-
/// - `FromZeroes`, `AsBytes`, `Unaligned`: Per the reference [1], `str` has
1370-
/// the same layout as `[u8]`, and `[u8]` is `FromZeroes`, `AsBytes`, and
1371-
/// `Unaligned`.
1384+
/// - `FromZeroes`, `AsBytes`, `Unaligned`: Per the reference [1], `str`
1385+
/// has the same layout as `[u8]`, and `[u8]` is `FromZeroes`, `AsBytes`,
1386+
/// and `Unaligned`.
13721387
///
13731388
/// Note that we don't `assert_unaligned!(str)` because `assert_unaligned!`
13741389
/// uses `align_of`, which only works for `Sized` types.
13751390
///
1391+
/// TODO(#429): Add quotes from documentation.
1392+
///
13761393
/// [1] https://doc.rust-lang.org/reference/type-layout.html#str-layout
13771394
unsafe_impl!(str: FromZeroes, AsBytes, Unaligned);
13781395
}
@@ -1395,6 +1412,8 @@ safety_comment! {
13951412
/// be 0 bytes, which means that they must be 1 byte. The only valid
13961413
/// alignment for a 1-byte type is 1.
13971414
///
1415+
/// TODO(#429): Add quotes from documentation.
1416+
///
13981417
/// [1] https://doc.rust-lang.org/stable/std/num/struct.NonZeroU8.html
13991418
/// [2] https://doc.rust-lang.org/stable/std/num/struct.NonZeroI8.html
14001419
/// TODO(https://github.com/rust-lang/rust/pull/104082): Cite documentation
@@ -1425,6 +1444,8 @@ safety_comment! {
14251444
/// unthinkable that that would ever change. The only valid alignment for
14261445
/// a 1-byte type is 1.
14271446
///
1447+
/// TODO(#429): Add quotes from documentation.
1448+
///
14281449
/// [1] https://doc.rust-lang.org/stable/std/num/struct.NonZeroU8.html
14291450
/// [2] https://doc.rust-lang.org/stable/std/num/struct.NonZeroI8.html
14301451
///
@@ -1447,7 +1468,11 @@ safety_comment! {
14471468

14481469
safety_comment! {
14491470
/// SAFETY:
1450-
/// For all `T`, `PhantomData<T>` has size 0 and alignment 1. [1]
1471+
/// Per reference [1]:
1472+
/// "For all T, the following are guaranteed:
1473+
/// size_of::<PhantomData<T>>() == 0
1474+
/// align_of::<PhantomData<T>>() == 1".
1475+
/// This gives:
14511476
/// - `FromZeroes`, `FromBytes`: There is only one possible sequence of 0
14521477
/// bytes, and `PhantomData` is inhabited.
14531478
/// - `AsBytes`: Since `PhantomData` has size 0, it contains no padding
@@ -1469,7 +1494,11 @@ safety_comment! {
14691494
/// field, which is `pub`. Per the reference [2], this means that the
14701495
/// `#[repr(transparent)]` attribute is "considered part of the public ABI".
14711496
///
1472-
/// [1] https://doc.rust-lang.org/nightly/core/num/struct.Wrapping.html#layout-1
1497+
/// TODO(#429): Add quotes from documentation.
1498+
///
1499+
/// [1] TODO(https://doc.rust-lang.org/nightly/core/num/struct.Wrapping.html#layout-1):
1500+
/// Reference this documentation once it's available on stable.
1501+
///
14731502
/// [2] https://doc.rust-lang.org/nomicon/other-reprs.html#reprtransparent
14741503
unsafe_impl!(T: FromZeroes => FromZeroes for Wrapping<T>);
14751504
unsafe_impl!(T: FromBytes => FromBytes for Wrapping<T>);
@@ -1488,11 +1517,10 @@ safety_comment! {
14881517
/// Thus, we require `T: FromZeroes` and `T: FromBytes` in order to ensure
14891518
/// that `T` - and thus `MaybeUninit<T>` - contains to `UnsafeCell`s.
14901519
/// Thus, requiring that `T` implement each of these traits is sufficient
1491-
/// - `Unaligned`: `MaybeUninit<T>` is guaranteed by its documentation [1]
1492-
/// to have the same alignment as `T`.
1520+
/// - `Unaligned`: "MaybeUninit<T> is guaranteed to have the same size,
1521+
/// alignment, and ABI as T" [1]
14931522
///
1494-
/// [1]
1495-
/// https://doc.rust-lang.org/nightly/core/mem/union.MaybeUninit.html#layout-1
1523+
/// [1] https://doc.rust-lang.org/stable/core/mem/union.MaybeUninit.html#layout-1
14961524
///
14971525
/// TODO(https://github.com/google/zerocopy/issues/251): If we split
14981526
/// `FromBytes` and `RefFromBytes`, or if we introduce a separate
@@ -1522,12 +1550,14 @@ safety_comment! {
15221550
/// - `Unaligned`: `ManuallyDrop` has the same layout (and thus alignment)
15231551
/// as `T`, and `T: Unaligned` guarantees that that alignment is 1.
15241552
///
1525-
/// [1] Per https://doc.rust-lang.org/nightly/core/mem/struct.ManuallyDrop.html:
1526-
///
15271553
/// `ManuallyDrop<T>` is guaranteed to have the same layout and bit
15281554
/// validity as `T`
15291555
///
1530-
/// TODO(#429): Once this text (added in
1556+
/// [1] Per https://doc.rust-lang.org/nightly/core/mem/struct.ManuallyDrop.html:
1557+
///
1558+
/// TODO(#429):
1559+
/// - Add quotes from docs.
1560+
/// - Once [1] (added in
15311561
/// https://github.com/rust-lang/rust/pull/115522) is available on stable,
15321562
/// quote the stable docs instead of the nightly docs.
15331563
unsafe_impl!(T: ?Sized + FromZeroes => FromZeroes for ManuallyDrop<T>);

0 commit comments

Comments
 (0)