[PATCH v5 3/7] rust: num: add `as_bool` method to `Bounded<_, 1>`

Alexandre Courbot posted 7 patches 1 week, 3 days ago
[PATCH v5 3/7] rust: num: add `as_bool` method to `Bounded<_, 1>`
Posted by Alexandre Courbot 1 week, 3 days ago
Single-bit numbers are typically treated as booleans. There is an
`Into<bool>` implementation for those, but invoking it from contexts
that lack type expectations is not always convenient.

Add an `as_bool` method as a simpler shortcut.

Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Gary Guo <gary@garyguo.net>
Tested-by: Dirk Behme <dirk.behme@de.bosch.com>
Acked-by: Miguel Ojeda <ojeda@kernel.org>
Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
 rust/kernel/num/bounded.rs | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/rust/kernel/num/bounded.rs b/rust/kernel/num/bounded.rs
index 4b929762d5c2..b41ca6df1525 100644
--- a/rust/kernel/num/bounded.rs
+++ b/rust/kernel/num/bounded.rs
@@ -1098,3 +1098,24 @@ fn from(value: bool) -> Self {
         Self::__new(T::from(value))
     }
 }
+
+impl<T> Bounded<T, 1>
+where
+    T: Integer + Zeroable,
+{
+    /// Returns the value of this [`Bounded`] as a [`bool`].
+    ///
+    /// This is a shorter way of writing `bool::from(self)`.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use kernel::num::Bounded;
+    ///
+    /// assert_eq!(Bounded::<u8, 1>::new::<0>().as_bool(), false);
+    /// assert_eq!(Bounded::<u8, 1>::new::<1>().as_bool(), true);
+    /// ```
+    pub fn as_bool(self) -> bool {
+        self.into()
+    }
+}

-- 
2.52.0
Re: [PATCH v5 3/7] rust: num: add `as_bool` method to `Bounded<_, 1>`
Posted by Daniel Almeida 2 days, 9 hours ago

> On 29 Jan 2026, at 10:32, Alexandre Courbot <acourbot@nvidia.com> wrote:
> 
> Single-bit numbers are typically treated as booleans. There is an
> `Into<bool>` implementation for those, but invoking it from contexts
> that lack type expectations is not always convenient.
> 
> Add an `as_bool` method as a simpler shortcut.
> 
> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> Reviewed-by: Gary Guo <gary@garyguo.net>
> Tested-by: Dirk Behme <dirk.behme@de.bosch.com>
> Acked-by: Miguel Ojeda <ojeda@kernel.org>
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
> ---
> rust/kernel/num/bounded.rs | 21 +++++++++++++++++++++
> 1 file changed, 21 insertions(+)
> 
> diff --git a/rust/kernel/num/bounded.rs b/rust/kernel/num/bounded.rs
> index 4b929762d5c2..b41ca6df1525 100644
> --- a/rust/kernel/num/bounded.rs
> +++ b/rust/kernel/num/bounded.rs
> @@ -1098,3 +1098,24 @@ fn from(value: bool) -> Self {
>         Self::__new(T::from(value))
>     }
> }
> +
> +impl<T> Bounded<T, 1>
> +where
> +    T: Integer + Zeroable,
> +{
> +    /// Returns the value of this [`Bounded`] as a [`bool`].
> +    ///
> +    /// This is a shorter way of writing `bool::from(self)`.
> +    ///
> +    /// # Examples
> +    ///
> +    /// ```
> +    /// use kernel::num::Bounded;
> +    ///
> +    /// assert_eq!(Bounded::<u8, 1>::new::<0>().as_bool(), false);
> +    /// assert_eq!(Bounded::<u8, 1>::new::<1>().as_bool(), true);
> +    /// ```
> +    pub fn as_bool(self) -> bool {
> +        self.into()
> +    }
> +}
> 
> -- 
> 2.52.0
> 

I wonder whether this should be into_bool() instead? as_foo() usually goes from &self to &Foo.

In any case:

Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>