aboutsummaryrefslogtreecommitdiff
path: root/src/convert.rs
blob: afc5faf5344961e22b7d26f65ecd1d818d8876af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
pub(crate) fn usize_to_u32(u: usize) -> Option<u32> {
    #[cfg(not(no_try_from))]
    {
        use core::convert::TryFrom;

        u32::try_from(u).ok()
    }

    #[cfg(no_try_from)]
    {
        use core::mem;

        if mem::size_of::<usize>() <= mem::size_of::<u32>() || u <= u32::max_value() as usize {
            Some(u as u32)
        } else {
            None
        }
    }
}