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
490490 /// All bit-patterns must be valid for this type. This type must not have interior mutability.
491491 pub unsafe trait FromBytes {}
492492
493−// SAFETY: All bit patterns are acceptable values of the types below.
494−unsafe impl FromBytes for u8 {}
495−unsafe impl FromBytes for u16 {}
496−unsafe impl FromBytes for u32 {}
497−unsafe impl FromBytes for u64 {}
498−unsafe impl FromBytes for usize {}
499−unsafe impl FromBytes for i8 {}
500−unsafe impl FromBytes for i16 {}
501−unsafe impl FromBytes for i32 {}
502−unsafe impl FromBytes for i64 {}
503−unsafe impl FromBytes for isize {}
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.
506−unsafe impl<T: FromBytes> FromBytes for [T] {}
507−unsafe impl<T: FromBytes, const N: usize> FromBytes for [T; N] {}
493+macro_rules! impl_frombytes {
494+ ($($({$($generics:tt)*})? $t:ty, )*) => {
495+ $(unsafe impl$($($generics)*)? FromBytes for $t {})*
496+ };
497+}
498+
499+impl_frombytes! {
500+ // SAFETY: All bit patterns are acceptable values of the types below.
501+ u8, u16, u32, u64, usize,
502+ i8, i16, i32, i64, isize,
503+
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.
506+ {<T: FromBytes>} [T],
507+ {<T: FromBytes, const N: usize>} [T; N],
508+}
508509
509510 /// Types that can be viewed as an immutable slice of initialized bytes.
510511 ///
@@ -523,21 +524,22 @@ unsafe impl<T: FromBytes, const N: usize> FromBytes for [T; N] {} · 12 unmodified lines
523524 /// mutability.
524525 pub unsafe trait AsBytes {}
525526
526−// SAFETY: Instances of the following types have no uninitialized portions.
527−unsafe impl AsBytes for u8 {}
528−unsafe impl AsBytes for u16 {}
529−unsafe impl AsBytes for u32 {}
530−unsafe impl AsBytes for u64 {}
531−unsafe impl AsBytes for usize {}
532−unsafe impl AsBytes for i8 {}
533−unsafe impl AsBytes for i16 {}
534−unsafe impl AsBytes for i32 {}
535−unsafe impl AsBytes for i64 {}
536−unsafe impl AsBytes for isize {}
537−unsafe impl AsBytes for bool {}
538−unsafe impl AsBytes for char {}
539−unsafe impl AsBytes for str {}
540−// SAFETY: If individual values in an array have no uninitialized portions, then the array itself
541−// does not have any uninitialized portions either.
542−unsafe impl<T: AsBytes> AsBytes for [T] {}
543−unsafe impl<T: AsBytes, const N: usize> AsBytes for [T; N] {}
527+macro_rules! impl_asbytes {
528+ ($($({$($generics:tt)*})? $t:ty, )*) => {
529+ $(unsafe impl$($($generics)*)? AsBytes for $t {})*
530+ };
531+}
532+
533+impl_asbytes! {
534+ // SAFETY: Instances of the following types have no uninitialized portions.
535+ u8, u16, u32, u64, usize,
536+ i8, i16, i32, i64, isize,
537+ bool,
538+ char,
539+ str,
540+
541+ // SAFETY: If individual values in an array have no uninitialized portions, then the array
542+ // itself does not have any uninitialized portions either.
543+ {<T: AsBytes>} [T],
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