Re: [PATCH 1/6] rust: num: add `shr` and `shl` methods to `Bounded`
From: Alexandre Courbot
Date: Tue Jan 20 2026 - 07:54:04 EST
On Tue Jan 20, 2026 at 5:44 PM JST, Alice Ryhl wrote:
> On Tue, Jan 20, 2026 at 03:17:54PM +0900, Alexandre Courbot wrote:
>> Shifting a `Bounded` left or right changes the number of bits required
>> to represent the value. Add methods that perform the shift and return a
>> `Bounded` with the appropriately adjusted bit width.
>>
>> These methods are particularly useful for bitfield extraction.
>>
>> Suggested-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
>> Signed-off-by: Alexandre Courbot <acourbot@xxxxxxxxxx>
>
> Reviewed-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
>
>> + /// Right-shifts `self` by `SHIFT` and returns the result as a `Bounded<_, { N - SHIFT }>`.
>> + ///
>> + /// # Examples
>> + ///
>> + /// ```
>> + /// use kernel::num::Bounded;
>> + ///
>> + /// let v = Bounded::<u32, 16>::new::<0xff00>();
>> + /// let v_shifted: Bounded::<u32, 8> = v.shr::<8, _>();
>> + ///
>> + /// assert_eq!(v_shifted.get(), 0xff);
>> + /// ```
>> + pub fn shr<const SHIFT: u32, const RES: u32>(self) -> Bounded<T, RES> {
>> + const { assert!(RES == N - SHIFT) }
>
> In principle this could be
>
> const { assert!(RES >= N - SHIFT) }
Oh, that's right and that's also more flexible - thanks, will do that for v2.