History

rust: types: avoid repetition in `{As,From}Bytes` impls

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.

Miguel Ojeda committed 2 years ago

Commit 567cdff1 parent a135aa31 file · +35 −33Browse files

Unified · Split

rust/kernel/types.rs+35−33
@@ -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.
491pub unsafe trait FromBytes {}491pub unsafe trait FromBytes {}
492 492
493// SAFETY: All bit patterns are acceptable values of the types below.493macro_rules! impl_frombytes {
494unsafe impl FromBytes for u8 {}494 ($($({$($generics:tt)*})? $t:ty, )*) => {
495unsafe impl FromBytes for u16 {}495 $(unsafe impl$($($generics)*)? FromBytes for $t {})*
496unsafe impl FromBytes for u32 {}496 };
497unsafe impl FromBytes for u64 {}497}
498unsafe impl FromBytes for usize {}498
499unsafe impl FromBytes for i8 {}499impl_frombytes! {
500unsafe impl FromBytes for i16 {}500 // SAFETY: All bit patterns are acceptable values of the types below.
501unsafe impl FromBytes for i32 {}501 u8, u16, u32, u64, usize,
502unsafe impl FromBytes for i64 {}502 i8, i16, i32, i64, isize,
503unsafe impl FromBytes for isize {}503
504// SAFETY: If all bit patterns are acceptable for individual values in an array, then all bit504 // 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.
506unsafe impl<T: FromBytes> FromBytes for [T] {}506 {<T: FromBytes>} [T],
507unsafe 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.
524pub unsafe trait AsBytes {}525pub unsafe trait AsBytes {}
525 526
526// SAFETY: Instances of the following types have no uninitialized portions.527macro_rules! impl_asbytes {
527unsafe impl AsBytes for u8 {}528 ($($({$($generics:tt)*})? $t:ty, )*) => {
528unsafe impl AsBytes for u16 {}529 $(unsafe impl$($($generics)*)? AsBytes for $t {})*
529unsafe impl AsBytes for u32 {}530 };
530unsafe impl AsBytes for u64 {}531}
531unsafe impl AsBytes for usize {}532
532unsafe impl AsBytes for i8 {}533impl_asbytes! {
533unsafe impl AsBytes for i16 {}534 // SAFETY: Instances of the following types have no uninitialized portions.
534unsafe impl AsBytes for i32 {}535 u8, u16, u32, u64, usize,
535unsafe impl AsBytes for i64 {}536 i8, i16, i32, i64, isize,
536unsafe impl AsBytes for isize {}537 bool,
537unsafe impl AsBytes for bool {}538 char,
538unsafe impl AsBytes for char {}539 str,
539unsafe impl AsBytes for str {}540
540// SAFETY: If individual values in an array have no uninitialized portions, then the array itself541 // 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.
542unsafe impl<T: AsBytes> AsBytes for [T] {}543 {<T: AsBytes>} [T],
543unsafe impl<T: AsBytes, const N: usize> AsBytes for [T; N] {}544 {<T: AsBytes, const N: usize>} [T; N],
545}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t