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
use {Error, ErrorKind};
use std;
pub trait Index : Copy + Into<u64> {
fn from_u64(v: u64) -> Result<Self, Error>;
}
macro_rules! impl_index {
($ty:ident) => {
impl Index for $ty {
fn from_u64(i: u64) -> Result<$ty, Error> {
if i < $ty::max_value() as u64 {
Ok(i as _)
} else {
let bits_available = std::mem::size_of::<$ty>() as u8 * 8;
return Err(ErrorKind::IndexTooSmall(i, bits_available).into())
}
}
}
}
}
impl_index!(u64);
impl_index!(u32);
impl_index!(u16);
impl_index!(u8);