[PATCH v4 4/7] rust: num: add `into_inner` method to `Bounded`

Alexandre Courbot posted 7 patches 1 week, 4 days ago
There is a newer version of this series
[PATCH v4 4/7] rust: num: add `into_inner` method to `Bounded`
Posted by Alexandre Courbot 1 week, 4 days ago
This is useful to access the inner value in const contexts.

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

diff --git a/rust/kernel/num/bounded.rs b/rust/kernel/num/bounded.rs
index b41ca6df1525..850827033f67 100644
--- a/rust/kernel/num/bounded.rs
+++ b/rust/kernel/num/bounded.rs
@@ -388,6 +388,22 @@ pub fn get(self) -> T {
         *self.deref()
     }
 
+    /// Returns the wrapped value as the backing type.
+    ///
+    /// This is a const-friendly variant of [`Self::get`] that can be used in const contexts.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use kernel::num::Bounded;
+    ///
+    /// const V: u32 = Bounded::<u32, 4>::new::<7>().into_inner();
+    /// assert_eq!(V, 7u32);
+    /// ```
+    pub const fn into_inner(self) -> T {
+        self.0
+    }
+
     /// Increases the number of bits usable for `self`.
     ///
     /// This operation cannot fail.

-- 
2.52.0
Re: [PATCH v4 4/7] rust: num: add `into_inner` method to `Bounded`
Posted by Gary Guo 1 week, 3 days ago
On Wed Jan 28, 2026 at 2:37 AM GMT, Alexandre Courbot wrote:
> This is useful to access the inner value in const contexts.
>
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
> ---
>  rust/kernel/num/bounded.rs | 16 ++++++++++++++++
>  1 file changed, 16 insertions(+)
>
> diff --git a/rust/kernel/num/bounded.rs b/rust/kernel/num/bounded.rs
> index b41ca6df1525..850827033f67 100644
> --- a/rust/kernel/num/bounded.rs
> +++ b/rust/kernel/num/bounded.rs
> @@ -388,6 +388,22 @@ pub fn get(self) -> T {
>          *self.deref()
>      }
>  
> +    /// Returns the wrapped value as the backing type.
> +    ///
> +    /// This is a const-friendly variant of [`Self::get`] that can be used in const contexts.
> +    ///
> +    /// # Examples
> +    ///
> +    /// ```
> +    /// use kernel::num::Bounded;
> +    ///
> +    /// const V: u32 = Bounded::<u32, 4>::new::<7>().into_inner();
> +    /// assert_eq!(V, 7u32);
> +    /// ```
> +    pub const fn into_inner(self) -> T {
> +        self.0
> +    }

This is... just `get`.

You can implement it (with the `fits_with_in` check) in the same manner as the
const new.

Best,
Gary

> +
>      /// Increases the number of bits usable for `self`.
>      ///
>      /// This operation cannot fail.
Re: [PATCH v4 4/7] rust: num: add `into_inner` method to `Bounded`
Posted by Alexandre Courbot 1 week, 3 days ago
On Thu Jan 29, 2026 at 12:43 AM JST, Gary Guo wrote:
> On Wed Jan 28, 2026 at 2:37 AM GMT, Alexandre Courbot wrote:
>> This is useful to access the inner value in const contexts.
>>
>> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
>> ---
>>  rust/kernel/num/bounded.rs | 16 ++++++++++++++++
>>  1 file changed, 16 insertions(+)
>>
>> diff --git a/rust/kernel/num/bounded.rs b/rust/kernel/num/bounded.rs
>> index b41ca6df1525..850827033f67 100644
>> --- a/rust/kernel/num/bounded.rs
>> +++ b/rust/kernel/num/bounded.rs
>> @@ -388,6 +388,22 @@ pub fn get(self) -> T {
>>          *self.deref()
>>      }
>>  
>> +    /// Returns the wrapped value as the backing type.
>> +    ///
>> +    /// This is a const-friendly variant of [`Self::get`] that can be used in const contexts.
>> +    ///
>> +    /// # Examples
>> +    ///
>> +    /// ```
>> +    /// use kernel::num::Bounded;
>> +    ///
>> +    /// const V: u32 = Bounded::<u32, 4>::new::<7>().into_inner();
>> +    /// assert_eq!(V, 7u32);
>> +    /// ```
>> +    pub const fn into_inner(self) -> T {
>> +        self.0
>> +    }
>
> This is... just `get`.
>
> You can implement it (with the `fits_with_in` check) in the same manner as the
> const new.

