[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <aQxwBn6wOarZ5ApN@google.com>
Date: Thu, 6 Nov 2025 09:53:10 +0000
From: Alice Ryhl <aliceryhl@...gle.com>
To: Alexandre Courbot <acourbot@...dia.com>
Cc: Danilo Krummrich <dakr@...nel.org>, Miguel Ojeda <ojeda@...nel.org>,
Joel Fernandes <joelagnelf@...dia.com>, Yury Norov <yury.norov@...il.com>,
Jesung Yang <y.j3ms.n@...il.com>, Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>,
"Björn Roy Baron" <bjorn3_gh@...tonmail.com>, Benno Lossin <lossin@...nel.org>,
Andreas Hindborg <a.hindborg@...nel.org>, Trevor Gross <tmgross@...ch.edu>, linux-kernel@...r.kernel.org,
rust-for-linux@...r.kernel.org
Subject: Re: [PATCH v3 2/4] rust: num: add Bounded integer wrapping type
On Thu, Nov 06, 2025 at 04:07:14PM +0900, Alexandre Courbot wrote:
> Add the `Bounded` integer wrapper type, which restricts the number of
> bits allowed to represent of value.
>
> This is useful to e.g. enforce guarantees when working with bitfields
> that have an arbitrary number of bits.
>
> Alongside this type, provide many `From` and `TryFrom` implementations
> are to reduce friction when using with regular integer types. Proxy
> implementations of common integer operations are also provided.
>
> Signed-off-by: Alexandre Courbot <acourbot@...dia.com>
> ---
> rust/kernel/num.rs | 3 +
> rust/kernel/num/bounded.rs | 1045 ++++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 1048 insertions(+)
>
> diff --git a/rust/kernel/num.rs b/rust/kernel/num.rs
> index 3f85e50b8632..bc9abcc3a317 100644
> --- a/rust/kernel/num.rs
> +++ b/rust/kernel/num.rs
> @@ -4,6 +4,9 @@
>
> use core::ops;
>
> +pub mod bounded;
> +pub use bounded::*;
> +
> /// Designates unsigned primitive types.
> pub struct Unsigned(());
>
> diff --git a/rust/kernel/num/bounded.rs b/rust/kernel/num/bounded.rs
> new file mode 100644
> index 000000000000..2e4bc4ce9af5
> --- /dev/null
> +++ b/rust/kernel/num/bounded.rs
> @@ -0,0 +1,1045 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +//! Implementation of [`Bounded`], a wrapper around integer types limiting the number of bits
> +//! usable for value representation.
> +
> +use core::{
> + cmp,
> + fmt,
> + ops::{
> + self,
> + Deref, //
> + }, //,
> +};
> +
> +use kernel::{
> + num::Integer,
> + prelude::*, //
> +};
> +
> +/// Evaluates to `true` if `$value` can be represented using at most `$n` bits in a `$type`.
> +///
> +/// Can be used in const context.
> +macro_rules! fits_within {
> + ($value:expr, $type:ty, $n:expr) => {{
> + let shift: u32 = <$type>::BITS - $n;
> +
> + // `value` fits within `$n` bits if shifting it left by the number of unused bits, then
> + // right by the same number, doesn't change it.
> + //
> + // This method has the benefit of working for both unsigned and signed values.
> + ($value << shift) >> shift == $value
I'm still confused about whether this works or not for signed values.
I guess for a signed 4-bit int, the range of values is -8 to 7, so those
are the values that this shift should preserve the values of. Is that
what it does?
Alice
Powered by blists - more mailing lists