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