In order to provide `// SAFETY` comments for every `unsafe impl`, we would need to repeat them, which is not very useful and would be harder to read. We could perhaps allow the lint (ideally within a small module), but we can take the chance to avoid the repetition of the `impl`s themselves too by using a small local macro, like in other places where we have had to do this sort of thing. Thus add the straightforward `impl_{from,as}bytes!` macros and use them to implement `FromBytes`. This, in turn, will allow us in the next patch to place a `// SAFETY` comment that defers to the actual invocation of the macro.
Unified · Split
| @@ -490,21 +490,22 @@ pub enum Either<L, R> { · 489 unmodified lines | |||
| 490 | /// All bit-patterns must be valid for this type. This type must not have interior mutability. | 490 | /// All bit-patterns must be valid for this type. This type must not have interior mutability. |
| 491 | pub unsafe trait FromBytes {} | 491 | pub unsafe trait FromBytes {} |
| 492 | 492 | ||
| 493 | // SAFETY: All bit patterns are acceptable values of the types below. | 493 | macro_rules! impl_frombytes { |
| 494 | unsafe impl FromBytes for u8 {} | 494 | ($($({$($generics:tt)*})? $t:ty, )*) => { |
| 495 | unsafe impl FromBytes for u16 {} | 495 | $(unsafe impl$($($generics)*)? FromBytes for $t {})* |
| 496 | unsafe impl FromBytes for u32 {} | 496 | }; |
| 497 | unsafe impl FromBytes for u64 {} | 497 | } |
| 498 | unsafe impl FromBytes for usize {} | 498 | |
| 499 | unsafe impl FromBytes for i8 {} | 499 | impl_frombytes! { |
| 500 | unsafe impl FromBytes for i16 {} | 500 | // SAFETY: All bit patterns are acceptable values of the types below. |
| 501 | unsafe impl FromBytes for i32 {} | 501 | u8, u16, u32, u64, usize, |
| 502 | unsafe impl FromBytes for i64 {} | 502 | i8, i16, i32, i64, isize, |
| 503 | unsafe impl FromBytes for isize {} | 503 | |
| 504 | // SAFETY: If all bit patterns are acceptable for individual values in an array, then all bit | 504 | // SAFETY: If all bit patterns are acceptable for individual values in an array, then all bit |
| 505 | // patterns are also acceptable for arrays of that type. | 505 | // patterns are also acceptable for arrays of that type. |
| 506 | unsafe impl<T: FromBytes> FromBytes for [T] {} | 506 | {<T: FromBytes>} [T], |
| 507 | unsafe impl<T: FromBytes, const N: usize> FromBytes for [T; N] {} | 507 | {<T: FromBytes, const N: usize>} [T; N], |
| 508 | } | ||
| 508 | 509 | ||
| 509 | /// Types that can be viewed as an immutable slice of initialized bytes. | 510 | /// Types that can be viewed as an immutable slice of initialized bytes. |
| 510 | /// | 511 | /// |
| @@ -523,21 +524,22 @@ unsafe impl<T: FromBytes, const N: usize> FromBytes for [T; N] {} · 12 unmodified lines | |||
| 523 | /// mutability. | 524 | /// mutability. |
| 524 | pub unsafe trait AsBytes {} | 525 | pub unsafe trait AsBytes {} |
| 525 | 526 | ||
| 526 | // SAFETY: Instances of the following types have no uninitialized portions. | 527 | macro_rules! impl_asbytes { |
| 527 | unsafe impl AsBytes for u8 {} | 528 | ($($({$($generics:tt)*})? $t:ty, )*) => { |
| 528 | unsafe impl AsBytes for u16 {} | 529 | $(unsafe impl$($($generics)*)? AsBytes for $t {})* |
| 529 | unsafe impl AsBytes for u32 {} | 530 | }; |
| 530 | unsafe impl AsBytes for u64 {} | 531 | } |
| 531 | unsafe impl AsBytes for usize {} | 532 | |
| 532 | unsafe impl AsBytes for i8 {} | 533 | impl_asbytes! { |
| 533 | unsafe impl AsBytes for i16 {} | 534 | // SAFETY: Instances of the following types have no uninitialized portions. |
| 534 | unsafe impl AsBytes for i32 {} | 535 | u8, u16, u32, u64, usize, |
| 535 | unsafe impl AsBytes for i64 {} | 536 | i8, i16, i32, i64, isize, |
| 536 | unsafe impl AsBytes for isize {} | 537 | bool, |
| 537 | unsafe impl AsBytes for bool {} | 538 | char, |
| 538 | unsafe impl AsBytes for char {} | 539 | str, |
| 539 | unsafe impl AsBytes for str {} | 540 | |
| 540 | // SAFETY: If individual values in an array have no uninitialized portions, then the array itself | 541 | // SAFETY: If individual values in an array have no uninitialized portions, then the array |
| 541 | // does not have any uninitialized portions either. | 542 | // itself does not have any uninitialized portions either. |
| 542 | unsafe impl<T: AsBytes> AsBytes for [T] {} | 543 | {<T: AsBytes>} [T], |
| 543 | unsafe impl<T: AsBytes, const N: usize> AsBytes for [T; N] {} | 544 | {<T: AsBytes, const N: usize>} [T; N], |
| 545 | } | ||