Re: [PATCH] rust: bitfield: add conversions to Bounded of matching width
From: Yury Norov
Date: Fri Nov 27 2026 - 15:36:55 EST
On Fri, Nov 27, 2026 at 09:05:40PM +0900, Alexandre Courbot wrote:
> `From`/`Into` implementations already exist between a bitfield type and
> its storage type, but these are not enough to support nested bitfields,
> since every field is a `Bounded`. This resulted in some ad-hoc
> implementations to enable this kind of declaration:
>
> bitfield! {
> pub struct Version(u8) {
> 7:4 major;
> 3:0 minor;
> }
> }
>
> bitfield! {
> pub struct Id(u16) {
> 15:8 vendor;
> 7:0 version => Version;
> }
> }
>
> Generate these conversions automatically through the `bitfield!` macro,
> as these tend to be useful.
>
> Tyr had such ad-hoc implementations, so remove them to avoid conflicts.
>
> Signed-off-by: Alexandre Courbot <acourbot@xxxxxxxxxx>
Reviewed-by: Yury Norov <ynorov@xxxxxxxxxx>
> ---
> drivers/gpu/drm/tyr/regs.rs | 30 +++++------------
> rust/kernel/bitfield.rs | 82 ++++++++++++++++++++++++++++++++++++++++++++-
> 2 files changed, 89 insertions(+), 23 deletions(-)
>
> diff --git a/drivers/gpu/drm/tyr/regs.rs b/drivers/gpu/drm/tyr/regs.rs
> index 69c901afb28f..0392869adc5a 100644
> --- a/drivers/gpu/drm/tyr/regs.rs
> +++ b/drivers/gpu/drm/tyr/regs.rs
> @@ -1150,20 +1150,6 @@ struct MMU_MEMATTR_STAGE1(u8) {
> }
> }
>
> - impl TryFrom<Bounded<u64, 8>> for MMU_MEMATTR_STAGE1 {
> - type Error = Error;
> -
> - fn try_from(val: Bounded<u64, 8>) -> Result<Self, Self::Error> {
> - Ok(Self::from_raw(val.get() as u8))
> - }
> - }
> -
> - impl From<MMU_MEMATTR_STAGE1> for Bounded<u64, 8> {
> - fn from(val: MMU_MEMATTR_STAGE1) -> Self {
> - Bounded::try_new(u64::from(val.into_raw())).unwrap()
> - }
> - }
> -
> register! {
> base: TyrRegisters;
>
> @@ -1172,14 +1158,14 @@ fn from(val: MMU_MEMATTR_STAGE1) -> Self {
> /// Each address space can configure up to 8 different memory attribute profiles.
> /// Each attribute profile follows the MMU_MEMATTR_STAGE1 layout.
> pub(crate) MEMATTR(u64)[MAX_AS, stride = STRIDE] @ 0x2408 {
> - 7:0 attribute0 ?=> MMU_MEMATTR_STAGE1;
> - 15:8 attribute1 ?=> MMU_MEMATTR_STAGE1;
> - 23:16 attribute2 ?=> MMU_MEMATTR_STAGE1;
> - 31:24 attribute3 ?=> MMU_MEMATTR_STAGE1;
> - 39:32 attribute4 ?=> MMU_MEMATTR_STAGE1;
> - 47:40 attribute5 ?=> MMU_MEMATTR_STAGE1;
> - 55:48 attribute6 ?=> MMU_MEMATTR_STAGE1;
> - 63:56 attribute7 ?=> MMU_MEMATTR_STAGE1;
> + 7:0 attribute0 => MMU_MEMATTR_STAGE1;
> + 15:8 attribute1 => MMU_MEMATTR_STAGE1;
> + 23:16 attribute2 => MMU_MEMATTR_STAGE1;
> + 31:24 attribute3 => MMU_MEMATTR_STAGE1;
> + 39:32 attribute4 => MMU_MEMATTR_STAGE1;
> + 47:40 attribute5 => MMU_MEMATTR_STAGE1;
> + 55:48 attribute6 => MMU_MEMATTR_STAGE1;
> + 63:56 attribute7 => MMU_MEMATTR_STAGE1;
> }
>
> // MEMATTR is a logical 64-bit register, but it is laid out in hardware as two
> diff --git a/rust/kernel/bitfield.rs b/rust/kernel/bitfield.rs
> index 8d7d78175336..f53001a41731 100644
> --- a/rust/kernel/bitfield.rs
> +++ b/rust/kernel/bitfield.rs
> @@ -78,7 +78,8 @@
> //! every value assigned to a field. This ensures that data is never accidentally truncated.
> //!
> //! The macro generates the bitfield type, [`From`] and [`Into`] implementations for its storage
> -//! type, as well as [`Debug`] and [`Zeroable`](pin_init::Zeroable) implementations.
> +//! type and for any [`Bounded`] with the same number of bits as its storage type, as well as
> +//! [`Debug`] and [`Zeroable`](pin_init::Zeroable) implementations.
> //!
> //! For each field, it also generates:
> //!
> @@ -249,6 +250,37 @@
> //! assert_eq!(Config::from(0b11).mode(), Err(3));
> //! ```
> //!
> +//! ## Nested bitfields
> +//!
> +//! A bitfield type can be converted from and into any [`Bounded`] with exactly as many bits as
> +//! its storage type. Thus, it can be used with `=>` as the type of a field of the same width in
> +//! another bitfield:
> +//!
> +//! ```rust
> +//! use kernel::bitfield;
> +//!
> +//! bitfield! {
> +//! pub struct Version(u8) {
> +//! 7:4 major;
> +//! 3:0 minor;
> +//! }
> +//! }
> +//!
> +//! bitfield! {
> +//! pub struct Id(u16) {
> +//! 15:8 vendor;
> +//! 7:0 version => Version;
> +//! }
> +//! }
> +//!
> +//! let id = Id::from_raw(0x10_42);
> +//! assert_eq!(id.version().major(), 0x4);
> +//! assert_eq!(id.version().minor(), 0x2);
> +//!
> +//! let id = id.with_version(Version::zeroed().with_const_major::<0x1>());
> +//! assert_eq!(id.into_raw(), 0x10_10);
> +//! ```
> +//!
> //! # Bits outside of declared fields
> //!
> //! Bits of the storage type that are not part of any declared field are preserved by the setter
> @@ -346,6 +378,31 @@ fn from(val: $storage) -> $name {
> }
> }
>
> + // Conversions from/to any `Bounded` with the exact same width as `$storage`, to allow
> + // infallible nested bitfields.
> +
> + impl<T> ::core::convert::From<$crate::num::Bounded<T, { <$storage>::BITS }>> for $name
> + where
> + T: $crate::num::Integer,
> + $storage: ::core::convert::From<$crate::num::Bounded<T, { <$storage>::BITS }>>,
> + {
> + #[inline(always)]
> + fn from(val: $crate::num::Bounded<T, { <$storage>::BITS }>) -> $name {
> + Self::from_raw(val.into())
> + }
> + }
> +
> + impl<T> ::core::convert::From<$name> for $crate::num::Bounded<T, { <$storage>::BITS }>
> + where
> + T: $crate::num::Integer,
> + Self: ::core::convert::From<$storage>,
> + {
> + #[inline(always)]
> + fn from(val: $name) -> Self {
> + val.into_raw().into()
> + }
> + }
> +
> // SAFETY: `$name` is transparent over `$storage` and `$storage` has no interior mutability.
> unsafe impl $crate::mem::AsRepr for $name {
> // Normalize `$storage` to the canonical repr type in case it is signed.
> @@ -653,6 +710,14 @@ struct TestU8(u8) {
> }
> }
>
> + bitfield! {
> + struct TestNested(u32) {
> + 31:16 field_31_16 => TestU16;
> + 15:8 field_15_8;
> + 7:0 field_7_0 => TestU8;
> + }
> + }
> +
> // Single and multi-bit fields basic access.
> #[test]
> fn test_basic_access() {
> @@ -770,6 +835,21 @@ fn test_fallible_conversion() {
> assert_eq!(TestU64::from_raw(raw).field_15_12(), Err(0x7));
> }
>
> + // `=>` conversion from/to another bitfield with the same width as the field.
> + #[test]
> + fn test_nested_bitfield() {
> + let val = TestNested::from_raw(0x42AB_00AF);
> + assert_eq!(val.field_31_16(), TestU16::from_raw(0x42AB));
> + assert_eq!(val.field_31_16().field_15_8(), 0x42);
> + assert_eq!(val.field_7_0(), TestU8::from_raw(0xAF));
> + assert_eq!(val.field_7_0().field_7_4(), 0xA);
> +
> + let val = val
> + .with_field_31_16(TestU16::zeroed().with_const_field_3_1::<0x5>())
> + .with_field_7_0(TestU8::zeroed().with_field_0(true));
> + assert_eq!(val.into_raw(), 0x000A_0001);
> + }
> +
> // Test that setting an overlapping field affects the overlapped one as expected.
> #[test]
> fn test_overlapping_fields() {
>
> ---
> base-commit: abfb640b2374e26de8ff81d488c39d8254db538e
> change-id: 20261127-bitfield-conversions-e5a6b02f0ac4
>
> Best regards,
> --
> Alexandre Courbot <acourbot@xxxxxxxxxx>