Skip to content

Commit f541f65

Browse files
ojedajfvogel
authored andcommitted
rust: types: avoid repetition in {As,From}Bytes impls
commit 567cdff upstream. 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. Reviewed-by: Alice Ryhl <[email protected]> Reviewed-by: Trevor Gross <[email protected]> Tested-by: Gary Guo <[email protected]> Reviewed-by: Gary Guo <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]> (cherry picked from commit 870de86d874e6d2596f5309df545b8eab8aef6a3) Signed-off-by: Jack Vogel <[email protected]>
1 parent 3f56cd8 commit f541f65

File tree

1 file changed

+35
-33
lines changed

1 file changed

+35
-33
lines changed

rust/kernel/types.rs

+35-33
Original file line numberDiff line numberDiff line change
@@ -481,21 +481,22 @@ pub enum Either<L, R> {
481481
/// All bit-patterns must be valid for this type. This type must not have interior mutability.
482482
pub unsafe trait FromBytes {}
483483

484-
// SAFETY: All bit patterns are acceptable values of the types below.
485-
unsafe impl FromBytes for u8 {}
486-
unsafe impl FromBytes for u16 {}
487-
unsafe impl FromBytes for u32 {}
488-
unsafe impl FromBytes for u64 {}
489-
unsafe impl FromBytes for usize {}
490-
unsafe impl FromBytes for i8 {}
491-
unsafe impl FromBytes for i16 {}
492-
unsafe impl FromBytes for i32 {}
493-
unsafe impl FromBytes for i64 {}
494-
unsafe impl FromBytes for isize {}
495-
// SAFETY: If all bit patterns are acceptable for individual values in an array, then all bit
496-
// patterns are also acceptable for arrays of that type.
497-
unsafe impl<T: FromBytes> FromBytes for [T] {}
498-
unsafe impl<T: FromBytes, const N: usize> FromBytes for [T; N] {}
484+
macro_rules! impl_frombytes {
485+
($($({$($generics:tt)*})? $t:ty, )*) => {
486+
$(unsafe impl$($($generics)*)? FromBytes for $t {})*
487+
};
488+
}
489+
490+
impl_frombytes! {
491+
// SAFETY: All bit patterns are acceptable values of the types below.
492+
u8, u16, u32, u64, usize,
493+
i8, i16, i32, i64, isize,
494+
495+
// SAFETY: If all bit patterns are acceptable for individual values in an array, then all bit
496+
// patterns are also acceptable for arrays of that type.
497+
{<T: FromBytes>} [T],
498+
{<T: FromBytes, const N: usize>} [T; N],
499+
}
499500

500501
/// Types that can be viewed as an immutable slice of initialized bytes.
501502
///
@@ -514,21 +515,22 @@ unsafe impl<T: FromBytes, const N: usize> FromBytes for [T; N] {}
514515
/// mutability.
515516
pub unsafe trait AsBytes {}
516517

517-
// SAFETY: Instances of the following types have no uninitialized portions.
518-
unsafe impl AsBytes for u8 {}
519-
unsafe impl AsBytes for u16 {}
520-
unsafe impl AsBytes for u32 {}
521-
unsafe impl AsBytes for u64 {}
522-
unsafe impl AsBytes for usize {}
523-
unsafe impl AsBytes for i8 {}
524-
unsafe impl AsBytes for i16 {}
525-
unsafe impl AsBytes for i32 {}
526-
unsafe impl AsBytes for i64 {}
527-
unsafe impl AsBytes for isize {}
528-
unsafe impl AsBytes for bool {}
529-
unsafe impl AsBytes for char {}
530-
unsafe impl AsBytes for str {}
531-
// SAFETY: If individual values in an array have no uninitialized portions, then the array itself
532-
// does not have any uninitialized portions either.
533-
unsafe impl<T: AsBytes> AsBytes for [T] {}
534-
unsafe impl<T: AsBytes, const N: usize> AsBytes for [T; N] {}
518+
macro_rules! impl_asbytes {
519+
($($({$($generics:tt)*})? $t:ty, )*) => {
520+
$(unsafe impl$($($generics)*)? AsBytes for $t {})*
521+
};
522+
}
523+
524+
impl_asbytes! {
525+
// SAFETY: Instances of the following types have no uninitialized portions.
526+
u8, u16, u32, u64, usize,
527+
i8, i16, i32, i64, isize,
528+
bool,
529+
char,
530+
str,
531+
532+
// SAFETY: If individual values in an array have no uninitialized portions, then the array
533+
// itself does not have any uninitialized portions either.
534+
{<T: AsBytes>} [T],
535+
{<T: AsBytes, const N: usize>} [T; N],
536+
}

0 commit comments

Comments
 (0)