1use core::ops::{Range, RangeFrom, RangeInclusive};
2
3use crate::{
4 ContiguousBorrowed, ContiguousMut, Slice, SliceBorrowed, SliceMut, SliceOwned, Unique,
5};
6
7impl<T, const N: usize> Slice for [T; N] {
8 type Output = T;
9
10 fn len(&self) -> usize {
11 N
12 }
13
14 fn get_with<W: FnMut(&Self::Output) -> R, R>(&self, index: usize, f: &mut W) -> Option<R> {
15 Some(f(self.get(index)?))
16 }
17}
18
19impl<T, const N: usize> SliceOwned for [T; N]
20where
21 T: Copy,
22{
23 fn get_owned(&self, index: usize) -> Option<Self::Output> {
24 (index < N).then(|| self[index])
25 }
26}
27
28impl<T, const N: usize> SliceBorrowed for [T; N] {
29 fn get(&self, index: usize) -> Option<&Self::Output> {
30 (index < N).then(|| &self[index])
31 }
32}
33
34impl<T, const N: usize> SliceMut for [T; N] {
35 fn get_mut(&mut self, index: usize) -> Option<&mut Self::Output> {
36 (index < N).then(|| &mut self[index])
37 }
38}
39
40impl<T, const N: usize> ContiguousBorrowed for [T; N] {
41 fn contiguous(&self) -> &[T] {
42 self
43 }
44}
45
46impl<T, const N: usize> ContiguousMut for [T; N] {
47 fn contiguous_mut(&mut self) -> &mut [T] {
48 self
49 }
50}
51
52unsafe impl<T, const N: usize> Unique for [T; N] {}
54
55impl<T> Slice for [T] {
56 type Output = T;
57
58 fn len(&self) -> usize {
59 (*self).len()
60 }
61
62 fn get_with<W: FnMut(&Self::Output) -> R, R>(&self, index: usize, f: &mut W) -> Option<R> {
63 Some(f(self.get(index)?))
64 }
65}
66
67impl<T> SliceOwned for [T]
68where
69 T: Copy,
70{
71 fn get_owned(&self, index: usize) -> Option<Self::Output> {
72 (index < self.len()).then(|| self[index])
73 }
74}
75
76impl<T> SliceBorrowed for [T] {
77 fn get(&self, index: usize) -> Option<&Self::Output> {
78 (index < self.len()).then(|| &self[index])
79 }
80}
81
82impl<T> SliceMut for [T] {
83 fn get_mut(&mut self, index: usize) -> Option<&mut Self::Output> {
84 (index < self.len()).then(|| &mut self[index])
85 }
86}
87
88impl<T> ContiguousBorrowed for [T] {
89 fn contiguous(&self) -> &[T] {
90 self
91 }
92}
93
94impl<T> ContiguousMut for [T] {
95 fn contiguous_mut(&mut self) -> &mut [T] {
96 self
97 }
98}
99
100unsafe impl<T> Unique for [T] {}
102
103impl<'a, S> Slice for &'a S
104where
105 S: Slice + ?Sized,
106{
107 type Output = S::Output;
108
109 fn len(&self) -> usize {
110 (*self).len()
111 }
112
113 fn get_with<W: FnMut(&Self::Output) -> R, R>(&self, index: usize, f: &mut W) -> Option<R> {
114 (*self).get_with(index, f)
115 }
116}
117
118impl<'a, S> SliceOwned for &'a S
119where
120 S: SliceOwned + ?Sized,
121{
122 fn get_owned(&self, index: usize) -> Option<Self::Output> {
123 (*self).get_owned(index)
124 }
125}
126
127impl<'a, S> SliceBorrowed for &'a S
128where
129 S: SliceBorrowed + ?Sized,
130{
131 fn get(&self, index: usize) -> Option<&Self::Output> {
132 (**self).get(index)
133 }
134}
135
136impl<S> ContiguousBorrowed for &'_ S
137where
138 S: ContiguousBorrowed + ?Sized,
139{
140 fn contiguous(&self) -> &[S::Output] {
141 (**self).contiguous()
142 }
143}
144
145unsafe impl<'a, S> Unique for &'a S where S: Unique + ?Sized {}
147
148impl<'a, S> Slice for &'a mut S
149where
150 S: Slice + ?Sized,
151{
152 type Output = S::Output;
153
154 fn len(&self) -> usize {
155 (**self).len()
156 }
157
158 fn get_with<W: FnMut(&Self::Output) -> R, R>(&self, index: usize, f: &mut W) -> Option<R> {
159 (**self).get_with(index, f)
160 }
161}
162
163impl<'a, S> SliceOwned for &'a mut S
164where
165 S: SliceOwned + ?Sized,
166{
167 fn get_owned(&self, index: usize) -> Option<Self::Output> {
168 (**self).get_owned(index)
169 }
170}
171
172impl<'a, S> SliceBorrowed for &'a mut S
173where
174 S: SliceBorrowed + ?Sized,
175{
176 fn get(&self, index: usize) -> Option<&Self::Output> {
177 (**self).get(index)
178 }
179}
180
181impl<'a, S> SliceMut for &'a mut S
182where
183 S: SliceMut + ?Sized,
184{
185 fn get_mut(&mut self, index: usize) -> Option<&mut Self::Output> {
186 (**self).get_mut(index)
187 }
188}
189
190impl<S> ContiguousBorrowed for &'_ mut S
191where
192 S: ContiguousBorrowed + ?Sized,
193{
194 fn contiguous(&self) -> &[S::Output] {
195 (**self).contiguous()
196 }
197}
198
199impl<S> ContiguousMut for &'_ mut S
200where
201 S: ContiguousMut + ?Sized,
202{
203 fn contiguous_mut(&mut self) -> &mut [S::Output] {
204 (**self).contiguous_mut()
205 }
206}
207
208unsafe impl<'a, S> Unique for &'a mut S where S: Unique + ?Sized {}
210
211#[cfg(feature = "std")]
212mod with_std {
213 use crate::{
214 ContiguousBorrowed, ContiguousMut, Slice, SliceBorrowed, SliceMut, SliceOwned, Unique,
215 };
216 impl<T> Slice for Vec<T> {
217 type Output = T;
218
219 fn len(&self) -> usize {
220 (*self).len()
221 }
222
223 fn get_with<W: FnMut(&Self::Output) -> R, R>(&self, index: usize, f: &mut W) -> Option<R> {
224 Some(f(self.get(index)?))
225 }
226 }
227
228 impl<T> SliceOwned for Vec<T>
229 where
230 T: Copy,
231 {
232 fn get_owned(&self, index: usize) -> Option<Self::Output> {
233 (index < self.len()).then(|| self[index])
234 }
235 }
236
237 impl<T> SliceBorrowed for Vec<T> {
238 fn get(&self, index: usize) -> Option<&Self::Output> {
239 (index < self.len()).then(|| &self[index])
240 }
241 }
242
243 impl<T> SliceMut for Vec<T> {
244 fn get_mut(&mut self, index: usize) -> Option<&mut Self::Output> {
245 (index < self.len()).then(|| &mut self[index])
246 }
247 }
248
249 impl<T> ContiguousBorrowed for Vec<T> {
250 fn contiguous(&self) -> &[T] {
251 &*self
252 }
253 }
254
255 impl<T> ContiguousMut for Vec<T> {
256 fn contiguous_mut(&mut self) -> &mut [T] {
257 &mut *self
258 }
259 }
260
261 unsafe impl<T> Unique for Vec<T> {}
263}
264
265macro_rules! impl_for_range {
266 ($($range:ident),*) => {$(
267 impl<T> Slice for $range<T>
268 where
269 T: Clone,
270 $range<T>: Iterator<Item = T>,
271 {
272 type Output = T;
273
274 fn len(&self) -> usize {
275 self.size_hint().0
276 }
277
278 fn get_with<W: FnMut(&Self::Output) -> R, R>(&self, index: usize, f: &mut W)
279 -> Option<R>
280 {
281 self.clone().nth(index)
282 .as_ref()
283 .map(f)
284 }
285 }
286
287 impl<T> SliceOwned for $range<T>
288 where
289 T: Clone,
290 $range<T>: Iterator<Item = T>,
291 {
292 fn get_owned(&self, index: usize) -> Option<T> {
293 self.clone().nth(index)
294 }
295 }
296 )*};
297}
298
299impl_for_range!(Range, RangeFrom, RangeInclusive);