1+ #![ unstable( feature = "sliceindex_wrappers" , issue = "146179" ) ]
2+
3+ //! Helper types for indexing slices.
4+
15use crate :: intrinsics:: slice_get_unchecked;
2- use crate :: range;
36use crate :: slice:: SliceIndex ;
4-
7+ use crate :: { ops, range} ;
8+
9+ /// Clamps an index, guaranteeing that it will only access valid elements of the slice.
10+ ///
11+ /// # Examples
12+ ///
13+ /// ```
14+ /// #![feature(sliceindex_wrappers)]
15+ ///
16+ /// use core::index::Clamp;
17+ ///
18+ /// let s: &[usize] = &[0, 1, 2, 3];
19+ ///
20+ /// assert_eq!(&3, &s[Clamp(6)]);
21+ /// assert_eq!(&[1, 2, 3], &s[Clamp(1..6)]);
22+ /// assert_eq!(&[0, 1, 2, 3], &s[Clamp(..6)]);
23+ /// assert_eq!(&[0, 1, 2, 3], &s[Clamp(..6)]);
24+ /// assert_eq!(&[0, 1, 2, 3], &s[Clamp(..=6)]);
25+ /// assert_eq!(&[3], &s[Clamp(6..)]);
26+ /// ```
527#[ unstable( feature = "sliceindex_wrappers" , issue = "146179" ) ]
28+ #[ derive( Debug ) ]
629pub struct Clamp < Idx > ( pub Idx ) ;
730
31+ /// Always accesses the last element of the slice.
32+ ///
33+ /// # Examples
34+ ///
35+ /// ```
36+ /// #![feature(sliceindex_wrappers)]
37+ ///
38+ /// use core::index::Last;
39+ ///
40+ /// let s = &[0, 1, 2, 3];
41+ ///
42+ /// assert_eq!(&3, &s[Last]);
843#[ unstable( feature = "sliceindex_wrappers" , issue = "146179" ) ]
44+ #[ derive( Debug ) ]
945pub struct Last ;
1046
1147#[ unstable( feature = "sliceindex_wrappers" , issue = "146179" ) ]
@@ -44,29 +80,64 @@ unsafe impl<T> SliceIndex<[T]> for Clamp<range::Range<usize>> {
4480 type Output = [ T ] ;
4581
4682 fn get ( self , slice : & [ T ] ) -> Option < & Self :: Output > {
47- ( self . 0 . start ..self . 0 . end . min ( slice. len ( ) ) ) . get ( slice)
83+ ( self . 0 . start . min ( slice. len ( ) ) ..self . 0 . end . min ( slice. len ( ) ) ) . get ( slice)
84+ }
85+
86+ fn get_mut ( self , slice : & mut [ T ] ) -> Option < & mut Self :: Output > {
87+ ( self . 0 . start . min ( slice. len ( ) ) ..self . 0 . end . min ( slice. len ( ) ) ) . get_mut ( slice)
88+ }
89+
90+ unsafe fn get_unchecked ( self , slice : * const [ T ] ) -> * const Self :: Output {
91+ // SAFETY: a range ending before len is always valid
92+ unsafe { ( self . 0 . start . min ( slice. len ( ) ) ..self . 0 . end . min ( slice. len ( ) ) ) . get_unchecked ( slice) }
93+ }
94+
95+ unsafe fn get_unchecked_mut ( self , slice : * mut [ T ] ) -> * mut Self :: Output {
96+ // SAFETY: a range ending before len is always valid
97+ unsafe {
98+ ( self . 0 . start . min ( slice. len ( ) ) ..self . 0 . end . min ( slice. len ( ) ) ) . get_unchecked_mut ( slice)
99+ }
100+ }
101+
102+ fn index ( self , slice : & [ T ] ) -> & Self :: Output {
103+ ( self . 0 . start . min ( slice. len ( ) ) ..self . 0 . end . min ( slice. len ( ) ) ) . index ( slice)
104+ }
105+
106+ fn index_mut ( self , slice : & mut [ T ] ) -> & mut Self :: Output {
107+ ( self . 0 . start . min ( slice. len ( ) ) ..self . 0 . end . min ( slice. len ( ) ) ) . index_mut ( slice)
108+ }
109+ }
110+
111+ #[ unstable( feature = "sliceindex_wrappers" , issue = "146179" ) ]
112+ unsafe impl < T > SliceIndex < [ T ] > for Clamp < ops:: Range < usize > > {
113+ type Output = [ T ] ;
114+
115+ fn get ( self , slice : & [ T ] ) -> Option < & Self :: Output > {
116+ ( self . 0 . start . min ( slice. len ( ) ) ..self . 0 . end . min ( slice. len ( ) ) ) . get ( slice)
48117 }
49118
50119 fn get_mut ( self , slice : & mut [ T ] ) -> Option < & mut Self :: Output > {
51- ( self . 0 . start ..self . 0 . end . min ( slice. len ( ) ) ) . get_mut ( slice)
120+ ( self . 0 . start . min ( slice . len ( ) ) . .self . 0 . end . min ( slice. len ( ) ) ) . get_mut ( slice)
52121 }
53122
54123 unsafe fn get_unchecked ( self , slice : * const [ T ] ) -> * const Self :: Output {
55124 // SAFETY: a range ending before len is always valid
56- unsafe { ( self . 0 . start ..self . 0 . end . min ( slice. len ( ) ) ) . get_unchecked ( slice) }
125+ unsafe { ( self . 0 . start . min ( slice . len ( ) ) . .self . 0 . end . min ( slice. len ( ) ) ) . get_unchecked ( slice) }
57126 }
58127
59128 unsafe fn get_unchecked_mut ( self , slice : * mut [ T ] ) -> * mut Self :: Output {
60129 // SAFETY: a range ending before len is always valid
61- unsafe { ( self . 0 . start ..self . 0 . end . min ( slice. len ( ) ) ) . get_unchecked_mut ( slice) }
130+ unsafe {
131+ ( self . 0 . start . min ( slice. len ( ) ) ..self . 0 . end . min ( slice. len ( ) ) ) . get_unchecked_mut ( slice)
132+ }
62133 }
63134
64135 fn index ( self , slice : & [ T ] ) -> & Self :: Output {
65- ( self . 0 . start ..self . 0 . end . min ( slice. len ( ) ) ) . index ( slice)
136+ ( self . 0 . start . min ( slice . len ( ) ) . .self . 0 . end . min ( slice. len ( ) ) ) . index ( slice)
66137 }
67138
68139 fn index_mut ( self , slice : & mut [ T ] ) -> & mut Self :: Output {
69- ( self . 0 . start ..self . 0 . end . min ( slice. len ( ) ) ) . index_mut ( slice)
140+ ( self . 0 . start . min ( slice . len ( ) ) . .self . 0 . end . min ( slice. len ( ) ) ) . index_mut ( slice)
70141 }
71142}
72143
@@ -75,29 +146,35 @@ unsafe impl<T> SliceIndex<[T]> for Clamp<range::RangeInclusive<usize>> {
75146 type Output = [ T ] ;
76147
77148 fn get ( self , slice : & [ T ] ) -> Option < & Self :: Output > {
78- ( self . 0 . start ..=self . 0 . end . min ( slice. len ( ) - 1 ) ) . get ( slice)
149+ ( self . 0 . start . min ( slice . len ( ) - 1 ) . .=self . 0 . end . min ( slice. len ( ) - 1 ) ) . get ( slice)
79150 }
80151
81152 fn get_mut ( self , slice : & mut [ T ] ) -> Option < & mut Self :: Output > {
82- ( self . 0 . start ..=self . 0 . end . min ( slice. len ( ) - 1 ) ) . get_mut ( slice)
153+ ( self . 0 . start . min ( slice . len ( ) - 1 ) . .=self . 0 . end . min ( slice. len ( ) - 1 ) ) . get_mut ( slice)
83154 }
84155
85156 unsafe fn get_unchecked ( self , slice : * const [ T ] ) -> * const Self :: Output {
86157 // SAFETY: the index before the length is always valid
87- unsafe { ( self . 0 . start ..=self . 0 . end . min ( slice. len ( ) - 1 ) ) . get_unchecked ( slice) }
158+ unsafe {
159+ ( self . 0 . start . min ( slice. len ( ) - 1 ) ..=self . 0 . end . min ( slice. len ( ) - 1 ) )
160+ . get_unchecked ( slice)
161+ }
88162 }
89163
90164 unsafe fn get_unchecked_mut ( self , slice : * mut [ T ] ) -> * mut Self :: Output {
91165 // SAFETY: the index before the length is always valid
92- unsafe { ( self . 0 . start ..=self . 0 . end . min ( slice. len ( ) - 1 ) ) . get_unchecked_mut ( slice) }
166+ unsafe {
167+ ( self . 0 . start . min ( slice. len ( ) - 1 ) ..=self . 0 . end . min ( slice. len ( ) - 1 ) )
168+ . get_unchecked_mut ( slice)
169+ }
93170 }
94171
95172 fn index ( self , slice : & [ T ] ) -> & Self :: Output {
96- ( self . 0 . start ..=self . 0 . end . min ( slice. len ( ) - 1 ) ) . index ( slice)
173+ ( self . 0 . start . min ( slice . len ( ) - 1 ) . .=self . 0 . end . min ( slice. len ( ) - 1 ) ) . index ( slice)
97174 }
98175
99176 fn index_mut ( self , slice : & mut [ T ] ) -> & mut Self :: Output {
100- ( self . 0 . start ..=self . 0 . end . min ( slice. len ( ) - 1 ) ) . index_mut ( slice)
177+ ( self . 0 . start . min ( slice . len ( ) - 1 ) . .=self . 0 . end . min ( slice. len ( ) - 1 ) ) . index_mut ( slice)
101178 }
102179}
103180
@@ -106,29 +183,60 @@ unsafe impl<T> SliceIndex<[T]> for Clamp<range::RangeFrom<usize>> {
106183 type Output = [ T ] ;
107184
108185 fn get ( self , slice : & [ T ] ) -> Option < & Self :: Output > {
109- ( self . 0 . start .. slice. len ( ) ) . get ( slice)
186+ ( self . 0 . start . min ( slice. len ( ) - 1 ) .. ) . get ( slice)
110187 }
111188
112189 fn get_mut ( self , slice : & mut [ T ] ) -> Option < & mut Self :: Output > {
113- ( self . 0 . start .. slice. len ( ) ) . get_mut ( slice)
190+ ( self . 0 . start . min ( slice. len ( ) - 1 ) .. ) . get_mut ( slice)
114191 }
115192
116193 unsafe fn get_unchecked ( self , slice : * const [ T ] ) -> * const Self :: Output {
117194 // SAFETY: a range ending before len is always valid
118- unsafe { ( self . 0 . start .. slice. len ( ) ) . get_unchecked ( slice) }
195+ unsafe { ( self . 0 . start . min ( slice. len ( ) - 1 ) .. ) . get_unchecked ( slice) }
119196 }
120197
121198 unsafe fn get_unchecked_mut ( self , slice : * mut [ T ] ) -> * mut Self :: Output {
122199 // SAFETY: a range ending before len is always valid
123- unsafe { ( self . 0 . start .. slice. len ( ) ) . get_unchecked_mut ( slice) }
200+ unsafe { ( self . 0 . start . min ( slice. len ( ) - 1 ) .. ) . get_unchecked_mut ( slice) }
124201 }
125202
126203 fn index ( self , slice : & [ T ] ) -> & Self :: Output {
127- ( self . 0 . start .. slice. len ( ) ) . index ( slice)
204+ ( self . 0 . start . min ( slice. len ( ) - 1 ) .. ) . index ( slice)
128205 }
129206
130207 fn index_mut ( self , slice : & mut [ T ] ) -> & mut Self :: Output {
131- ( self . 0 . start ..slice. len ( ) ) . index_mut ( slice)
208+ ( self . 0 . start . min ( slice. len ( ) - 1 ) ..) . index_mut ( slice)
209+ }
210+ }
211+
212+ #[ unstable( feature = "sliceindex_wrappers" , issue = "146179" ) ]
213+ unsafe impl < T > SliceIndex < [ T ] > for Clamp < ops:: RangeFrom < usize > > {
214+ type Output = [ T ] ;
215+
216+ fn get ( self , slice : & [ T ] ) -> Option < & Self :: Output > {
217+ ( self . 0 . start . min ( slice. len ( ) - 1 ) ..) . get ( slice)
218+ }
219+
220+ fn get_mut ( self , slice : & mut [ T ] ) -> Option < & mut Self :: Output > {
221+ ( self . 0 . start . min ( slice. len ( ) - 1 ) ..) . get_mut ( slice)
222+ }
223+
224+ unsafe fn get_unchecked ( self , slice : * const [ T ] ) -> * const Self :: Output {
225+ // SAFETY: a range ending before len is always valid
226+ unsafe { ( self . 0 . start . min ( slice. len ( ) - 1 ) ..) . get_unchecked ( slice) }
227+ }
228+
229+ unsafe fn get_unchecked_mut ( self , slice : * mut [ T ] ) -> * mut Self :: Output {
230+ // SAFETY: a range ending before len is always valid
231+ unsafe { ( self . 0 . start . min ( slice. len ( ) - 1 ) ..) . get_unchecked_mut ( slice) }
232+ }
233+
234+ fn index ( self , slice : & [ T ] ) -> & Self :: Output {
235+ ( self . 0 . start . min ( slice. len ( ) - 1 ) ..) . index ( slice)
236+ }
237+
238+ fn index_mut ( self , slice : & mut [ T ] ) -> & mut Self :: Output {
239+ ( self . 0 . start . min ( slice. len ( ) - 1 ) ..) . index_mut ( slice)
132240 }
133241}
134242
@@ -137,29 +245,29 @@ unsafe impl<T> SliceIndex<[T]> for Clamp<range::RangeTo<usize>> {
137245 type Output = [ T ] ;
138246
139247 fn get ( self , slice : & [ T ] ) -> Option < & Self :: Output > {
140- ( 0 ..self . 0 . end . min ( slice. len ( ) ) ) . get ( slice)
248+ ( ..self . 0 . end . min ( slice. len ( ) ) ) . get ( slice)
141249 }
142250
143251 fn get_mut ( self , slice : & mut [ T ] ) -> Option < & mut Self :: Output > {
144- ( 0 ..self . 0 . end . min ( slice. len ( ) ) ) . get_mut ( slice)
252+ ( ..self . 0 . end . min ( slice. len ( ) ) ) . get_mut ( slice)
145253 }
146254
147255 unsafe fn get_unchecked ( self , slice : * const [ T ] ) -> * const Self :: Output {
148256 // SAFETY: a range ending before len is always valid
149- unsafe { ( 0 ..self . 0 . end . min ( slice. len ( ) ) ) . get_unchecked ( slice) }
257+ unsafe { ( ..self . 0 . end . min ( slice. len ( ) ) ) . get_unchecked ( slice) }
150258 }
151259
152260 unsafe fn get_unchecked_mut ( self , slice : * mut [ T ] ) -> * mut Self :: Output {
153261 // SAFETY: a range ending before len is always valid
154- unsafe { ( 0 ..self . 0 . end . min ( slice. len ( ) ) ) . get_unchecked_mut ( slice) }
262+ unsafe { ( ..self . 0 . end . min ( slice. len ( ) ) ) . get_unchecked_mut ( slice) }
155263 }
156264
157265 fn index ( self , slice : & [ T ] ) -> & Self :: Output {
158- ( 0 ..self . 0 . end . min ( slice. len ( ) ) ) . index ( slice)
266+ ( ..self . 0 . end . min ( slice. len ( ) ) ) . index ( slice)
159267 }
160268
161269 fn index_mut ( self , slice : & mut [ T ] ) -> & mut Self :: Output {
162- ( 0 ..self . 0 . end . min ( slice. len ( ) ) ) . index_mut ( slice)
270+ ( ..self . 0 . end . min ( slice. len ( ) ) ) . index_mut ( slice)
163271 }
164272}
165273
@@ -168,29 +276,60 @@ unsafe impl<T> SliceIndex<[T]> for Clamp<range::RangeToInclusive<usize>> {
168276 type Output = [ T ] ;
169277
170278 fn get ( self , slice : & [ T ] ) -> Option < & Self :: Output > {
171- ( 0 ..=self . 0 . end . min ( slice. len ( ) - 1 ) ) . get ( slice)
279+ ( ..=self . 0 . end . min ( slice. len ( ) - 1 ) ) . get ( slice)
280+ }
281+
282+ fn get_mut ( self , slice : & mut [ T ] ) -> Option < & mut Self :: Output > {
283+ ( ..=self . 0 . end . min ( slice. len ( ) - 1 ) ) . get_mut ( slice)
284+ }
285+
286+ unsafe fn get_unchecked ( self , slice : * const [ T ] ) -> * const Self :: Output {
287+ // SAFETY: the index before the length is always valid
288+ unsafe { ( ..=self . 0 . end . min ( slice. len ( ) - 1 ) ) . get_unchecked ( slice) }
289+ }
290+
291+ unsafe fn get_unchecked_mut ( self , slice : * mut [ T ] ) -> * mut Self :: Output {
292+ // SAFETY: the index before the length is always valid
293+ unsafe { ( ..=self . 0 . end . min ( slice. len ( ) - 1 ) ) . get_unchecked_mut ( slice) }
294+ }
295+
296+ fn index ( self , slice : & [ T ] ) -> & Self :: Output {
297+ ( ..=self . 0 . end . min ( slice. len ( ) - 1 ) ) . index ( slice)
298+ }
299+
300+ fn index_mut ( self , slice : & mut [ T ] ) -> & mut Self :: Output {
301+ ( ..=self . 0 . end . min ( slice. len ( ) - 1 ) ) . index_mut ( slice)
302+ }
303+ }
304+
305+ #[ unstable( feature = "sliceindex_wrappers" , issue = "146179" ) ]
306+ unsafe impl < T > SliceIndex < [ T ] > for Clamp < range:: RangeFull > {
307+ type Output = [ T ] ;
308+
309+ fn get ( self , slice : & [ T ] ) -> Option < & Self :: Output > {
310+ ( ..) . get ( slice)
172311 }
173312
174313 fn get_mut ( self , slice : & mut [ T ] ) -> Option < & mut Self :: Output > {
175- ( 0 ..= self . 0 . end . min ( slice . len ( ) - 1 ) ) . get_mut ( slice)
314+ ( .. ) . get_mut ( slice)
176315 }
177316
178317 unsafe fn get_unchecked ( self , slice : * const [ T ] ) -> * const Self :: Output {
179318 // SAFETY: the index before the length is always valid
180- unsafe { ( 0 ..= self . 0 . end . min ( slice . len ( ) - 1 ) ) . get_unchecked ( slice) }
319+ unsafe { ( .. ) . get_unchecked ( slice) }
181320 }
182321
183322 unsafe fn get_unchecked_mut ( self , slice : * mut [ T ] ) -> * mut Self :: Output {
184323 // SAFETY: the index before the length is always valid
185- unsafe { ( 0 ..= self . 0 . end . min ( slice . len ( ) - 1 ) ) . get_unchecked_mut ( slice) }
324+ unsafe { ( .. ) . get_unchecked_mut ( slice) }
186325 }
187326
188327 fn index ( self , slice : & [ T ] ) -> & Self :: Output {
189- ( 0 ..= self . 0 . end . min ( slice . len ( ) - 1 ) ) . index ( slice)
328+ ( .. ) . index ( slice)
190329 }
191330
192331 fn index_mut ( self , slice : & mut [ T ] ) -> & mut Self :: Output {
193- ( 0 ..= self . 0 . end . min ( slice . len ( ) - 1 ) ) . index_mut ( slice)
332+ ( .. ) . index_mut ( slice)
194333 }
195334}
196335
0 commit comments