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

    /// View this signed integer as an unsigned integer of the same width.
    ///
    /// All bits are preserved.
    fn unsigned(self) -> Self::Unsigned;
}

impl Unsigned for i8 {
    type Unsigned = u8;

    #[inline]
    fn unsigned(self) -> u8 {
        self as u8
    }
}

impl Unsigned for i16 {
    type Unsigned = u16;

    #[inline]
    fn unsigned(self) -> u16 {
        self as u16
    }
}

impl Unsigned for i32 {
    type Unsigned = u32;

    #[inline]
    fn unsigned(self) -> u32 {
        self as u32
    }
}

impl Unsigned for i64 {
    type Unsigned = u64;

    #[inline]
    fn unsigned(self) -> u64 {
        self as u64
    }
}

impl Unsigned for i128 {
    type Unsigned = u128;

    #[inline]
    fn unsigned(self) -> u128 {
        self as u128
    }
}

impl Unsigned for isize {
    type Unsigned = usize;

    #[inline]
    fn unsigned(self) -> usize {
        self as usize
    }
}