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
81
#![no_std]
use core::ops::{Index,IndexMut};
struct MyType;
macro_rules! array_impls {
( $($size:expr),* ) => (
$(
impl<T> Index<MyType> for [T;$size]{
type Output=T;
fn index(&self,_:MyType)->&T{
&coerce_slice(self)[0]
}
}
impl<T> IndexMut<MyType> for [T;$size]{
fn index_mut(&mut self,_:MyType)->&mut T{
&mut coerce_slice_mut(self)[0]
}
}
)*
)
}
array_impls!{
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
}
#[inline(always)]
const fn coerce_slice<'a, T>(slic: &'a [T]) -> &'a [T] {
slic
}
#[inline(always)]
fn coerce_slice_mut<'a, T>(slic: &'a mut [T]) -> &'a mut [T] {
slic
}
#[cfg(test)]
mod tests{
use crate::MyType;
#[test]
fn can_index_my_type(){
assert_eq!(&[3,4,5][MyType], &3);
assert_eq!(&mut [6,7,8][MyType], &mut 6);
}
}