|
13 | 13 | // If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. |
14 | 14 | // |
15 | 15 |
|
16 | | -// This is a macro that routinely comes in handy |
| 16 | +/// Implement methods and traits for types that contain an inner array. |
17 | 17 | #[macro_export] |
18 | 18 | macro_rules! impl_array_newtype { |
19 | 19 | ($thing:ident, $ty:ty, $len:expr) => { |
20 | 20 | impl Copy for $thing {} |
21 | 21 |
|
22 | 22 | impl $thing { |
23 | | - /// Converts the object to a raw pointer for FFI interfacing |
| 23 | + /// Converts the object to a raw pointer for FFI interfacing. |
24 | 24 | #[inline] |
25 | 25 | pub fn as_ptr(&self) -> *const $ty { |
26 | 26 | let &$thing(ref dat) = self; |
27 | 27 | dat.as_ptr() |
28 | 28 | } |
29 | 29 |
|
30 | | - /// Converts the object to a mutable raw pointer for FFI interfacing |
| 30 | + /// Converts the object to a mutable raw pointer for FFI interfacing. |
31 | 31 | #[inline] |
32 | 32 | pub fn as_mut_ptr(&mut self) -> *mut $ty { |
33 | 33 | let &mut $thing(ref mut dat) = self; |
34 | 34 | dat.as_mut_ptr() |
35 | 35 | } |
36 | 36 |
|
37 | | - /// Returns the length of the object as an array |
| 37 | + /// Returns the length of the object as an array. |
38 | 38 | #[inline] |
39 | 39 | pub fn len(&self) -> usize { $len } |
40 | 40 |
|
41 | | - /// Returns whether the object as an array is empty |
| 41 | + /// Returns whether the object as an array is empty. |
42 | 42 | #[inline] |
43 | 43 | pub fn is_empty(&self) -> bool { false } |
44 | 44 | } |
@@ -89,55 +89,16 @@ macro_rules! impl_array_newtype { |
89 | 89 | } |
90 | 90 | } |
91 | 91 |
|
92 | | - impl core::ops::Index<usize> for $thing { |
93 | | - type Output = $ty; |
| 92 | + impl<I> core::ops::Index<I> for $thing |
| 93 | + where |
| 94 | + [$ty]: core::ops::Index<I>, |
| 95 | + { |
| 96 | + type Output = <[$ty] as core::ops::Index<I>>::Output; |
94 | 97 |
|
95 | 98 | #[inline] |
96 | | - fn index(&self, index: usize) -> &$ty { |
97 | | - let &$thing(ref dat) = self; |
98 | | - &dat[index] |
99 | | - } |
| 99 | + fn index(&self, index: I) -> &Self::Output { &self.0[index] } |
100 | 100 | } |
101 | 101 |
|
102 | | - impl core::ops::Index<core::ops::Range<usize>> for $thing { |
103 | | - type Output = [$ty]; |
104 | | - |
105 | | - #[inline] |
106 | | - fn index(&self, index: core::ops::Range<usize>) -> &[$ty] { |
107 | | - let &$thing(ref dat) = self; |
108 | | - &dat[index] |
109 | | - } |
110 | | - } |
111 | | - |
112 | | - impl core::ops::Index<core::ops::RangeTo<usize>> for $thing { |
113 | | - type Output = [$ty]; |
114 | | - |
115 | | - #[inline] |
116 | | - fn index(&self, index: core::ops::RangeTo<usize>) -> &[$ty] { |
117 | | - let &$thing(ref dat) = self; |
118 | | - &dat[index] |
119 | | - } |
120 | | - } |
121 | | - |
122 | | - impl core::ops::Index<core::ops::RangeFrom<usize>> for $thing { |
123 | | - type Output = [$ty]; |
124 | | - |
125 | | - #[inline] |
126 | | - fn index(&self, index: core::ops::RangeFrom<usize>) -> &[$ty] { |
127 | | - let &$thing(ref dat) = self; |
128 | | - &dat[index] |
129 | | - } |
130 | | - } |
131 | | - |
132 | | - impl core::ops::Index<core::ops::RangeFull> for $thing { |
133 | | - type Output = [$ty]; |
134 | | - |
135 | | - #[inline] |
136 | | - fn index(&self, _: core::ops::RangeFull) -> &[$ty] { |
137 | | - let &$thing(ref dat) = self; |
138 | | - &dat[..] |
139 | | - } |
140 | | - } |
141 | 102 | impl $crate::CPtr for $thing { |
142 | 103 | type Target = $ty; |
143 | 104 | fn as_c_ptr(&self) -> *const Self::Target { |
|
0 commit comments