Skip to main content

kernel/
num.rs

1// SPDX-License-Identifier: GPL-2.0
2
3//! Additional numerical features for the kernel.
4
5use core::ops;
6
7pub mod bounded;
8pub mod casts;
9
10pub use bounded::*;
11
12/// Designates unsigned primitive types.
13pub enum Unsigned {}
14
15/// Designates signed primitive types.
16pub enum Signed {}
17
18/// Describes core properties of integer types.
19pub trait Integer:
20    Sized
21    + Copy
22    + Clone
23    + PartialEq
24    + Eq
25    + PartialOrd
26    + Ord
27    + ops::Add<Output = Self>
28    + ops::AddAssign
29    + ops::Sub<Output = Self>
30    + ops::SubAssign
31    + ops::Mul<Output = Self>
32    + ops::MulAssign
33    + ops::Div<Output = Self>
34    + ops::DivAssign
35    + ops::Rem<Output = Self>
36    + ops::RemAssign
37    + ops::BitAnd<Output = Self>
38    + ops::BitAndAssign
39    + ops::BitOr<Output = Self>
40    + ops::BitOrAssign
41    + ops::BitXor<Output = Self>
42    + ops::BitXorAssign
43    + ops::Shl<u32, Output = Self>
44    + ops::ShlAssign<u32>
45    + ops::Shr<u32, Output = Self>
46    + ops::ShrAssign<u32>
47    + ops::Not
48{
49    /// Whether this type is [`Signed`] or [`Unsigned`].
50    type Signedness;
51
52    /// Number of bits used for value representation.
53    const BITS: u32;
54}
55
56macro_rules! impl_integer {
57    ($($type:ty: $signedness:ty), *) => {
58        $(
59        impl Integer for $type {
60            type Signedness = $signedness;
61
62            const BITS: u32 = <$type>::BITS;
63        }
64        )*
65    };
66}
67
68impl_integer!(
69    u8: Unsigned,
70    u16: Unsigned,
71    u32: Unsigned,
72    u64: Unsigned,
73    u128: Unsigned,
74    usize: Unsigned,
75    i8: Signed,
76    i16: Signed,
77    i32: Signed,
78    i64: Signed,
79    i128: Signed,
80    isize: Signed
81);