Skip to content

Commit 44513f7

Browse files
Add TODOs related to #429 and quotes to safety comments. (#433)
* Add TODOs related to #429 and quotes to safety comments. * Update src/lib.rs * Update src/lib.rs * Update src/lib.rs --------- Co-authored-by: Joshua Liebow-Feeser <joshlf@users.noreply.github.com>
1 parent dcadae6 commit 44513f7

File tree

1 file changed

+54
-24
lines changed

1 file changed

+54
-24
lines changed

src/lib.rs

+54-24
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,9 @@ safety_comment! {
732732
/// `ManuallyDrop<T>` is guaranteed to have the same layout and bit
733733
/// validity as `T`
734734
///
735-
/// TODO(#429): Once this text (added in
735+
/// TODO(#429):
736+
/// - Add quotes from docs.
737+
/// - Once [1] (added in
736738
/// https://github.com/rust-lang/rust/pull/115522) is available on stable,
737739
/// quote the stable docs instead of the nightly docs.
738740
unsafe_impl_known_layout!(#[repr([u8])] str);
@@ -965,6 +967,8 @@ pub unsafe trait FromZeroes {
965967
// as required by `u8`.
966968
// - Since `Self: FromZeroes`, the all-zeroes instance is a valid
967969
// instance of `Self.`
970+
//
971+
// TODO(#429): Add references to docs and quotes.
968972
unsafe { ptr::write_bytes(slf.cast::<u8>(), 0, len) };
969973
}
970974

@@ -1605,6 +1609,8 @@ pub unsafe trait AsBytes {
16051609
// - The total size of the resulting slice is no larger than
16061610
// `isize::MAX` because no allocation produced by safe code can be
16071611
// larger than `isize::MAX`.
1612+
//
1613+
// TODO(#429): Add references to docs and quotes.
16081614
unsafe { slice::from_raw_parts(slf.cast::<u8>(), len) }
16091615
}
16101616

@@ -1640,6 +1646,8 @@ pub unsafe trait AsBytes {
16401646
// - The total size of the resulting slice is no larger than
16411647
// `isize::MAX` because no allocation produced by safe code can be
16421648
// larger than `isize::MAX`.
1649+
//
1650+
// TODO(#429): Add references to docs and quotes.
16431651
unsafe { slice::from_raw_parts_mut(slf.cast::<u8>(), len) }
16441652
}
16451653

@@ -1736,6 +1744,8 @@ safety_comment! {
17361744
/// - The only value >= 1 for which 1 is an integer multiple is 1
17371745
/// Therefore, the only possible alignment for `u8` and `i8` is 1.
17381746
///
1747+
/// TODO(#429): Add quotes from documentation.
1748+
///
17391749
/// [1] TODO(https://github.com/rust-lang/reference/issues/1291): Once the
17401750
/// reference explicitly guarantees these properties, cite it.
17411751
/// [2] https://doc.rust-lang.org/reference/type-layout.html#primitive-data-layout
@@ -1762,7 +1772,9 @@ safety_comment! {
17621772
/// - `AsBytes`: the `{f32,f64}::to_bits` methods' documentation [4,5]
17631773
/// states that they are currently equivalent to `transmute`. [3]
17641774
///
1765-
/// TODO: Make these arguments more precisely in terms of the documentation.
1775+
/// TODO(#429):
1776+
/// - Make these arguments more precisely in terms of the documentation.
1777+
/// - Add quotes from documentation.
17661778
///
17671779
/// [1] https://doc.rust-lang.org/nightly/std/primitive.f32.html#method.from_bits
17681780
/// [2] https://doc.rust-lang.org/nightly/std/primitive.f64.html#method.from_bits
@@ -1776,11 +1788,12 @@ safety_comment! {
17761788

17771789
safety_comment! {
17781790
/// SAFETY:
1779-
/// - `FromZeroes`: Per the reference [1], 0x00 is a valid bit pattern for
1780-
/// `bool`.
1781-
/// - `AsBytes`: Per the reference [1], `bool` always has a size of 1 with
1782-
/// valid bit patterns 0x01 and 0x00, so the only byte of the bool is
1783-
/// always initialized
1791+
/// - `FromZeroes`: Valid since "[t]he value false has the bit pattern
1792+
/// 0x00" [1].
1793+
/// - `AsBytes`: Since "the boolean type has a size and alignment of 1 each"
1794+
/// and "The value false has the bit pattern 0x00 and the value true has
1795+
/// the bit pattern 0x01" [1]. Thus, the only byte of the bool is always
1796+
/// initialized.
17841797
/// - `Unaligned`: Per the reference [1], "[a]n object with the boolean type
17851798
/// has a size and alignment of 1 each."
17861799
///
@@ -1790,24 +1803,28 @@ safety_comment! {
17901803
}
17911804
safety_comment! {
17921805
/// SAFETY:
1793-
/// - `FromZeroes`: Per the reference [1], 0x0000 is a valid bit pattern for
1794-
/// `char`.
1795-
/// - `AsBytes`: `char` is represented as a 32-bit unsigned word (`u32`)
1796-
/// [1], which is `AsBytes`. Note that unlike `u32`, not all bit patterns
1797-
/// are valid for `char`.
1806+
/// - `FromZeroes`: Per reference [1], "[a] value of type char is a Unicode
1807+
/// scalar value (i.e. a code point that is not a surrogate), represented
1808+
/// as a 32-bit unsigned word in the 0x0000 to 0xD7FF or 0xE000 to
1809+
/// 0x10FFFF range" which contains 0x0000.
1810+
/// - `AsBytes`: `char` is per reference [1] "represented as a 32-bit
1811+
/// unsigned word" (`u32`) which is `AsBytes`. Note that unlike `u32`, not
1812+
/// all bit patterns are valid for `char`.
17981813
///
17991814
/// [1] https://doc.rust-lang.org/reference/types/textual.html
18001815
unsafe_impl!(char: FromZeroes, AsBytes);
18011816
}
18021817
safety_comment! {
18031818
/// SAFETY:
1804-
/// - `FromZeroes`, `AsBytes`, `Unaligned`: Per the reference [1], `str` has
1805-
/// the same layout as `[u8]`, and `[u8]` is `FromZeroes`, `AsBytes`, and
1806-
/// `Unaligned`.
1819+
/// - `FromZeroes`, `AsBytes`, `Unaligned`: Per the reference [1], `str`
1820+
/// has the same layout as `[u8]`, and `[u8]` is `FromZeroes`, `AsBytes`,
1821+
/// and `Unaligned`.
18071822
///
18081823
/// Note that we don't `assert_unaligned!(str)` because `assert_unaligned!`
18091824
/// uses `align_of`, which only works for `Sized` types.
18101825
///
1826+
/// TODO(#429): Add quotes from documentation.
1827+
///
18111828
/// [1] https://doc.rust-lang.org/reference/type-layout.html#str-layout
18121829
unsafe_impl!(str: FromZeroes, AsBytes, Unaligned);
18131830
}
@@ -1830,6 +1847,8 @@ safety_comment! {
18301847
/// be 0 bytes, which means that they must be 1 byte. The only valid
18311848
/// alignment for a 1-byte type is 1.
18321849
///
1850+
/// TODO(#429): Add quotes from documentation.
1851+
///
18331852
/// [1] https://doc.rust-lang.org/stable/std/num/struct.NonZeroU8.html
18341853
/// [2] https://doc.rust-lang.org/stable/std/num/struct.NonZeroI8.html
18351854
/// TODO(https://github.com/rust-lang/rust/pull/104082): Cite documentation
@@ -1860,6 +1879,8 @@ safety_comment! {
18601879
/// unthinkable that that would ever change. The only valid alignment for
18611880
/// a 1-byte type is 1.
18621881
///
1882+
/// TODO(#429): Add quotes from documentation.
1883+
///
18631884
/// [1] https://doc.rust-lang.org/stable/std/num/struct.NonZeroU8.html
18641885
/// [2] https://doc.rust-lang.org/stable/std/num/struct.NonZeroI8.html
18651886
///
@@ -1913,7 +1934,11 @@ safety_comment! {
19131934

19141935
safety_comment! {
19151936
/// SAFETY:
1916-
/// For all `T`, `PhantomData<T>` has size 0 and alignment 1. [1]
1937+
/// Per reference [1]:
1938+
/// "For all T, the following are guaranteed:
1939+
/// size_of::<PhantomData<T>>() == 0
1940+
/// align_of::<PhantomData<T>>() == 1".
1941+
/// This gives:
19171942
/// - `FromZeroes`, `FromBytes`: There is only one possible sequence of 0
19181943
/// bytes, and `PhantomData` is inhabited.
19191944
/// - `AsBytes`: Since `PhantomData` has size 0, it contains no padding
@@ -1935,7 +1960,11 @@ safety_comment! {
19351960
/// field, which is `pub`. Per the reference [2], this means that the
19361961
/// `#[repr(transparent)]` attribute is "considered part of the public ABI".
19371962
///
1938-
/// [1] https://doc.rust-lang.org/nightly/core/num/struct.Wrapping.html#layout-1
1963+
/// TODO(#429): Add quotes from documentation.
1964+
///
1965+
/// [1] TODO(https://doc.rust-lang.org/nightly/core/num/struct.Wrapping.html#layout-1):
1966+
/// Reference this documentation once it's available on stable.
1967+
///
19391968
/// [2] https://doc.rust-lang.org/nomicon/other-reprs.html#reprtransparent
19401969
unsafe_impl!(T: FromZeroes => FromZeroes for Wrapping<T>);
19411970
unsafe_impl!(T: FromBytes => FromBytes for Wrapping<T>);
@@ -1954,11 +1983,10 @@ safety_comment! {
19541983
/// Thus, we require `T: FromZeroes` and `T: FromBytes` in order to ensure
19551984
/// that `T` - and thus `MaybeUninit<T>` - contains to `UnsafeCell`s.
19561985
/// Thus, requiring that `T` implement each of these traits is sufficient
1957-
/// - `Unaligned`: `MaybeUninit<T>` is guaranteed by its documentation [1]
1958-
/// to have the same alignment as `T`.
1986+
/// - `Unaligned`: "MaybeUninit<T> is guaranteed to have the same size,
1987+
/// alignment, and ABI as T" [1]
19591988
///
1960-
/// [1]
1961-
/// https://doc.rust-lang.org/nightly/core/mem/union.MaybeUninit.html#layout-1
1989+
/// [1] https://doc.rust-lang.org/stable/core/mem/union.MaybeUninit.html#layout-1
19621990
///
19631991
/// TODO(https://github.com/google/zerocopy/issues/251): If we split
19641992
/// `FromBytes` and `RefFromBytes`, or if we introduce a separate
@@ -1988,12 +2016,14 @@ safety_comment! {
19882016
/// - `Unaligned`: `ManuallyDrop` has the same layout (and thus alignment)
19892017
/// as `T`, and `T: Unaligned` guarantees that that alignment is 1.
19902018
///
1991-
/// [1] Per https://doc.rust-lang.org/nightly/core/mem/struct.ManuallyDrop.html:
1992-
///
19932019
/// `ManuallyDrop<T>` is guaranteed to have the same layout and bit
19942020
/// validity as `T`
19952021
///
1996-
/// TODO(#429): Once this text (added in
2022+
/// [1] Per https://doc.rust-lang.org/nightly/core/mem/struct.ManuallyDrop.html:
2023+
///
2024+
/// TODO(#429):
2025+
/// - Add quotes from docs.
2026+
/// - Once [1] (added in
19972027
/// https://github.com/rust-lang/rust/pull/115522) is available on stable,
19982028
/// quote the stable docs instead of the nightly docs.
19992029
unsafe_impl!(T: ?Sized + FromZeroes => FromZeroes for ManuallyDrop<T>);

0 commit comments

Comments
 (0)