cranelift_entity/
unsigned.rs

1/// Helper trait used to add `unsigned()` methods to primitive signed integer
2/// types.
3///
4/// The purpose of this trait is to signal the intent that the sign bit of a
5/// signed integer is intended to be discarded and the value is instead
6/// understood to be a "bag of bits" where the conversion to an unsigned number
7/// is intended to be lossless. This can be used for example when converting a
8/// signed integer into a larger width with zero-extension.
9pub trait Unsigned {
10    /// The unsigned integer for this type which has the same width.
11    type Unsigned;
12
13    /// View this signed integer as an unsigned integer of the same width.
14    ///
15    /// All bits are preserved.
16    fn unsigned(self) -> Self::Unsigned;
17}
18
19impl Unsigned for i8 {
20    type Unsigned = u8;
21
22    #[inline]
23    fn unsigned(self) -> u8 {
24        self as u8
25    }
26}
27
28impl Unsigned for i16 {
29    type Unsigned = u16;
30
31    #[inline]
32    fn unsigned(self) -> u16 {
33        self as u16
34    }
35}
36
37impl Unsigned for i32 {
38    type Unsigned = u32;
39
40    #[inline]
41    fn unsigned(self) -> u32 {
42        self as u32
43    }
44}
45
46impl Unsigned for i64 {
47    type Unsigned = u64;
48
49    #[inline]
50    fn unsigned(self) -> u64 {
51        self as u64
52    }
53}
54
55impl Unsigned for i128 {
56    type Unsigned = u128;
57
58    #[inline]
59    fn unsigned(self) -> u128 {
60        self as u128
61    }
62}
63
64impl Unsigned for isize {
65    type Unsigned = usize;
66
67    #[inline]
68    fn unsigned(self) -> usize {
69        self as usize
70    }
71}