@@ -656,12 +656,14 @@ safety_comment! {
656
656
/// `[u8]` and `[T]` repsectively. `str` has different bit validity than
657
657
/// `[u8]`, but that doesn't affect the soundness of this impl.
658
658
///
659
- /// [1] Per https://doc.rust-lang.org/nightly/core/mem/struct.ManuallyDrop.html:
660
- ///
661
659
/// `ManuallyDrop<T>` is guaranteed to have the same layout and bit
662
660
/// validity as `T`
663
661
///
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
665
667
/// https://github.com/rust-lang/rust/pull/115522) is available on stable,
666
668
/// quote the stable docs instead of the nightly docs.
667
669
unsafe_impl_known_layout!( #[ repr( [ u8 ] ) ] str ) ;
@@ -771,6 +773,8 @@ pub unsafe trait FromZeroes {
771
773
// as required by `u8`.
772
774
// - Since `Self: FromZeroes`, the all-zeroes instance is a valid
773
775
// instance of `Self.`
776
+ //
777
+ // TODO(#429): Add references to docs and quotes.
774
778
unsafe { ptr:: write_bytes ( slf. cast :: < u8 > ( ) , 0 , len) } ;
775
779
}
776
780
@@ -1170,6 +1174,8 @@ pub unsafe trait AsBytes {
1170
1174
// - The total size of the resulting slice is no larger than
1171
1175
// `isize::MAX` because no allocation produced by safe code can be
1172
1176
// larger than `isize::MAX`.
1177
+ //
1178
+ // TODO(#429): Add references to docs and quotes.
1173
1179
unsafe { slice:: from_raw_parts ( slf. cast :: < u8 > ( ) , len) }
1174
1180
}
1175
1181
@@ -1205,6 +1211,8 @@ pub unsafe trait AsBytes {
1205
1211
// - The total size of the resulting slice is no larger than
1206
1212
// `isize::MAX` because no allocation produced by safe code can be
1207
1213
// larger than `isize::MAX`.
1214
+ //
1215
+ // TODO(#429): Add references to docs and quotes.
1208
1216
unsafe { slice:: from_raw_parts_mut ( slf. cast :: < u8 > ( ) , len) }
1209
1217
}
1210
1218
@@ -1301,6 +1309,8 @@ safety_comment! {
1301
1309
/// - The only value >= 1 for which 1 is an integer multiple is 1
1302
1310
/// Therefore, the only possible alignment for `u8` and `i8` is 1.
1303
1311
///
1312
+ /// TODO(#429): Add quotes from documentation.
1313
+ ///
1304
1314
/// [1] TODO(https://github.com/rust-lang/reference/issues/1291): Once the
1305
1315
/// reference explicitly guarantees these properties, cite it.
1306
1316
/// [2] https://doc.rust-lang.org/reference/type-layout.html#primitive-data-layout
@@ -1327,7 +1337,9 @@ safety_comment! {
1327
1337
/// - `AsBytes`: the `{f32,f64}::to_bits` methods' documentation [4,5]
1328
1338
/// states that they are currently equivalent to `transmute`. [3]
1329
1339
///
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.
1331
1343
///
1332
1344
/// [1] https://doc.rust-lang.org/nightly/std/primitive.f32.html#method.from_bits
1333
1345
/// [2] https://doc.rust-lang.org/nightly/std/primitive.f64.html#method.from_bits
@@ -1341,11 +1353,12 @@ safety_comment! {
1341
1353
1342
1354
safety_comment ! {
1343
1355
/// 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.
1349
1362
/// - `Unaligned`: Per the reference [1], "[a]n object with the boolean type
1350
1363
/// has a size and alignment of 1 each."
1351
1364
///
@@ -1355,24 +1368,28 @@ safety_comment! {
1355
1368
}
1356
1369
safety_comment ! {
1357
1370
/// 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`.
1363
1378
///
1364
1379
/// [1] https://doc.rust-lang.org/reference/types/textual.html
1365
1380
unsafe_impl!( char : FromZeroes , AsBytes ) ;
1366
1381
}
1367
1382
safety_comment ! {
1368
1383
/// 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`.
1372
1387
///
1373
1388
/// Note that we don't `assert_unaligned!(str)` because `assert_unaligned!`
1374
1389
/// uses `align_of`, which only works for `Sized` types.
1375
1390
///
1391
+ /// TODO(#429): Add quotes from documentation.
1392
+ ///
1376
1393
/// [1] https://doc.rust-lang.org/reference/type-layout.html#str-layout
1377
1394
unsafe_impl!( str : FromZeroes , AsBytes , Unaligned ) ;
1378
1395
}
@@ -1395,6 +1412,8 @@ safety_comment! {
1395
1412
/// be 0 bytes, which means that they must be 1 byte. The only valid
1396
1413
/// alignment for a 1-byte type is 1.
1397
1414
///
1415
+ /// TODO(#429): Add quotes from documentation.
1416
+ ///
1398
1417
/// [1] https://doc.rust-lang.org/stable/std/num/struct.NonZeroU8.html
1399
1418
/// [2] https://doc.rust-lang.org/stable/std/num/struct.NonZeroI8.html
1400
1419
/// TODO(https://github.com/rust-lang/rust/pull/104082): Cite documentation
@@ -1425,6 +1444,8 @@ safety_comment! {
1425
1444
/// unthinkable that that would ever change. The only valid alignment for
1426
1445
/// a 1-byte type is 1.
1427
1446
///
1447
+ /// TODO(#429): Add quotes from documentation.
1448
+ ///
1428
1449
/// [1] https://doc.rust-lang.org/stable/std/num/struct.NonZeroU8.html
1429
1450
/// [2] https://doc.rust-lang.org/stable/std/num/struct.NonZeroI8.html
1430
1451
///
@@ -1447,7 +1468,11 @@ safety_comment! {
1447
1468
1448
1469
safety_comment ! {
1449
1470
/// 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:
1451
1476
/// - `FromZeroes`, `FromBytes`: There is only one possible sequence of 0
1452
1477
/// bytes, and `PhantomData` is inhabited.
1453
1478
/// - `AsBytes`: Since `PhantomData` has size 0, it contains no padding
@@ -1469,7 +1494,11 @@ safety_comment! {
1469
1494
/// field, which is `pub`. Per the reference [2], this means that the
1470
1495
/// `#[repr(transparent)]` attribute is "considered part of the public ABI".
1471
1496
///
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
+ ///
1473
1502
/// [2] https://doc.rust-lang.org/nomicon/other-reprs.html#reprtransparent
1474
1503
unsafe_impl!( T : FromZeroes => FromZeroes for Wrapping <T >) ;
1475
1504
unsafe_impl!( T : FromBytes => FromBytes for Wrapping <T >) ;
@@ -1488,11 +1517,10 @@ safety_comment! {
1488
1517
/// Thus, we require `T: FromZeroes` and `T: FromBytes` in order to ensure
1489
1518
/// that `T` - and thus `MaybeUninit<T>` - contains to `UnsafeCell`s.
1490
1519
/// 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]
1493
1522
///
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
1496
1524
///
1497
1525
/// TODO(https://github.com/google/zerocopy/issues/251): If we split
1498
1526
/// `FromBytes` and `RefFromBytes`, or if we introduce a separate
@@ -1522,12 +1550,14 @@ safety_comment! {
1522
1550
/// - `Unaligned`: `ManuallyDrop` has the same layout (and thus alignment)
1523
1551
/// as `T`, and `T: Unaligned` guarantees that that alignment is 1.
1524
1552
///
1525
- /// [1] Per https://doc.rust-lang.org/nightly/core/mem/struct.ManuallyDrop.html:
1526
- ///
1527
1553
/// `ManuallyDrop<T>` is guaranteed to have the same layout and bit
1528
1554
/// validity as `T`
1529
1555
///
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
1531
1561
/// https://github.com/rust-lang/rust/pull/115522) is available on stable,
1532
1562
/// quote the stable docs instead of the nightly docs.
1533
1563
unsafe_impl!( T : ?Sized + FromZeroes => FromZeroes for ManuallyDrop <T >) ;
0 commit comments