Skip to content

Commit 50de79f

Browse files
ojedasmb49
authored andcommitted
rust: types: avoid repetition in {As,From}Bytes impls
BugLink: https://bugs.launchpad.net/bugs/2107437 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]> Signed-off-by: Noah Wager <[email protected]> Signed-off-by: Stefan Bader <[email protected]>
1 parent 2ec6c2e commit 50de79f

File tree

1 file changed

+35
-33
lines changed

1 file changed

+35
-33
lines changed

rust/kernel/types.rs

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -422,21 +422,22 @@ pub enum Either<L, R> {
422422
/// All bit-patterns must be valid for this type. This type must not have interior mutability.
423423
pub unsafe trait FromBytes {}
424424

425-
// SAFETY: All bit patterns are acceptable values of the types below.
426-
unsafe impl FromBytes for u8 {}
427-
unsafe impl FromBytes for u16 {}
428-
unsafe impl FromBytes for u32 {}
429-
unsafe impl FromBytes for u64 {}
430-
unsafe impl FromBytes for usize {}
431-
unsafe impl FromBytes for i8 {}
432-
unsafe impl FromBytes for i16 {}
433-
unsafe impl FromBytes for i32 {}
434-
unsafe impl FromBytes for i64 {}
435-
unsafe impl FromBytes for isize {}
436-
// SAFETY: If all bit patterns are acceptable for individual values in an array, then all bit
437-
// patterns are also acceptable for arrays of that type.
438-
unsafe impl<T: FromBytes> FromBytes for [T] {}
439-
unsafe impl<T: FromBytes, const N: usize> FromBytes for [T; N] {}
425+
macro_rules! impl_frombytes {
426+
($($({$($generics:tt)*})? $t:ty, )*) => {
427+
$(unsafe impl$($($generics)*)? FromBytes for $t {})*
428+
};
429+
}
430+
431+
impl_frombytes! {
432+
// SAFETY: All bit patterns are acceptable values of the types below.
433+
u8, u16, u32, u64, usize,
434+
i8, i16, i32, i64, isize,
435+
436+
// SAFETY: If all bit patterns are acceptable for individual values in an array, then all bit
437+
// patterns are also acceptable for arrays of that type.
438+
{<T: FromBytes>} [T],
439+
{<T: FromBytes, const N: usize>} [T; N],
440+
}
440441

441442
/// Types that can be viewed as an immutable slice of initialized bytes.
442443
///
@@ -455,21 +456,22 @@ unsafe impl<T: FromBytes, const N: usize> FromBytes for [T; N] {}
455456
/// mutability.
456457
pub unsafe trait AsBytes {}
457458

458-
// SAFETY: Instances of the following types have no uninitialized portions.
459-
unsafe impl AsBytes for u8 {}
460-
unsafe impl AsBytes for u16 {}
461-
unsafe impl AsBytes for u32 {}
462-
unsafe impl AsBytes for u64 {}
463-
unsafe impl AsBytes for usize {}
464-
unsafe impl AsBytes for i8 {}
465-
unsafe impl AsBytes for i16 {}
466-
unsafe impl AsBytes for i32 {}
467-
unsafe impl AsBytes for i64 {}
468-
unsafe impl AsBytes for isize {}
469-
unsafe impl AsBytes for bool {}
470-
unsafe impl AsBytes for char {}
471-
unsafe impl AsBytes for str {}
472-
// SAFETY: If individual values in an array have no uninitialized portions, then the array itself
473-
// does not have any uninitialized portions either.
474-
unsafe impl<T: AsBytes> AsBytes for [T] {}
475-
unsafe impl<T: AsBytes, const N: usize> AsBytes for [T; N] {}
459+
macro_rules! impl_asbytes {
460+
($($({$($generics:tt)*})? $t:ty, )*) => {
461+
$(unsafe impl$($($generics)*)? AsBytes for $t {})*
462+
};
463+
}
464+
465+
impl_asbytes! {
466+
// SAFETY: Instances of the following types have no uninitialized portions.
467+
u8, u16, u32, u64, usize,
468+
i8, i16, i32, i64, isize,
469+
bool,
470+
char,
471+
str,
472+
473+
// SAFETY: If individual values in an array have no uninitialized portions, then the array
474+
// itself does not have any uninitialized portions either.
475+
{<T: AsBytes>} [T],
476+
{<T: AsBytes, const N: usize>} [T; N],
477+
}

0 commit comments

Comments
 (0)