cranelift_entity/
signed.rs

1/// Helper trait used to add `signed()` methods to primitive unsigned integer
2/// types.
3///
4/// The purpose of this trait is to signal the intent that the sign bit of an
5/// unsigned integer is intended to be discarded and the value is instead
6/// understood to be a "bag of bits" where the conversion to a signed number
7/// is intended to be lossless bit-wise. This can be used for example when
8/// converting an unsigned integer into a signed integer for constrained reasons
9/// outside the scope of the code in question.
10pub trait Signed {
11    /// The signed integer for this type which has the same width.
12    type Signed;
13
14    /// View this unsigned integer as a signed integer of the same width.
15    ///
16    /// All bits are preserved.
17    fn signed(self) -> Self::Signed;
18}
19
20macro_rules! impls {
21    ($($unsigned:ident => $signed:ident)*) => {$(
22        impl Signed for $unsigned {
23            type Signed = $signed;
24
25            #[inline]
26            fn signed(self) -> $signed {
27                self as $signed
28            }
29        }
30    )*}
31}
32
33impls! {
34    u8 => i8
35    u16 => i16
36    u32 => i32
37    u64 => i64
38    u128 => i128
39    usize => isize
40}