Skip to content

Commit 51d2016

Browse files
committedOct 13, 2023
Adress feedback in #433
1 parent 48d2a97 commit 51d2016

File tree

1 file changed

+38
-30
lines changed

1 file changed

+38
-30
lines changed
 

‎src/lib.rs

+38-30
Original file line numberDiff line numberDiff line change
@@ -652,10 +652,10 @@ impl_known_layout!(const N: usize, T => [T; N]);
652652

653653
safety_comment! {
654654
/// SAFETY:
655-
// TODO(#429): Add reference to docs + quotes
656655
/// `str` and `ManuallyDrop<[T]>` have the same representations as `[u8]`
657656
/// and `[T]` repsectively. `str` has different bit validity than `[u8]`,
658657
/// but that doesn't affect the soundness of this impl.
658+
/// TODO(#429): Add references to docs and quotes.
659659
unsafe_impl_known_layout!(#[repr([u8])] str);
660660
unsafe_impl_known_layout!(T: ?Sized + KnownLayout => #[repr(T)] ManuallyDrop<T>);
661661
}
@@ -757,13 +757,13 @@ pub unsafe trait FromZeroes {
757757
let slf: *mut Self = self;
758758
let len = mem::size_of_val(self);
759759
// SAFETY:
760-
// TODO(#429): Add reference to docs + quotes
761760
// - `self` is guaranteed by the type system to be valid for writes of
762761
// size `size_of_val(self)`.
763762
// - `u8`'s alignment is 1, and thus `self` is guaranteed to be aligned
764763
// as required by `u8`.
765764
// - Since `Self: FromZeroes`, the all-zeroes instance is a valid
766765
// instance of `Self.`
766+
// TODO(#429): Add references to docs and quotes.
767767
unsafe { ptr::write_bytes(slf.cast::<u8>(), 0, len) };
768768
}
769769

@@ -1145,7 +1145,6 @@ pub unsafe trait AsBytes {
11451145
let slf: *const Self = self;
11461146

11471147
// SAFETY:
1148-
// TODO(#429): Add reference to docs + quotes
11491148
// - `slf.cast::<u8>()` is valid for reads for `len *
11501149
// mem::size_of::<u8>()` many bytes because...
11511150
// - `slf` is the same pointer as `self`, and `self` is a reference
@@ -1164,6 +1163,7 @@ pub unsafe trait AsBytes {
11641163
// - The total size of the resulting slice is no larger than
11651164
// `isize::MAX` because no allocation produced by safe code can be
11661165
// larger than `isize::MAX`.
1166+
// TODO(#429): Add references to docs and quotes.
11671167
unsafe { slice::from_raw_parts(slf.cast::<u8>(), len) }
11681168
}
11691169

@@ -1199,6 +1199,7 @@ pub unsafe trait AsBytes {
11991199
// - The total size of the resulting slice is no larger than
12001200
// `isize::MAX` because no allocation produced by safe code can be
12011201
// larger than `isize::MAX`.
1202+
// TODO(#429): Add references to docs and quotes.
12021203
unsafe { slice::from_raw_parts_mut(slf.cast::<u8>(), len) }
12031204
}
12041205

@@ -1286,7 +1287,6 @@ safety_comment! {
12861287

12871288
safety_comment! {
12881289
/// SAFETY:
1289-
/// TODO(#429): Add quotes
12901290
/// - `FromZeroes`, `FromBytes`: all bit patterns are valid for integers [1]
12911291
/// - `AsBytes`: integers have no padding bytes [1]
12921292
/// - `Unaligned` (`u8` and `i8` only): The reference [2] specifies the size
@@ -1296,6 +1296,8 @@ safety_comment! {
12961296
/// - The only value >= 1 for which 1 is an integer multiple is 1
12971297
/// Therefore, the only possible alignment for `u8` and `i8` is 1.
12981298
///
1299+
/// TODO(#429): Add quotes from documentation.
1300+
///
12991301
/// [1] TODO(https://github.com/rust-lang/reference/issues/1291): Once the
13001302
/// reference explicitly guarantees these properties, cite it.
13011303
/// [2] https://doc.rust-lang.org/reference/type-layout.html#primitive-data-layout
@@ -1322,8 +1324,9 @@ safety_comment! {
13221324
/// - `AsBytes`: the `{f32,f64}::to_bits` methods' documentation [4,5]
13231325
/// states that they are currently equivalent to `transmute`. [3]
13241326
///
1325-
/// TODO(#429): Make these arguments more precisely in terms of the documentation
1326-
/// TODO(#429): Add quotes
1327+
/// TODO(#429):
1328+
/// - Make these arguments more precisely in terms of the documentation.
1329+
/// - Add quotes from documentation.
13271330
///
13281331
/// [1] https://doc.rust-lang.org/nightly/std/primitive.f32.html#method.from_bits
13291332
/// [2] https://doc.rust-lang.org/nightly/std/primitive.f64.html#method.from_bits
@@ -1337,11 +1340,12 @@ safety_comment! {
13371340

13381341
safety_comment! {
13391342
/// SAFETY:
1340-
/// - `FromZeroes`: Valid since "The value false has the bit pattern 0x00" [1].
1341-
/// - `AsBytes`: Since "the boolean type has a size and
1342-
/// alignment of 1 each" and "The value false has the bit pattern 0x00 and
1343-
/// the value true has the bit pattern 0x01" [1]. Note that it is undefined
1344-
/// behavior for an object with the boolean type to have any other bit pattern.
1343+
/// - `FromZeroes`: Valid since "The value false has the bit pattern
1344+
/// 0x00" [1].
1345+
/// - `AsBytes`: Since "the boolean type has a size and alignment of 1 each"
1346+
/// and "The value false has the bit pattern 0x00 and the value true has
1347+
/// the bit pattern 0x01" [1]. Thus, the only byte of the bool is always
1348+
/// initialized.
13451349
/// - `Unaligned`: Per the reference [1], "[a]n object with the boolean type
13461350
/// has a size and alignment of 1 each."
13471351
///
@@ -1351,28 +1355,28 @@ safety_comment! {
13511355
}
13521356
safety_comment! {
13531357
/// SAFETY:
1354-
/// - `FromZeroes`: Per reference [1], "A value of type char is
1355-
/// a Unicode scalar value (i.e. a code point that is not a surrogate),
1356-
/// represented as a 32-bit unsigned word in the 0x0000 to 0xD7FF or 0xE000
1357-
/// to 0x10FFFF range" which contains 0x0000.
1358-
/// - `AsBytes`: `char` is per reference [1] "represented as a 32-bit unsigned
1359-
/// word" (`u32`)
1360-
/// which is `AsBytes`. Note that unlike `u32`, not all bit patterns
1361-
/// are valid for `char`.
1358+
/// - `FromZeroes`: Per reference [1], "A value of type char is a Unicode
1359+
/// scalar value (i.e. a code point that is not a surrogate), represented
1360+
/// as a 32-bit unsigned word in the 0x0000 to 0xD7FF or 0xE000 to
1361+
/// 0x10FFFF range" which contains 0x0000.
1362+
/// - `AsBytes`: `char` is per reference [1] "represented as a 32-bit
1363+
/// unsigned word" (`u32`) which is `AsBytes`. Note that unlike `u32`, not
1364+
/// all bit patterns are valid for `char`.
13621365
///
13631366
/// [1] https://doc.rust-lang.org/reference/types/textual.html
13641367
unsafe_impl!(char: FromZeroes, AsBytes);
13651368
}
13661369
safety_comment! {
13671370
/// SAFETY:
1368-
/// TODO(#429): Add quotes
1369-
/// - `FromZeroes`, `AsBytes`, `Unaligned`: Per the reference [1], `str` has
1370-
/// the same layout as `[u8]`, and `[u8]` is `FromZeroes`, `AsBytes`, and
1371-
/// `Unaligned`.
1371+
/// - `FromZeroes`, `AsBytes`, `Unaligned`: Per the reference [1], `str`
1372+
/// has the same layout as `[u8]`, and `[u8]` is `FromZeroes`, `AsBytes`,
1373+
/// and `Unaligned`.
13721374
///
13731375
/// Note that we don't `assert_unaligned!(str)` because `assert_unaligned!`
13741376
/// uses `align_of`, which only works for `Sized` types.
13751377
///
1378+
/// TODO(#429): Add quotes from documentation.
1379+
///
13761380
/// [1] https://doc.rust-lang.org/reference/type-layout.html#str-layout
13771381
unsafe_impl!(str: FromZeroes, AsBytes, Unaligned);
13781382
}
@@ -1381,7 +1385,6 @@ safety_comment! {
13811385
// `NonZeroXxx` is `AsBytes`, but not `FromZeroes` or `FromBytes`.
13821386
//
13831387
/// SAFETY:
1384-
/// TODO(#429): Add quotes
13851388
/// - `AsBytes`: `NonZeroXxx` has the same layout as its associated
13861389
/// primitive. Since it is the same size, this guarantees it has no
13871390
/// padding - integers have no padding, and there's no room for padding
@@ -1396,6 +1399,8 @@ safety_comment! {
13961399
/// be 0 bytes, which means that they must be 1 byte. The only valid
13971400
/// alignment for a 1-byte type is 1.
13981401
///
1402+
/// TODO(#429): Add quotes from documentation.
1403+
///
13991404
/// [1] https://doc.rust-lang.org/stable/std/num/struct.NonZeroU8.html
14001405
/// [2] https://doc.rust-lang.org/stable/std/num/struct.NonZeroI8.html
14011406
/// TODO(https://github.com/rust-lang/rust/pull/104082): Cite documentation
@@ -1416,7 +1421,6 @@ safety_comment! {
14161421
}
14171422
safety_comment! {
14181423
/// SAFETY:
1419-
/// TODO(#429): Add quotes
14201424
/// - `FromZeroes`, `FromBytes`, `AsBytes`: The Rust compiler reuses `0`
14211425
/// value to represent `None`, so `size_of::<Option<NonZeroXxx>>() ==
14221426
/// size_of::<xxx>()`; see `NonZeroXxx` documentation.
@@ -1427,6 +1431,8 @@ safety_comment! {
14271431
/// unthinkable that that would ever change. The only valid alignment for
14281432
/// a 1-byte type is 1.
14291433
///
1434+
/// TODO(#429): Add quotes from documentation.
1435+
///
14301436
/// [1] https://doc.rust-lang.org/stable/std/num/struct.NonZeroU8.html
14311437
/// [2] https://doc.rust-lang.org/stable/std/num/struct.NonZeroI8.html
14321438
///
@@ -1470,14 +1476,16 @@ safety_comment! {
14701476
}
14711477
safety_comment! {
14721478
/// SAFETY:
1473-
/// TODO(#429): Add quotes
14741479
/// `Wrapping<T>` is guaranteed by its docs [1] to have the same layout as
14751480
/// `T`. Also, `Wrapping<T>` is `#[repr(transparent)]`, and has a single
14761481
/// field, which is `pub`. Per the reference [2], this means that the
14771482
/// `#[repr(transparent)]` attribute is "considered part of the public ABI".
14781483
///
1479-
/// references nightly?:
1484+
/// TODO(#429): Add quotes from documentation.
1485+
///
14801486
/// [1] TODO(https://doc.rust-lang.org/nightly/core/num/struct.Wrapping.html#layout-1):
1487+
/// Reference this documentation once it's available on stable.
1488+
///
14811489
/// [2] https://doc.rust-lang.org/nomicon/other-reprs.html#reprtransparent
14821490
unsafe_impl!(T: FromZeroes => FromZeroes for Wrapping<T>);
14831491
unsafe_impl!(T: FromBytes => FromBytes for Wrapping<T>);
@@ -1496,8 +1504,8 @@ safety_comment! {
14961504
/// Thus, we require `T: FromZeroes` and `T: FromBytes` in order to ensure
14971505
/// that `T` - and thus `MaybeUninit<T>` - contains to `UnsafeCell`s.
14981506
/// Thus, requiring that `T` implement each of these traits is sufficient
1499-
/// - `Unaligned`: "MaybeUninit<T> is guaranteed to have the same size, alignment,
1500-
/// and ABI as T" [1]
1507+
/// - `Unaligned`: "MaybeUninit<T> is guaranteed to have the same size,
1508+
/// alignment, and ABI as T" [1]
15011509
///
15021510
/// [1] https://doc.rust-lang.org/stable/core/mem/union.MaybeUninit.html#layout-1
15031511
///
@@ -1512,7 +1520,6 @@ safety_comment! {
15121520
}
15131521
safety_comment! {
15141522
/// SAFETY:
1515-
// TODO(#429): Add reference to docs + quotes
15161523
/// `ManuallyDrop` has the same layout as `T`, and accessing the inner value
15171524
/// is safe (meaning that it's unsound to leave the inner value
15181525
/// uninitialized while exposing the `ManuallyDrop` to safe code).
@@ -1528,6 +1535,7 @@ safety_comment! {
15281535
/// code can only ever access a `ManuallyDrop` with all initialized bytes.
15291536
/// - `Unaligned`: `ManuallyDrop` has the same layout (and thus alignment)
15301537
/// as `T`, and `T: Unaligned` guarantees that that alignment is 1.
1538+
/// TODO(#429): Add references to docs and quotes.
15311539
unsafe_impl!(T: ?Sized + FromZeroes => FromZeroes for ManuallyDrop<T>);
15321540
unsafe_impl!(T: ?Sized + FromBytes => FromBytes for ManuallyDrop<T>);
15331541
unsafe_impl!(T: ?Sized + AsBytes => AsBytes for ManuallyDrop<T>);

0 commit comments

Comments
 (0)
Please sign in to comment.