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
72
73
74
75
76
77
78
79
80
81
//! Conversion traits and functions for conversions between `usize` and fixed sized integers.
//!
//! Warning: The traits are conditionally implemented based on the target pointer width,
//! so they can make your crate less portable.

#![feature(i128_type)]
#![warn(missing_docs)]

#![no_std]

/// Create an usize from a fixed sized integer.
pub fn usize_from<T>(value: T) -> usize where T: IntoUsize {
    IntoUsize::into_usize(value)
}

/// Convert a type to `usize`.
pub trait IntoUsize: Sized {
    /// Performs the conversion.
    fn into_usize(self) -> usize;
}

/// Convert a type from `usize`.
pub trait FromUsize: Sized {
    /// Performs the conversion.
    fn from_usize(usize) -> Self;
}

macro_rules! impl_into_usize {
    ($x:ty) => {
        impl IntoUsize for $x {
            fn into_usize(self) -> usize {
                self as usize
            }
        }
    }
}

macro_rules! impl_from_usize {
    ($x:ty) => {
        impl FromUsize for $x {
            fn from_usize(value: usize) -> Self {
                value as Self
            }
        }
    }
}

#[cfg(target_pointer_width = "128")] impl_from_usize!(u128);
#[cfg(target_pointer_width = "128")] impl_into_usize!(u128);
#[cfg(target_pointer_width = "128")] impl_into_usize!(u64);
#[cfg(target_pointer_width = "128")] impl_into_usize!(u32);
#[cfg(target_pointer_width = "128")] impl_into_usize!(u16);
#[cfg(target_pointer_width = "128")] impl_into_usize!(u8);

#[cfg(target_pointer_width = "64")] impl_from_usize!(u128);
#[cfg(target_pointer_width = "64")] impl_from_usize!(u64);
#[cfg(target_pointer_width = "64")] impl_into_usize!(u64);
#[cfg(target_pointer_width = "64")] impl_into_usize!(u32);
#[cfg(target_pointer_width = "64")] impl_into_usize!(u16);
#[cfg(target_pointer_width = "64")] impl_into_usize!(u8);

#[cfg(target_pointer_width = "32")] impl_from_usize!(u128);
#[cfg(target_pointer_width = "32")] impl_from_usize!(u64);
#[cfg(target_pointer_width = "32")] impl_from_usize!(u32);
#[cfg(target_pointer_width = "32")] impl_into_usize!(u32);
#[cfg(target_pointer_width = "32")] impl_into_usize!(u16);
#[cfg(target_pointer_width = "32")] impl_into_usize!(u8);

#[cfg(target_pointer_width = "16")] impl_from_usize!(u128);
#[cfg(target_pointer_width = "16")] impl_from_usize!(u64);
#[cfg(target_pointer_width = "16")] impl_from_usize!(u32);
#[cfg(target_pointer_width = "16")] impl_from_usize!(u16);
#[cfg(target_pointer_width = "16")] impl_into_usize!(u16);
#[cfg(target_pointer_width = "16")] impl_into_usize!(u8);

#[cfg(target_pointer_width = "8")] impl_from_usize!(u128);
#[cfg(target_pointer_width = "8")] impl_from_usize!(u64);
#[cfg(target_pointer_width = "8")] impl_from_usize!(u32);
#[cfg(target_pointer_width = "8")] impl_from_usize!(u16);
#[cfg(target_pointer_width = "8")] impl_from_usize!(u8);
#[cfg(target_pointer_width = "8")] impl_into_usize!(u8);