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
//! A helper trait to convert values to `usize`.
//!
//! This is most useful for code that works with array indices. The trait allows
//! to pass anything into the function that can be resolved to a `usize`.

/// A trait used to convert a type into a `usize`.
pub trait ToUsize {
    /// Converts self into `usize`.
    fn to_usize(&self) -> usize;
}

impl ToUsize for u8 {
    #[inline]
    fn to_usize(&self) -> usize {
        *self as usize
    }
}

impl ToUsize for u16 {
    #[inline]
    fn to_usize(&self) -> usize {
        *self as usize
    }
}

// 16bit platforms use a `usize` of 16bit. Converting using a cast would fail on
// those systems.
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
impl ToUsize for u32 {
    #[inline]
    fn to_usize(&self) -> usize {
        *self as usize
    }
}

// 32bit platforms use a `usize` of 32bit. Converting using a cast would fail on
// those systems.
#[cfg(target_pointer_width = "64")]
impl ToUsize for u64 {
    #[inline]
    fn to_usize(&self) -> usize {
        *self as usize
    }
}

impl ToUsize for usize {
    #[inline]
    fn to_usize(&self) -> usize {
        *self
    }
}