Introduce the `num` module, which will provide numerical extensions and
utilities for the kernel.
For now, introduce the `Integer` trait, which is implemented for all
primitive integer types to provides their core properties to generic
code.
Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
rust/kernel/lib.rs | 1 +
rust/kernel/num.rs | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 77 insertions(+)
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index 3dd7bebe7888..235d0d8b1eff 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -109,6 +109,7 @@
pub mod mm;
#[cfg(CONFIG_NET)]
pub mod net;
+pub mod num;
pub mod of;
#[cfg(CONFIG_PM_OPP)]
pub mod opp;
diff --git a/rust/kernel/num.rs b/rust/kernel/num.rs
new file mode 100644
index 000000000000..3f85e50b8632
--- /dev/null
+++ b/rust/kernel/num.rs
@@ -0,0 +1,76 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Additional numerical features for the kernel.
+
+use core::ops;
+
+/// Designates unsigned primitive types.
+pub struct Unsigned(());
+
+/// Designates signed primitive types.
+pub struct Signed(());
+
+/// Describes core properties of integer types.
+pub trait Integer:
+ Sized
+ + Copy
+ + Clone
+ + PartialEq
+ + Eq
+ + PartialOrd
+ + Ord
+ + ops::Add<Output = Self>
+ + ops::AddAssign
+ + ops::Sub<Output = Self>
+ + ops::SubAssign
+ + ops::Mul<Output = Self>
+ + ops::MulAssign
+ + ops::Div<Output = Self>
+ + ops::DivAssign
+ + ops::Rem<Output = Self>
+ + ops::RemAssign
+ + ops::BitAnd<Output = Self>
+ + ops::BitAndAssign
+ + ops::BitOr<Output = Self>
+ + ops::BitOrAssign
+ + ops::BitXor<Output = Self>
+ + ops::BitXorAssign
+ + ops::Shl<u32, Output = Self>
+ + ops::ShlAssign<u32>
+ + ops::Shr<u32, Output = Self>
+ + ops::ShrAssign<u32>
+ + ops::Not
+{
+ /// Whether this type is [`Signed`] or [`Unsigned`].
+ type Signedness;
+
+ /// Number of bits used for value representation.
+ const BITS: u32;
+}
+
+macro_rules! impl_integer {
+ ($($type:ty: $signedness:ty), *) => {
+ $(
+ impl Integer for $type {
+ type Signedness = $signedness;
+
+ const BITS: u32 = <$type>::BITS;
+ }
+ )*
+ };
+}
+
+impl_integer!(
+ u8: Unsigned,
+ u16: Unsigned,
+ u32: Unsigned,
+ u64: Unsigned,
+ u128: Unsigned,
+ usize: Unsigned,
+ i8: Signed,
+ i16: Signed,
+ i32: Signed,
+ i64: Signed,
+ i128: Signed,
+ isize: Signed
+);
--
2.51.2
On Thu, Nov 06, 2025 at 04:07:13PM +0900, Alexandre Courbot wrote:
> Introduce the `num` module, which will provide numerical extensions and
> utilities for the kernel.
>
> For now, introduce the `Integer` trait, which is implemented for all
> primitive integer types to provides their core properties to generic
> code.
>
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
> ---
> rust/kernel/lib.rs | 1 +
> rust/kernel/num.rs | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 77 insertions(+)
>
> diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
> index 3dd7bebe7888..235d0d8b1eff 100644
> --- a/rust/kernel/lib.rs
> +++ b/rust/kernel/lib.rs
> @@ -109,6 +109,7 @@
> pub mod mm;
> #[cfg(CONFIG_NET)]
> pub mod net;
> +pub mod num;
> pub mod of;
> #[cfg(CONFIG_PM_OPP)]
> pub mod opp;
> diff --git a/rust/kernel/num.rs b/rust/kernel/num.rs
> new file mode 100644
> index 000000000000..3f85e50b8632
> --- /dev/null
> +++ b/rust/kernel/num.rs
> @@ -0,0 +1,76 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +//! Additional numerical features for the kernel.
> +
> +use core::ops;
> +
> +/// Designates unsigned primitive types.
> +pub struct Unsigned(());
> +
> +/// Designates signed primitive types.
> +pub struct Signed(());
Since these types are not intended to be constructed, I would suggest
using enums so that they can't be constructed:
pub enum Unsigned {}
pub enum Signed {}
Alice
On Thu Nov 6, 2025 at 6:46 PM JST, Alice Ryhl wrote:
> On Thu, Nov 06, 2025 at 04:07:13PM +0900, Alexandre Courbot wrote:
>> Introduce the `num` module, which will provide numerical extensions and
>> utilities for the kernel.
>>
>> For now, introduce the `Integer` trait, which is implemented for all
>> primitive integer types to provides their core properties to generic
>> code.
>>
>> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
>> ---
>> rust/kernel/lib.rs | 1 +
>> rust/kernel/num.rs | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
>> 2 files changed, 77 insertions(+)
>>
>> diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
>> index 3dd7bebe7888..235d0d8b1eff 100644
>> --- a/rust/kernel/lib.rs
>> +++ b/rust/kernel/lib.rs
>> @@ -109,6 +109,7 @@
>> pub mod mm;
>> #[cfg(CONFIG_NET)]
>> pub mod net;
>> +pub mod num;
>> pub mod of;
>> #[cfg(CONFIG_PM_OPP)]
>> pub mod opp;
>> diff --git a/rust/kernel/num.rs b/rust/kernel/num.rs
>> new file mode 100644
>> index 000000000000..3f85e50b8632
>> --- /dev/null
>> +++ b/rust/kernel/num.rs
>> @@ -0,0 +1,76 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +
>> +//! Additional numerical features for the kernel.
>> +
>> +use core::ops;
>> +
>> +/// Designates unsigned primitive types.
>> +pub struct Unsigned(());
>> +
>> +/// Designates signed primitive types.
>> +pub struct Signed(());
>
> Since these types are not intended to be constructed, I would suggest
> using enums so that they can't be constructed:
>
> pub enum Unsigned {}
> pub enum Signed {}
Ah, of course - I thought about having a private `()` to forbid
construction outside of the module, but the empty enum prevents them
from being instantiated from the module as well! Will apply, thanks!
© 2016 - 2026 Red Hat, Inc.