[PATCH v8 03/10] rust: num: add `into_bool` method to `Bounded`

Alexandre Courbot posted 10 patches 1 month ago
There is a newer version of this series
[PATCH v8 03/10] rust: num: add `into_bool` method to `Bounded`
Posted by Alexandre Courbot 1 month 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 `into_bool` method as a simpler shortcut.

Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Gary Guo <gary@garyguo.net>
Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>
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 2f5f13ecd3d6..d28d118abd8e 100644
--- a/rust/kernel/num/bounded.rs
+++ b/rust/kernel/num/bounded.rs
@@ -1101,3 +1101,24 @@ fn from(value: bool) -> Self {
         unsafe { Self::__new(T::from(value)) }
     }
 }
+
+impl<T> Bounded<T, 1>
+where
+    T: Integer + Zeroable,
+{
+    /// Converts this [`Bounded`] into a [`bool`].
+    ///
+    /// This is a shorter way of writing `bool::from(self)`.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use kernel::num::Bounded;
+    ///
+    /// assert_eq!(Bounded::<u8, 1>::new::<0>().into_bool(), false);
+    /// assert_eq!(Bounded::<u8, 1>::new::<1>().into_bool(), true);
+    /// ```
+    pub fn into_bool(self) -> bool {
+        self.into()
+    }
+}

-- 
2.53.0
Re: [PATCH v8 03/10] rust: num: add `into_bool` method to `Bounded`
Posted by Yury Norov 4 weeks, 1 day ago
On Tue, Mar 10, 2026 at 12:14:00AM +0900, Alexandre Courbot 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 `into_bool` method as a simpler shortcut.
> 
> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> Reviewed-by: Gary Guo <gary@garyguo.net>
> Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>
> Tested-by: Dirk Behme <dirk.behme@de.bosch.com>
> Acked-by: Miguel Ojeda <ojeda@kernel.org>
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>

Reviewed-by: Yury Norov <yury.norov@gmail.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 2f5f13ecd3d6..d28d118abd8e 100644
> --- a/rust/kernel/num/bounded.rs
> +++ b/rust/kernel/num/bounded.rs
> @@ -1101,3 +1101,24 @@ fn from(value: bool) -> Self {
>          unsafe { Self::__new(T::from(value)) }
>      }
>  }
> +
> +impl<T> Bounded<T, 1>
> +where
> +    T: Integer + Zeroable,
> +{
> +    /// Converts this [`Bounded`] into a [`bool`].
> +    ///
> +    /// This is a shorter way of writing `bool::from(self)`.
> +    ///
> +    /// # Examples
> +    ///
> +    /// ```
> +    /// use kernel::num::Bounded;
> +    ///
> +    /// assert_eq!(Bounded::<u8, 1>::new::<0>().into_bool(), false);
> +    /// assert_eq!(Bounded::<u8, 1>::new::<1>().into_bool(), true);
> +    /// ```
> +    pub fn into_bool(self) -> bool {
> +        self.into()
> +    }
> +}
> 
> -- 
> 2.53.0