[PATCH v6 4/9] rust: num: make Bounded::get const

Alexandre Courbot posted 9 patches 1 month, 2 weeks ago
There is a newer version of this series
[PATCH v6 4/9] rust: num: make Bounded::get const
Posted by Alexandre Courbot 1 month, 2 weeks ago
There is a need to access the inner value of a `Bounded` in const
context, notably for bitfields and registers. Remove the invariant check
of `Bounded::get`, which allows us to make it const.

Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
 rust/kernel/num/bounded.rs | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/rust/kernel/num/bounded.rs b/rust/kernel/num/bounded.rs
index 8e4c08924d96..b09b93cd0c5c 100644
--- a/rust/kernel/num/bounded.rs
+++ b/rust/kernel/num/bounded.rs
@@ -379,6 +379,9 @@ pub fn from_expr(expr: T) -> Self {
 
     /// Returns the wrapped value as the backing type.
     ///
+    /// This is similar to the [`Deref`] implementation, but doesn't enforce the size invariant of
+    /// the [`Bounded`], which might produce slightly less optimal code.
+    ///
     /// # Examples
     ///
     /// ```
@@ -387,8 +390,8 @@ pub fn from_expr(expr: T) -> Self {
     /// let v = Bounded::<u32, 4>::new::<7>();
     /// assert_eq!(v.get(), 7u32);
     /// ```
-    pub fn get(self) -> T {
-        *self.deref()
+    pub const fn get(self) -> T {
+        self.0
     }
 
     /// Increases the number of bits usable for `self`.

-- 
2.53.0
Re: [PATCH v6 4/9] rust: num: make Bounded::get const
Posted by Alice Ryhl 1 month, 2 weeks ago
On Mon, Feb 16, 2026 at 05:04:40PM +0900, Alexandre Courbot wrote:
> There is a need to access the inner value of a `Bounded` in const
> context, notably for bitfields and registers. Remove the invariant check
> of `Bounded::get`, which allows us to make it const.
> 
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
> ---
>  rust/kernel/num/bounded.rs | 7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)
> 
> diff --git a/rust/kernel/num/bounded.rs b/rust/kernel/num/bounded.rs
> index 8e4c08924d96..b09b93cd0c5c 100644
> --- a/rust/kernel/num/bounded.rs
> +++ b/rust/kernel/num/bounded.rs
> @@ -379,6 +379,9 @@ pub fn from_expr(expr: T) -> Self {
>  
>      /// Returns the wrapped value as the backing type.
>      ///
> +    /// This is similar to the [`Deref`] implementation, but doesn't enforce the size invariant of
> +    /// the [`Bounded`], which might produce slightly less optimal code.

The unreachable_unchecked() hint is const, so I see no reason you
couldn't add the same call in `fn get`.

Alice
Re: [PATCH v6 4/9] rust: num: make Bounded::get const
Posted by Gary Guo 1 month, 2 weeks ago
On 2026-02-16 08:56, Alice Ryhl wrote:
> On Mon, Feb 16, 2026 at 05:04:40PM +0900, Alexandre Courbot wrote:
>> There is a need to access the inner value of a `Bounded` in const
>> context, notably for bitfields and registers. Remove the invariant 
>> check
>> of `Bounded::get`, which allows us to make it const.
>> 
>> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
>> ---
>>  rust/kernel/num/bounded.rs | 7 +++++--
>>  1 file changed, 5 insertions(+), 2 deletions(-)
>> 
>> diff --git a/rust/kernel/num/bounded.rs b/rust/kernel/num/bounded.rs
>> index 8e4c08924d96..b09b93cd0c5c 100644
>> --- a/rust/kernel/num/bounded.rs
>> +++ b/rust/kernel/num/bounded.rs
>> @@ -379,6 +379,9 @@ pub fn from_expr(expr: T) -> Self {
>> 
>>      /// Returns the wrapped value as the backing type.
>>      ///
>> +    /// This is similar to the [`Deref`] implementation, but doesn't 
>> enforce the size invariant of
>> +    /// the [`Bounded`], which might produce slightly less optimal 
>> code.
> 
> The unreachable_unchecked() hint is const, so I see no reason you
> couldn't add the same call in `fn get`.

The issue is that trait impls are not const, so the if condition cannot 
be represented in const yet.

Best,
Gary