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
use core::convert::Infallible;
pub trait Enum<V>: Sized {
type Array: Array<V>;
fn from_usize(value: usize) -> Self;
fn into_usize(self) -> usize;
}
pub trait Array<V> {
const LENGTH: usize;
fn slice(&self) -> &[V];
fn slice_mut(&mut self) -> &mut [V];
}
impl<V, const N: usize> Array<V> for [V; N] {
const LENGTH: usize = N;
fn slice(&self) -> &[V] {
self
}
fn slice_mut(&mut self) -> &mut [V] {
self
}
}
impl<T> Enum<T> for bool {
type Array = [T; 2];
#[inline]
fn from_usize(value: usize) -> Self {
match value {
0 => false,
1 => true,
_ => unreachable!(),
}
}
#[inline]
fn into_usize(self) -> usize {
self as usize
}
}
impl<T> Enum<T> for u8 {
type Array = [T; 256];
#[inline]
fn from_usize(value: usize) -> Self {
value as u8
}
#[inline]
fn into_usize(self) -> usize {
self as usize
}
}
impl<T> Enum<T> for Infallible {
type Array = [T; 0];
#[inline]
fn from_usize(_: usize) -> Self {
unreachable!();
}
#[inline]
fn into_usize(self) -> usize {
match self {}
}
}