Skip to content

Commit b49a953

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.
1 parent 6b67c57 commit b49a953

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

src/lib.rs

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,18 +1230,35 @@ safety_comment! {
12301230

12311231
safety_comment! {
12321232
/// SAFETY:
1233-
/// - `FromZeroes`, `FromBytes`: all bit patterns are valid for integers [1]
1234-
/// - `AsBytes`: integers have no padding bytes [1]
1233+
/// - `FromZeroes`, `FromBytes`: all bit patterns are valid for numeric
1234+
/// types [1]
1235+
/// - `AsBytes`: numeric types have no padding bytes [1]
12351236
/// - `Unaligned` (`u8` and `i8` only): The reference [2] specifies the size
12361237
/// of `u8` and `i8` as 1 byte. We also know that:
1237-
/// - Alignment is >= 1
1238-
/// - Size is an integer multiple of alignment
1238+
/// - Alignment is >= 1 [3]
1239+
/// - Size is an integer multiple of alignment [4]
12391240
/// - The only value >= 1 for which 1 is an integer multiple is 1
12401241
/// Therefore, the only possible alignment for `u8` and `i8` is 1.
12411242
///
1242-
/// [1] TODO(https://github.com/rust-lang/reference/issues/1291): Once the
1243-
/// reference explicitly guarantees these properties, cite it.
1243+
/// [1] Per https://doc.rust-lang.org/reference/types/numeric.html#bit-validity:
1244+
///
1245+
/// For every numeric type, `T`, the bit validity of `T` is equivalent to
1246+
/// the bit validity of `[u8; size_of::<T>()]`. An uninitialized byte is
1247+
/// not a valid `u8`.
1248+
///
12441249
/// [2] https://doc.rust-lang.org/reference/type-layout.html#primitive-data-layout
1250+
///
1251+
/// [3] Per https://doc.rust-lang.org/reference/type-layout.html#size-and-alignment:
1252+
///
1253+
/// Alignment is measured in bytes, and must be at least 1.
1254+
///
1255+
/// [4] Per https://doc.rust-lang.org/reference/type-layout.html#size-and-alignment:
1256+
///
1257+
/// The size of a value is always a multiple of its alignment.
1258+
///
1259+
/// TODO(#278): Once we've updated the trait docs to refer to `u8`s rather
1260+
/// than bits or bytes, update this comment, especially the reference to
1261+
/// [1].
12451262
unsafe_impl!(u8: FromZeroes, FromBytes, AsBytes, Unaligned);
12461263
unsafe_impl!(i8: FromZeroes, FromBytes, AsBytes, Unaligned);
12471264
assert_unaligned!(u8, i8);
@@ -1255,24 +1272,6 @@ safety_comment! {
12551272
unsafe_impl!(i128: FromZeroes, FromBytes, AsBytes);
12561273
unsafe_impl!(usize: FromZeroes, FromBytes, AsBytes);
12571274
unsafe_impl!(isize: FromZeroes, FromBytes, AsBytes);
1258-
}
1259-
1260-
safety_comment! {
1261-
/// SAFETY:
1262-
/// - `FromZeroes`, `FromBytes`: the `{f32,f64}::from_bits` constructors'
1263-
/// documentation [1,2] states that they are currently equivalent to
1264-
/// `transmute`. [3]
1265-
/// - `AsBytes`: the `{f32,f64}::to_bits` methods' documentation [4,5]
1266-
/// states that they are currently equivalent to `transmute`. [3]
1267-
///
1268-
/// TODO: Make these arguments more precisely in terms of the documentation.
1269-
///
1270-
/// [1] https://doc.rust-lang.org/nightly/std/primitive.f32.html#method.from_bits
1271-
/// [2] https://doc.rust-lang.org/nightly/std/primitive.f64.html#method.from_bits
1272-
/// [3] TODO(https://github.com/rust-lang/reference/issues/1291): Once the
1273-
/// reference explicitly guarantees these properties, cite it.
1274-
/// [4] https://doc.rust-lang.org/nightly/std/primitive.f32.html#method.to_bits
1275-
/// [5] https://doc.rust-lang.org/nightly/std/primitive.f64.html#method.to_bits
12761275
unsafe_impl!(f32: FromZeroes, FromBytes, AsBytes);
12771276
unsafe_impl!(f64: FromZeroes, FromBytes, AsBytes);
12781277
}

0 commit comments

Comments
 (0)