But unfortunately doing so also prevents `get` from being called from
generic code, which is a must-have. Or am I misunderstanding your
suggestion?
Re: [PATCH v4 4/7] rust: num: add `into_inner` method to `Bounded`
Posted by Gary Guo 1 week, 2 days ago
On Thu Jan 29, 2026 at 8:05 AM GMT, Alexandre Courbot wrote:
> On Thu Jan 29, 2026 at 12:43 AM JST, Gary Guo wrote:
>> On Wed Jan 28, 2026 at 2:37 AM GMT, Alexandre Courbot wrote:
>>> This is useful to access the inner value in const contexts.
>>>
>>> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
>>> ---
>>>  rust/kernel/num/bounded.rs | 16 ++++++++++++++++
>>>  1 file changed, 16 insertions(+)
>>>
>>> diff --git a/rust/kernel/num/bounded.rs b/rust/kernel/num/bounded.rs
>>> index b41ca6df1525..850827033f67 100644
>>> --- a/rust/kernel/num/bounded.rs
>>> +++ b/rust/kernel/num/bounded.rs
>>> @@ -388,6 +388,22 @@ pub fn get(self) -> T {
>>>          *self.deref()
>>>      }
>>>  
>>> +    /// Returns the wrapped value as the backing type.
>>> +    ///
>>> +    /// This is a const-friendly variant of [`Self::get`] that can be used in const contexts.
>>> +    ///
>>> +    /// # Examples
>>> +    ///
>>> +    /// ```
>>> +    /// use kernel::num::Bounded;
>>> +    ///
>>> +    /// const V: u32 = Bounded::<u32, 4>::new::<7>().into_inner();
>>> +    /// assert_eq!(V, 7u32);
>>> +    /// ```
>>> +    pub const fn into_inner(self) -> T {
>>> +        self.0
>>> +    }
>>
>> This is... just `get`.
>>
>> You can implement it (with the `fits_with_in` check) in the same manner as the
>> const new.
>
> But unfortunately doing so also prevents `get` from being called from
> generic code, which is a must-have. Or am I misunderstanding your
> suggestion?

Can you remove the `fits_with_in` check then and just provide `self.0` in `get`
then?

If the assume is really important, then we should at least name this `get_const`
(or `const_get`) to indicate that this is a limitation caused by const eval, not
a completely different method as the name would otherwise suggest.

Best,
Gary
Re: [PATCH v4 4/7] rust: num: add `into_inner` method to `Bounded`
Posted by Alexandre Courbot 1 week, 2 days ago
On Fri Jan 30, 2026 at 5:23 AM JST, Gary Guo wrote:
> On Thu Jan 29, 2026 at 8:05 AM GMT, Alexandre Courbot wrote:
>> On Thu Jan 29, 2026 at 12:43 AM JST, Gary Guo wrote:
>>> On Wed Jan 28, 2026 at 2:37 AM GMT, Alexandre Courbot wrote:
>>>> This is useful to access the inner value in const contexts.
>>>>
>>>> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
>>>> ---
>>>>  rust/kernel/num/bounded.rs | 16 ++++++++++++++++
>>>>  1 file changed, 16 insertions(+)
>>>>
>>>> diff --git a/rust/kernel/num/bounded.rs b/rust/kernel/num/bounded.rs
>>>> index b41ca6df1525..850827033f67 100644
>>>> --- a/rust/kernel/num/bounded.rs
>>>> +++ b/rust/kernel/num/bounded.rs
>>>> @@ -388,6 +388,22 @@ pub fn get(self) -> T {
>>>>          *self.deref()
>>>>      }
>>>>  
>>>> +    /// Returns the wrapped value as the backing type.
>>>> +    ///
>>>> +    /// This is a const-friendly variant of [`Self::get`] that can be used in const contexts.
>>>> +    ///
>>>> +    /// # Examples
>>>> +    ///
>>>> +    /// ```
>>>> +    /// use kernel::num::Bounded;
>>>> +    ///
>>>> +    /// const V: u32 = Bounded::<u32, 4>::new::<7>().into_inner();
>>>> +    /// assert_eq!(V, 7u32);
>>>> +    /// ```
>>>> +    pub const fn into_inner(self) -> T {
>>>> +        self.0
>>>> +    }
>>>
>>> This is... just `get`.
>>>
>>> You can implement it (with the `fits_with_in` check) in the same manner as the
>>> const new.
>>
>> But unfortunately doing so also prevents `get` from being called from
>> generic code, which is a must-have. Or am I misunderstanding your
>> suggestion?
>
> Can you remove the `fits_with_in` check then and just provide `self.0` in `get`
> then?
>
> If the assume is really important, then we should at least name this `get_const`
> (or `const_get`) to indicate that this is a limitation caused by const eval, not
> a completely different method as the name would otherwise suggest.

You're right, we should just do that. The benefits of the invariant
check are negligible at best, and being able to access the value in
const context is more important. Furthermore, one can always use the
`Deref` implementation if they need the potential optimization brought
by checking the invariant.