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
#![cfg_attr(not(test), no_std)]

use core::mem::size_of;

/// Converts a value to `usize`, or raises a compile error if the **type** is wider than a `usize`.
///
/// Unlike `try_into` + `unwrap`, this fails even if the **value** would fit in `usize`.
pub trait AsUsize {
    fn as_usize(self) -> usize;
}

macro_rules! impl_for {
    ($ty:ty) => {
        impl AsUsize for $ty {
            fn as_usize(self) -> usize {
                const _: () = assert!(size_of::<usize>() >= size_of::<$ty>());

                self as usize
            }
        }
    };
}

impl_for!(u8);
impl_for!(u16);
impl_for!(u32);
impl_for!(u64);