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
77
78
79
80
pub trait Array {
type Item: Default;
const CAPACITY: usize;
fn as_slice(&self) -> &[Self::Item];
fn as_slice_mut(&mut self) -> &mut [Self::Item];
}
#[cfg(feature = "nightly_const_generics")]
impl<T: Default, const N: usize> Array for [T; N] {
type Item = T;
const CAPACITY: usize = N;
#[inline(always)]
#[must_use]
fn as_slice(&self) -> &[T] {
&*self
}
#[inline(always)]
#[must_use]
fn as_slice_mut(&mut self) -> &mut [T] {
&mut *self
}
}
#[cfg(not(feature = "nightly_const_generics"))]
macro_rules! impl_array_for_len {
($($len:expr),+ $(,)?) => {
$(impl<T: Default> Array for [T; $len] {
type Item = T;
const CAPACITY: usize = $len;
#[inline(always)]
#[must_use]
fn as_slice(&self) -> &[T] {
&*self
}
#[inline(always)]
#[must_use]
fn as_slice_mut(&mut self) -> &mut [T] {
&mut *self
}
})+
}
}
#[cfg(not(feature = "nightly_const_generics"))]
impl_array_for_len! {
0,
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,
64, 128, 256, 512, 1024, 2048, 4096,
}