Skip to content

Commit 3effe73

Browse files
committed
Safety comments quote new bit validity guarantees
rust-lang/reference#1392 adds bit validity guarantees for numeric types. This commit makes use of those guarantees to provide stronger soundness justifications for some trait impls. Closes #440
1 parent ac90bb5 commit 3effe73

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

src/lib.rs

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1734,20 +1734,38 @@ safety_comment! {
17341734

17351735
safety_comment! {
17361736
/// SAFETY:
1737-
/// - `FromZeroes`, `FromBytes`: all bit patterns are valid for integers [1]
1738-
/// - `AsBytes`: integers have no padding bytes [1]
1737+
/// - `FromZeroes`, `FromBytes`: all bit patterns are valid for numeric
1738+
/// types [1]
1739+
/// - `AsBytes`: numeric types have no padding bytes [1]
17391740
/// - `Unaligned` (`u8` and `i8` only): The reference [2] specifies the size
17401741
/// of `u8` and `i8` as 1 byte. We also know that:
1741-
/// - Alignment is >= 1
1742-
/// - Size is an integer multiple of alignment
1742+
/// - Alignment is >= 1 [3]
1743+
/// - Size is an integer multiple of alignment [4]
17431744
/// - The only value >= 1 for which 1 is an integer multiple is 1
17441745
/// Therefore, the only possible alignment for `u8` and `i8` is 1.
17451746
///
1746-
/// TODO(#429): Add quotes from documentation.
1747+
/// [1] Per https://doc.rust-lang.org/beta/reference/types/numeric.html#bit-validity:
1748+
///
1749+
/// For every numeric type, `T`, the bit validity of `T` is equivalent to
1750+
/// the bit validity of `[u8; size_of::<T>()]`. An uninitialized byte is
1751+
/// not a valid `u8`.
1752+
///
1753+
/// TODO(https://github.com/rust-lang/reference/pull/1392): Once this text
1754+
/// is available on the Stable docs, cite those instead.
17471755
///
1748-
/// [1] TODO(https://github.com/rust-lang/reference/issues/1291): Once the
1749-
/// reference explicitly guarantees these properties, cite it.
17501756
/// [2] https://doc.rust-lang.org/reference/type-layout.html#primitive-data-layout
1757+
///
1758+
/// [3] Per https://doc.rust-lang.org/reference/type-layout.html#size-and-alignment:
1759+
///
1760+
/// Alignment is measured in bytes, and must be at least 1.
1761+
///
1762+
/// [4] Per https://doc.rust-lang.org/reference/type-layout.html#size-and-alignment:
1763+
///
1764+
/// The size of a value is always a multiple of its alignment.
1765+
///
1766+
/// TODO(#278): Once we've updated the trait docs to refer to `u8`s rather
1767+
/// than bits or bytes, update this comment, especially the reference to
1768+
/// [1].
17511769
unsafe_impl!(u8: FromZeroes, FromBytes, AsBytes, Unaligned);
17521770
unsafe_impl!(i8: FromZeroes, FromBytes, AsBytes, Unaligned);
17531771
assert_unaligned!(u8, i8);
@@ -1761,26 +1779,6 @@ safety_comment! {
17611779
unsafe_impl!(i128: FromZeroes, FromBytes, AsBytes);
17621780
unsafe_impl!(usize: FromZeroes, FromBytes, AsBytes);
17631781
unsafe_impl!(isize: FromZeroes, FromBytes, AsBytes);
1764-
}
1765-
1766-
safety_comment! {
1767-
/// SAFETY:
1768-
/// - `FromZeroes`, `FromBytes`: the `{f32,f64}::from_bits` constructors'
1769-
/// documentation [1,2] states that they are currently equivalent to
1770-
/// `transmute`. [3]
1771-
/// - `AsBytes`: the `{f32,f64}::to_bits` methods' documentation [4,5]
1772-
/// states that they are currently equivalent to `transmute`. [3]
1773-
///
1774-
/// TODO(#429):
1775-
/// - Make these arguments more precisely in terms of the documentation.
1776-
/// - Add quotes from documentation.
1777-
///
1778-
/// [1] https://doc.rust-lang.org/nightly/std/primitive.f32.html#method.from_bits
1779-
/// [2] https://doc.rust-lang.org/nightly/std/primitive.f64.html#method.from_bits
1780-
/// [3] TODO(https://github.com/rust-lang/reference/issues/1291): Once the
1781-
/// reference explicitly guarantees these properties, cite it.
1782-
/// [4] https://doc.rust-lang.org/nightly/std/primitive.f32.html#method.to_bits
1783-
/// [5] https://doc.rust-lang.org/nightly/std/primitive.f64.html#method.to_bits
17841782
unsafe_impl!(f32: FromZeroes, FromBytes, AsBytes);
17851783
unsafe_impl!(f64: FromZeroes, FromBytes, AsBytes);
17861784
}

0 commit comments

Comments
 (0)