@@ -652,10 +652,10 @@ impl_known_layout!(const N: usize, T => [T; N]);
652
652
653
653
safety_comment ! {
654
654
/// SAFETY:
655
- // TODO(#429): Add reference to docs + quotes
656
655
/// `str` and `ManuallyDrop<[T]>` have the same representations as `[u8]`
657
656
/// and `[T]` repsectively. `str` has different bit validity than `[u8]`,
658
657
/// but that doesn't affect the soundness of this impl.
658
+ /// TODO(#429): Add references to docs and quotes.
659
659
unsafe_impl_known_layout!( #[ repr( [ u8 ] ) ] str ) ;
660
660
unsafe_impl_known_layout!( T : ?Sized + KnownLayout => #[ repr( T ) ] ManuallyDrop <T >) ;
661
661
}
@@ -757,13 +757,13 @@ pub unsafe trait FromZeroes {
757
757
let slf: * mut Self = self ;
758
758
let len = mem:: size_of_val ( self ) ;
759
759
// SAFETY:
760
- // TODO(#429): Add reference to docs + quotes
761
760
// - `self` is guaranteed by the type system to be valid for writes of
762
761
// size `size_of_val(self)`.
763
762
// - `u8`'s alignment is 1, and thus `self` is guaranteed to be aligned
764
763
// as required by `u8`.
765
764
// - Since `Self: FromZeroes`, the all-zeroes instance is a valid
766
765
// instance of `Self.`
766
+ // TODO(#429): Add references to docs and quotes.
767
767
unsafe { ptr:: write_bytes ( slf. cast :: < u8 > ( ) , 0 , len) } ;
768
768
}
769
769
@@ -1145,7 +1145,6 @@ pub unsafe trait AsBytes {
1145
1145
let slf: * const Self = self ;
1146
1146
1147
1147
// SAFETY:
1148
- // TODO(#429): Add reference to docs + quotes
1149
1148
// - `slf.cast::<u8>()` is valid for reads for `len *
1150
1149
// mem::size_of::<u8>()` many bytes because...
1151
1150
// - `slf` is the same pointer as `self`, and `self` is a reference
@@ -1164,6 +1163,7 @@ pub unsafe trait AsBytes {
1164
1163
// - The total size of the resulting slice is no larger than
1165
1164
// `isize::MAX` because no allocation produced by safe code can be
1166
1165
// larger than `isize::MAX`.
1166
+ // TODO(#429): Add references to docs and quotes.
1167
1167
unsafe { slice:: from_raw_parts ( slf. cast :: < u8 > ( ) , len) }
1168
1168
}
1169
1169
@@ -1199,6 +1199,7 @@ pub unsafe trait AsBytes {
1199
1199
// - The total size of the resulting slice is no larger than
1200
1200
// `isize::MAX` because no allocation produced by safe code can be
1201
1201
// larger than `isize::MAX`.
1202
+ // TODO(#429): Add references to docs and quotes.
1202
1203
unsafe { slice:: from_raw_parts_mut ( slf. cast :: < u8 > ( ) , len) }
1203
1204
}
1204
1205
@@ -1286,7 +1287,6 @@ safety_comment! {
1286
1287
1287
1288
safety_comment ! {
1288
1289
/// SAFETY:
1289
- /// TODO(#429): Add quotes
1290
1290
/// - `FromZeroes`, `FromBytes`: all bit patterns are valid for integers [1]
1291
1291
/// - `AsBytes`: integers have no padding bytes [1]
1292
1292
/// - `Unaligned` (`u8` and `i8` only): The reference [2] specifies the size
@@ -1296,6 +1296,8 @@ safety_comment! {
1296
1296
/// - The only value >= 1 for which 1 is an integer multiple is 1
1297
1297
/// Therefore, the only possible alignment for `u8` and `i8` is 1.
1298
1298
///
1299
+ /// TODO(#429): Add quotes from documentation.
1300
+ ///
1299
1301
/// [1] TODO(https://github.com/rust-lang/reference/issues/1291): Once the
1300
1302
/// reference explicitly guarantees these properties, cite it.
1301
1303
/// [2] https://doc.rust-lang.org/reference/type-layout.html#primitive-data-layout
@@ -1322,8 +1324,9 @@ safety_comment! {
1322
1324
/// - `AsBytes`: the `{f32,f64}::to_bits` methods' documentation [4,5]
1323
1325
/// states that they are currently equivalent to `transmute`. [3]
1324
1326
///
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.
1327
1330
///
1328
1331
/// [1] https://doc.rust-lang.org/nightly/std/primitive.f32.html#method.from_bits
1329
1332
/// [2] https://doc.rust-lang.org/nightly/std/primitive.f64.html#method.from_bits
@@ -1337,11 +1340,12 @@ safety_comment! {
1337
1340
1338
1341
safety_comment ! {
1339
1342
/// 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.
1345
1349
/// - `Unaligned`: Per the reference [1], "[a]n object with the boolean type
1346
1350
/// has a size and alignment of 1 each."
1347
1351
///
@@ -1351,28 +1355,28 @@ safety_comment! {
1351
1355
}
1352
1356
safety_comment ! {
1353
1357
/// 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`.
1362
1365
///
1363
1366
/// [1] https://doc.rust-lang.org/reference/types/textual.html
1364
1367
unsafe_impl!( char : FromZeroes , AsBytes ) ;
1365
1368
}
1366
1369
safety_comment ! {
1367
1370
/// 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`.
1372
1374
///
1373
1375
/// Note that we don't `assert_unaligned!(str)` because `assert_unaligned!`
1374
1376
/// uses `align_of`, which only works for `Sized` types.
1375
1377
///
1378
+ /// TODO(#429): Add quotes from documentation.
1379
+ ///
1376
1380
/// [1] https://doc.rust-lang.org/reference/type-layout.html#str-layout
1377
1381
unsafe_impl!( str : FromZeroes , AsBytes , Unaligned ) ;
1378
1382
}
@@ -1381,7 +1385,6 @@ safety_comment! {
1381
1385
// `NonZeroXxx` is `AsBytes`, but not `FromZeroes` or `FromBytes`.
1382
1386
//
1383
1387
/// SAFETY:
1384
- /// TODO(#429): Add quotes
1385
1388
/// - `AsBytes`: `NonZeroXxx` has the same layout as its associated
1386
1389
/// primitive. Since it is the same size, this guarantees it has no
1387
1390
/// padding - integers have no padding, and there's no room for padding
@@ -1396,6 +1399,8 @@ safety_comment! {
1396
1399
/// be 0 bytes, which means that they must be 1 byte. The only valid
1397
1400
/// alignment for a 1-byte type is 1.
1398
1401
///
1402
+ /// TODO(#429): Add quotes from documentation.
1403
+ ///
1399
1404
/// [1] https://doc.rust-lang.org/stable/std/num/struct.NonZeroU8.html
1400
1405
/// [2] https://doc.rust-lang.org/stable/std/num/struct.NonZeroI8.html
1401
1406
/// TODO(https://github.com/rust-lang/rust/pull/104082): Cite documentation
@@ -1416,7 +1421,6 @@ safety_comment! {
1416
1421
}
1417
1422
safety_comment ! {
1418
1423
/// SAFETY:
1419
- /// TODO(#429): Add quotes
1420
1424
/// - `FromZeroes`, `FromBytes`, `AsBytes`: The Rust compiler reuses `0`
1421
1425
/// value to represent `None`, so `size_of::<Option<NonZeroXxx>>() ==
1422
1426
/// size_of::<xxx>()`; see `NonZeroXxx` documentation.
@@ -1427,6 +1431,8 @@ safety_comment! {
1427
1431
/// unthinkable that that would ever change. The only valid alignment for
1428
1432
/// a 1-byte type is 1.
1429
1433
///
1434
+ /// TODO(#429): Add quotes from documentation.
1435
+ ///
1430
1436
/// [1] https://doc.rust-lang.org/stable/std/num/struct.NonZeroU8.html
1431
1437
/// [2] https://doc.rust-lang.org/stable/std/num/struct.NonZeroI8.html
1432
1438
///
@@ -1470,14 +1476,16 @@ safety_comment! {
1470
1476
}
1471
1477
safety_comment ! {
1472
1478
/// SAFETY:
1473
- /// TODO(#429): Add quotes
1474
1479
/// `Wrapping<T>` is guaranteed by its docs [1] to have the same layout as
1475
1480
/// `T`. Also, `Wrapping<T>` is `#[repr(transparent)]`, and has a single
1476
1481
/// field, which is `pub`. Per the reference [2], this means that the
1477
1482
/// `#[repr(transparent)]` attribute is "considered part of the public ABI".
1478
1483
///
1479
- /// references nightly?:
1484
+ /// TODO(#429): Add quotes from documentation.
1485
+ ///
1480
1486
/// [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
+ ///
1481
1489
/// [2] https://doc.rust-lang.org/nomicon/other-reprs.html#reprtransparent
1482
1490
unsafe_impl!( T : FromZeroes => FromZeroes for Wrapping <T >) ;
1483
1491
unsafe_impl!( T : FromBytes => FromBytes for Wrapping <T >) ;
@@ -1496,8 +1504,8 @@ safety_comment! {
1496
1504
/// Thus, we require `T: FromZeroes` and `T: FromBytes` in order to ensure
1497
1505
/// that `T` - and thus `MaybeUninit<T>` - contains to `UnsafeCell`s.
1498
1506
/// 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]
1501
1509
///
1502
1510
/// [1] https://doc.rust-lang.org/stable/core/mem/union.MaybeUninit.html#layout-1
1503
1511
///
@@ -1512,7 +1520,6 @@ safety_comment! {
1512
1520
}
1513
1521
safety_comment ! {
1514
1522
/// SAFETY:
1515
- // TODO(#429): Add reference to docs + quotes
1516
1523
/// `ManuallyDrop` has the same layout as `T`, and accessing the inner value
1517
1524
/// is safe (meaning that it's unsound to leave the inner value
1518
1525
/// uninitialized while exposing the `ManuallyDrop` to safe code).
@@ -1528,6 +1535,7 @@ safety_comment! {
1528
1535
/// code can only ever access a `ManuallyDrop` with all initialized bytes.
1529
1536
/// - `Unaligned`: `ManuallyDrop` has the same layout (and thus alignment)
1530
1537
/// as `T`, and `T: Unaligned` guarantees that that alignment is 1.
1538
+ /// TODO(#429): Add references to docs and quotes.
1531
1539
unsafe_impl!( T : ?Sized + FromZeroes => FromZeroes for ManuallyDrop <T >) ;
1532
1540
unsafe_impl!( T : ?Sized + FromBytes => FromBytes for ManuallyDrop <T >) ;
1533
1541
unsafe_impl!( T : ?Sized + AsBytes => AsBytes for ManuallyDrop <T >) ;
0 commit comments