]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
11 | //! Slice management and manipulation | |
12 | //! | |
13 | //! For more details `std::slice`. | |
14 | ||
85aaf69f | 15 | #![stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
16 | |
17 | // How this module is organized. | |
18 | // | |
19 | // The library infrastructure for slices is fairly messy. There's | |
20 | // a lot of stuff defined here. Let's keep it clean. | |
21 | // | |
22 | // Since slices don't support inherent methods; all operations | |
23 | // on them are defined on traits, which are then reexported from | |
24 | // the prelude for convenience. So there are a lot of traits here. | |
25 | // | |
26 | // The layout of this file is thus: | |
27 | // | |
28 | // * Slice-specific 'extension' traits and their implementations. This | |
29 | // is where most of the slice API resides. | |
30 | // * Implementations of a few common traits with important slice ops. | |
31 | // * Definitions of a bunch of iterators. | |
32 | // * Free functions. | |
33 | // * The `raw` and `bytes` submodules. | |
34 | // * Boilerplate trait implementations. | |
35 | ||
9e0c209e | 36 | use cmp::Ordering::{self, Less, Equal, Greater}; |
1a4d82fc | 37 | use cmp; |
54a0048b | 38 | use fmt; |
c34b1796 | 39 | use intrinsics::assume; |
1a4d82fc | 40 | use iter::*; |
9e0c209e | 41 | use ops::{self, RangeFull}; |
1a4d82fc | 42 | use ptr; |
1a4d82fc | 43 | use mem; |
9e0c209e | 44 | use marker; |
3157f602 | 45 | use iter_private::TrustedRandomAccess; |
1a4d82fc | 46 | |
54a0048b SL |
47 | #[repr(C)] |
48 | struct Repr<T> { | |
49 | pub data: *const T, | |
50 | pub len: usize, | |
51 | } | |
1a4d82fc JJ |
52 | |
53 | // | |
54 | // Extension traits | |
55 | // | |
56 | ||
57 | /// Extension methods for slices. | |
62682a34 | 58 | #[unstable(feature = "core_slice_ext", |
e9174d1e | 59 | reason = "stable interface provided by `impl [T]` in later crates", |
54a0048b | 60 | issue = "32110")] |
92a42be0 | 61 | #[allow(missing_docs)] // documented elsewhere |
1a4d82fc JJ |
62 | pub trait SliceExt { |
63 | type Item; | |
64 | ||
92a42be0 | 65 | #[stable(feature = "core", since = "1.6.0")] |
e9174d1e | 66 | fn split_at(&self, mid: usize) -> (&[Self::Item], &[Self::Item]); |
92a42be0 | 67 | #[stable(feature = "core", since = "1.6.0")] |
e9174d1e | 68 | fn iter(&self) -> Iter<Self::Item>; |
92a42be0 | 69 | #[stable(feature = "core", since = "1.6.0")] |
e9174d1e | 70 | fn split<P>(&self, pred: P) -> Split<Self::Item, P> |
1a4d82fc | 71 | where P: FnMut(&Self::Item) -> bool; |
92a42be0 | 72 | #[stable(feature = "core", since = "1.6.0")] |
e9174d1e | 73 | fn splitn<P>(&self, n: usize, pred: P) -> SplitN<Self::Item, P> |
1a4d82fc | 74 | where P: FnMut(&Self::Item) -> bool; |
92a42be0 | 75 | #[stable(feature = "core", since = "1.6.0")] |
e9174d1e | 76 | fn rsplitn<P>(&self, n: usize, pred: P) -> RSplitN<Self::Item, P> |
1a4d82fc | 77 | where P: FnMut(&Self::Item) -> bool; |
92a42be0 | 78 | #[stable(feature = "core", since = "1.6.0")] |
e9174d1e | 79 | fn windows(&self, size: usize) -> Windows<Self::Item>; |
92a42be0 | 80 | #[stable(feature = "core", since = "1.6.0")] |
e9174d1e | 81 | fn chunks(&self, size: usize) -> Chunks<Self::Item>; |
92a42be0 | 82 | #[stable(feature = "core", since = "1.6.0")] |
e9174d1e | 83 | fn get(&self, index: usize) -> Option<&Self::Item>; |
92a42be0 | 84 | #[stable(feature = "core", since = "1.6.0")] |
e9174d1e | 85 | fn first(&self) -> Option<&Self::Item>; |
92a42be0 | 86 | #[stable(feature = "core", since = "1.6.0")] |
e9174d1e | 87 | fn split_first(&self) -> Option<(&Self::Item, &[Self::Item])>; |
92a42be0 | 88 | #[stable(feature = "core", since = "1.6.0")] |
e9174d1e | 89 | fn split_last(&self) -> Option<(&Self::Item, &[Self::Item])>; |
92a42be0 | 90 | #[stable(feature = "core", since = "1.6.0")] |
e9174d1e | 91 | fn last(&self) -> Option<&Self::Item>; |
92a42be0 | 92 | #[stable(feature = "core", since = "1.6.0")] |
e9174d1e | 93 | unsafe fn get_unchecked(&self, index: usize) -> &Self::Item; |
92a42be0 | 94 | #[stable(feature = "core", since = "1.6.0")] |
1a4d82fc | 95 | fn as_ptr(&self) -> *const Self::Item; |
92a42be0 SL |
96 | #[stable(feature = "core", since = "1.6.0")] |
97 | fn binary_search(&self, x: &Self::Item) -> Result<usize, usize> | |
98 | where Self::Item: Ord; | |
99 | #[stable(feature = "core", since = "1.6.0")] | |
5bcae85e SL |
100 | fn binary_search_by<'a, F>(&'a self, f: F) -> Result<usize, usize> |
101 | where F: FnMut(&'a Self::Item) -> Ordering; | |
a7813a04 | 102 | #[stable(feature = "slice_binary_search_by_key", since = "1.10.0")] |
5bcae85e SL |
103 | fn binary_search_by_key<'a, B, F>(&'a self, b: &B, f: F) -> Result<usize, usize> |
104 | where F: FnMut(&'a Self::Item) -> B, | |
a7813a04 | 105 | B: Ord; |
92a42be0 | 106 | #[stable(feature = "core", since = "1.6.0")] |
85aaf69f | 107 | fn len(&self) -> usize; |
92a42be0 | 108 | #[stable(feature = "core", since = "1.6.0")] |
1a4d82fc | 109 | fn is_empty(&self) -> bool { self.len() == 0 } |
92a42be0 | 110 | #[stable(feature = "core", since = "1.6.0")] |
e9174d1e | 111 | fn get_mut(&mut self, index: usize) -> Option<&mut Self::Item>; |
92a42be0 | 112 | #[stable(feature = "core", since = "1.6.0")] |
e9174d1e | 113 | fn iter_mut(&mut self) -> IterMut<Self::Item>; |
92a42be0 | 114 | #[stable(feature = "core", since = "1.6.0")] |
e9174d1e | 115 | fn first_mut(&mut self) -> Option<&mut Self::Item>; |
92a42be0 | 116 | #[stable(feature = "core", since = "1.6.0")] |
e9174d1e | 117 | fn split_first_mut(&mut self) -> Option<(&mut Self::Item, &mut [Self::Item])>; |
92a42be0 | 118 | #[stable(feature = "core", since = "1.6.0")] |
e9174d1e | 119 | fn split_last_mut(&mut self) -> Option<(&mut Self::Item, &mut [Self::Item])>; |
92a42be0 | 120 | #[stable(feature = "core", since = "1.6.0")] |
e9174d1e | 121 | fn last_mut(&mut self) -> Option<&mut Self::Item>; |
92a42be0 | 122 | #[stable(feature = "core", since = "1.6.0")] |
e9174d1e | 123 | fn split_mut<P>(&mut self, pred: P) -> SplitMut<Self::Item, P> |
1a4d82fc | 124 | where P: FnMut(&Self::Item) -> bool; |
92a42be0 | 125 | #[stable(feature = "core", since = "1.6.0")] |
85aaf69f | 126 | fn splitn_mut<P>(&mut self, n: usize, pred: P) -> SplitNMut<Self::Item, P> |
1a4d82fc | 127 | where P: FnMut(&Self::Item) -> bool; |
92a42be0 | 128 | #[stable(feature = "core", since = "1.6.0")] |
85aaf69f | 129 | fn rsplitn_mut<P>(&mut self, n: usize, pred: P) -> RSplitNMut<Self::Item, P> |
1a4d82fc | 130 | where P: FnMut(&Self::Item) -> bool; |
92a42be0 | 131 | #[stable(feature = "core", since = "1.6.0")] |
e9174d1e | 132 | fn chunks_mut(&mut self, chunk_size: usize) -> ChunksMut<Self::Item>; |
92a42be0 | 133 | #[stable(feature = "core", since = "1.6.0")] |
85aaf69f | 134 | fn swap(&mut self, a: usize, b: usize); |
92a42be0 | 135 | #[stable(feature = "core", since = "1.6.0")] |
e9174d1e | 136 | fn split_at_mut(&mut self, mid: usize) -> (&mut [Self::Item], &mut [Self::Item]); |
92a42be0 | 137 | #[stable(feature = "core", since = "1.6.0")] |
1a4d82fc | 138 | fn reverse(&mut self); |
92a42be0 | 139 | #[stable(feature = "core", since = "1.6.0")] |
e9174d1e | 140 | unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut Self::Item; |
92a42be0 | 141 | #[stable(feature = "core", since = "1.6.0")] |
1a4d82fc JJ |
142 | fn as_mut_ptr(&mut self) -> *mut Self::Item; |
143 | ||
92a42be0 | 144 | #[stable(feature = "core", since = "1.6.0")] |
1a4d82fc JJ |
145 | fn contains(&self, x: &Self::Item) -> bool where Self::Item: PartialEq; |
146 | ||
92a42be0 | 147 | #[stable(feature = "core", since = "1.6.0")] |
1a4d82fc JJ |
148 | fn starts_with(&self, needle: &[Self::Item]) -> bool where Self::Item: PartialEq; |
149 | ||
92a42be0 | 150 | #[stable(feature = "core", since = "1.6.0")] |
1a4d82fc JJ |
151 | fn ends_with(&self, needle: &[Self::Item]) -> bool where Self::Item: PartialEq; |
152 | ||
9cc50fc6 | 153 | #[stable(feature = "clone_from_slice", since = "1.7.0")] |
54a0048b SL |
154 | fn clone_from_slice(&mut self, src: &[Self::Item]) where Self::Item: Clone; |
155 | #[stable(feature = "copy_from_slice", since = "1.9.0")] | |
7453a54e | 156 | fn copy_from_slice(&mut self, src: &[Self::Item]) where Self::Item: Copy; |
1a4d82fc JJ |
157 | } |
158 | ||
62682a34 SL |
159 | // Use macros to be generic over const/mut |
160 | macro_rules! slice_offset { | |
161 | ($ptr:expr, $by:expr) => {{ | |
162 | let ptr = $ptr; | |
163 | if size_from_ptr(ptr) == 0 { | |
164 | ::intrinsics::arith_offset(ptr as *mut i8, $by) as *mut _ | |
165 | } else { | |
166 | ptr.offset($by) | |
167 | } | |
168 | }}; | |
169 | } | |
170 | ||
171 | macro_rules! slice_ref { | |
172 | ($ptr:expr) => {{ | |
173 | let ptr = $ptr; | |
174 | if size_from_ptr(ptr) == 0 { | |
175 | // Use a non-null pointer value | |
176 | &mut *(1 as *mut _) | |
177 | } else { | |
e9174d1e | 178 | mem::transmute(ptr) |
62682a34 SL |
179 | } |
180 | }}; | |
181 | } | |
182 | ||
92a42be0 SL |
183 | #[unstable(feature = "core_slice_ext", |
184 | reason = "stable interface provided by `impl [T]` in later crates", | |
54a0048b | 185 | issue = "32110")] |
1a4d82fc JJ |
186 | impl<T> SliceExt for [T] { |
187 | type Item = T; | |
188 | ||
189 | #[inline] | |
85aaf69f SL |
190 | fn split_at(&self, mid: usize) -> (&[T], &[T]) { |
191 | (&self[..mid], &self[mid..]) | |
1a4d82fc JJ |
192 | } |
193 | ||
194 | #[inline] | |
e9174d1e | 195 | fn iter(&self) -> Iter<T> { |
1a4d82fc | 196 | unsafe { |
62682a34 SL |
197 | let p = if mem::size_of::<T>() == 0 { |
198 | 1 as *const _ | |
1a4d82fc | 199 | } else { |
62682a34 SL |
200 | let p = self.as_ptr(); |
201 | assume(!p.is_null()); | |
202 | p | |
203 | }; | |
204 | ||
205 | Iter { | |
206 | ptr: p, | |
207 | end: slice_offset!(p, self.len() as isize), | |
208 | _marker: marker::PhantomData | |
1a4d82fc JJ |
209 | } |
210 | } | |
211 | } | |
212 | ||
213 | #[inline] | |
e9174d1e | 214 | fn split<P>(&self, pred: P) -> Split<T, P> where P: FnMut(&T) -> bool { |
1a4d82fc JJ |
215 | Split { |
216 | v: self, | |
217 | pred: pred, | |
218 | finished: false | |
219 | } | |
220 | } | |
221 | ||
222 | #[inline] | |
e9174d1e | 223 | fn splitn<P>(&self, n: usize, pred: P) -> SplitN<T, P> where |
1a4d82fc JJ |
224 | P: FnMut(&T) -> bool, |
225 | { | |
226 | SplitN { | |
227 | inner: GenericSplitN { | |
228 | iter: self.split(pred), | |
229 | count: n, | |
230 | invert: false | |
231 | } | |
232 | } | |
233 | } | |
234 | ||
235 | #[inline] | |
e9174d1e | 236 | fn rsplitn<P>(&self, n: usize, pred: P) -> RSplitN<T, P> where |
1a4d82fc JJ |
237 | P: FnMut(&T) -> bool, |
238 | { | |
239 | RSplitN { | |
240 | inner: GenericSplitN { | |
241 | iter: self.split(pred), | |
242 | count: n, | |
243 | invert: true | |
244 | } | |
245 | } | |
246 | } | |
247 | ||
248 | #[inline] | |
85aaf69f | 249 | fn windows(&self, size: usize) -> Windows<T> { |
1a4d82fc JJ |
250 | assert!(size != 0); |
251 | Windows { v: self, size: size } | |
252 | } | |
253 | ||
254 | #[inline] | |
85aaf69f | 255 | fn chunks(&self, size: usize) -> Chunks<T> { |
1a4d82fc JJ |
256 | assert!(size != 0); |
257 | Chunks { v: self, size: size } | |
258 | } | |
259 | ||
260 | #[inline] | |
85aaf69f | 261 | fn get(&self, index: usize) -> Option<&T> { |
1a4d82fc JJ |
262 | if index < self.len() { Some(&self[index]) } else { None } |
263 | } | |
264 | ||
265 | #[inline] | |
266 | fn first(&self) -> Option<&T> { | |
9346a6ac | 267 | if self.is_empty() { None } else { Some(&self[0]) } |
1a4d82fc JJ |
268 | } |
269 | ||
1a4d82fc | 270 | #[inline] |
c1a9b12d SL |
271 | fn split_first(&self) -> Option<(&T, &[T])> { |
272 | if self.is_empty() { None } else { Some((&self[0], &self[1..])) } | |
273 | } | |
274 | ||
c1a9b12d SL |
275 | #[inline] |
276 | fn split_last(&self) -> Option<(&T, &[T])> { | |
277 | let len = self.len(); | |
278 | if len == 0 { None } else { Some((&self[len - 1], &self[..(len - 1)])) } | |
1a4d82fc JJ |
279 | } |
280 | ||
281 | #[inline] | |
282 | fn last(&self) -> Option<&T> { | |
9346a6ac | 283 | if self.is_empty() { None } else { Some(&self[self.len() - 1]) } |
1a4d82fc JJ |
284 | } |
285 | ||
286 | #[inline] | |
85aaf69f | 287 | unsafe fn get_unchecked(&self, index: usize) -> &T { |
54a0048b | 288 | &*(self.as_ptr().offset(index as isize)) |
1a4d82fc JJ |
289 | } |
290 | ||
291 | #[inline] | |
292 | fn as_ptr(&self) -> *const T { | |
54a0048b | 293 | self as *const [T] as *const T |
1a4d82fc JJ |
294 | } |
295 | ||
5bcae85e SL |
296 | fn binary_search_by<'a, F>(&'a self, mut f: F) -> Result<usize, usize> |
297 | where F: FnMut(&'a T) -> Ordering | |
1a4d82fc | 298 | { |
7453a54e SL |
299 | let mut base = 0usize; |
300 | let mut s = self; | |
1a4d82fc | 301 | |
7453a54e SL |
302 | loop { |
303 | let (head, tail) = s.split_at(s.len() >> 1); | |
304 | if tail.is_empty() { | |
305 | return Err(base) | |
306 | } | |
307 | match f(&tail[0]) { | |
1a4d82fc | 308 | Less => { |
7453a54e SL |
309 | base += head.len() + 1; |
310 | s = &tail[1..]; | |
1a4d82fc | 311 | } |
7453a54e SL |
312 | Greater => s = head, |
313 | Equal => return Ok(base + head.len()), | |
1a4d82fc | 314 | } |
1a4d82fc | 315 | } |
1a4d82fc JJ |
316 | } |
317 | ||
318 | #[inline] | |
54a0048b SL |
319 | fn len(&self) -> usize { |
320 | unsafe { | |
321 | mem::transmute::<&[T], Repr<T>>(self).len | |
322 | } | |
323 | } | |
1a4d82fc JJ |
324 | |
325 | #[inline] | |
85aaf69f | 326 | fn get_mut(&mut self, index: usize) -> Option<&mut T> { |
1a4d82fc JJ |
327 | if index < self.len() { Some(&mut self[index]) } else { None } |
328 | } | |
329 | ||
1a4d82fc | 330 | #[inline] |
85aaf69f | 331 | fn split_at_mut(&mut self, mid: usize) -> (&mut [T], &mut [T]) { |
c1a9b12d SL |
332 | let len = self.len(); |
333 | let ptr = self.as_mut_ptr(); | |
e9174d1e | 334 | |
1a4d82fc | 335 | unsafe { |
e9174d1e SL |
336 | assert!(mid <= len); |
337 | ||
c1a9b12d SL |
338 | (from_raw_parts_mut(ptr, mid), |
339 | from_raw_parts_mut(ptr.offset(mid as isize), len - mid)) | |
1a4d82fc JJ |
340 | } |
341 | } | |
342 | ||
343 | #[inline] | |
e9174d1e | 344 | fn iter_mut(&mut self) -> IterMut<T> { |
1a4d82fc | 345 | unsafe { |
62682a34 SL |
346 | let p = if mem::size_of::<T>() == 0 { |
347 | 1 as *mut _ | |
1a4d82fc | 348 | } else { |
62682a34 SL |
349 | let p = self.as_mut_ptr(); |
350 | assume(!p.is_null()); | |
351 | p | |
352 | }; | |
353 | ||
354 | IterMut { | |
355 | ptr: p, | |
356 | end: slice_offset!(p, self.len() as isize), | |
357 | _marker: marker::PhantomData | |
1a4d82fc JJ |
358 | } |
359 | } | |
360 | } | |
361 | ||
362 | #[inline] | |
363 | fn last_mut(&mut self) -> Option<&mut T> { | |
364 | let len = self.len(); | |
365 | if len == 0 { return None; } | |
366 | Some(&mut self[len - 1]) | |
367 | } | |
368 | ||
369 | #[inline] | |
370 | fn first_mut(&mut self) -> Option<&mut T> { | |
9346a6ac | 371 | if self.is_empty() { None } else { Some(&mut self[0]) } |
1a4d82fc JJ |
372 | } |
373 | ||
c1a9b12d SL |
374 | #[inline] |
375 | fn split_first_mut(&mut self) -> Option<(&mut T, &mut [T])> { | |
376 | if self.is_empty() { None } else { | |
377 | let split = self.split_at_mut(1); | |
378 | Some((&mut split.0[0], split.1)) | |
379 | } | |
1a4d82fc JJ |
380 | } |
381 | ||
c1a9b12d SL |
382 | #[inline] |
383 | fn split_last_mut(&mut self) -> Option<(&mut T, &mut [T])> { | |
384 | let len = self.len(); | |
385 | if len == 0 { None } else { | |
386 | let split = self.split_at_mut(len - 1); | |
387 | Some((&mut split.1[0], split.0)) | |
388 | } | |
389 | } | |
390 | ||
1a4d82fc | 391 | #[inline] |
e9174d1e | 392 | fn split_mut<P>(&mut self, pred: P) -> SplitMut<T, P> where P: FnMut(&T) -> bool { |
1a4d82fc JJ |
393 | SplitMut { v: self, pred: pred, finished: false } |
394 | } | |
395 | ||
396 | #[inline] | |
e9174d1e | 397 | fn splitn_mut<P>(&mut self, n: usize, pred: P) -> SplitNMut<T, P> where |
1a4d82fc JJ |
398 | P: FnMut(&T) -> bool |
399 | { | |
400 | SplitNMut { | |
401 | inner: GenericSplitN { | |
402 | iter: self.split_mut(pred), | |
403 | count: n, | |
404 | invert: false | |
405 | } | |
406 | } | |
407 | } | |
408 | ||
409 | #[inline] | |
e9174d1e | 410 | fn rsplitn_mut<P>(&mut self, n: usize, pred: P) -> RSplitNMut<T, P> where |
1a4d82fc JJ |
411 | P: FnMut(&T) -> bool, |
412 | { | |
413 | RSplitNMut { | |
414 | inner: GenericSplitN { | |
415 | iter: self.split_mut(pred), | |
416 | count: n, | |
417 | invert: true | |
418 | } | |
419 | } | |
420 | } | |
421 | ||
422 | #[inline] | |
85aaf69f | 423 | fn chunks_mut(&mut self, chunk_size: usize) -> ChunksMut<T> { |
1a4d82fc JJ |
424 | assert!(chunk_size > 0); |
425 | ChunksMut { v: self, chunk_size: chunk_size } | |
426 | } | |
427 | ||
c34b1796 | 428 | #[inline] |
85aaf69f | 429 | fn swap(&mut self, a: usize, b: usize) { |
1a4d82fc JJ |
430 | unsafe { |
431 | // Can't take two mutable loans from one vector, so instead just cast | |
432 | // them to their raw pointers to do the swap | |
433 | let pa: *mut T = &mut self[a]; | |
434 | let pb: *mut T = &mut self[b]; | |
435 | ptr::swap(pa, pb); | |
436 | } | |
437 | } | |
438 | ||
439 | fn reverse(&mut self) { | |
85aaf69f | 440 | let mut i: usize = 0; |
1a4d82fc JJ |
441 | let ln = self.len(); |
442 | while i < ln / 2 { | |
443 | // Unsafe swap to avoid the bounds check in safe swap. | |
444 | unsafe { | |
445 | let pa: *mut T = self.get_unchecked_mut(i); | |
446 | let pb: *mut T = self.get_unchecked_mut(ln - i - 1); | |
447 | ptr::swap(pa, pb); | |
448 | } | |
449 | i += 1; | |
450 | } | |
451 | } | |
452 | ||
453 | #[inline] | |
85aaf69f | 454 | unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T { |
54a0048b | 455 | &mut *self.as_mut_ptr().offset(index as isize) |
1a4d82fc JJ |
456 | } |
457 | ||
458 | #[inline] | |
459 | fn as_mut_ptr(&mut self) -> *mut T { | |
54a0048b | 460 | self as *mut [T] as *mut T |
1a4d82fc JJ |
461 | } |
462 | ||
1a4d82fc JJ |
463 | #[inline] |
464 | fn contains(&self, x: &T) -> bool where T: PartialEq { | |
465 | self.iter().any(|elt| *x == *elt) | |
466 | } | |
467 | ||
468 | #[inline] | |
469 | fn starts_with(&self, needle: &[T]) -> bool where T: PartialEq { | |
470 | let n = needle.len(); | |
85aaf69f | 471 | self.len() >= n && needle == &self[..n] |
1a4d82fc JJ |
472 | } |
473 | ||
474 | #[inline] | |
475 | fn ends_with(&self, needle: &[T]) -> bool where T: PartialEq { | |
476 | let (m, n) = (self.len(), needle.len()); | |
85aaf69f | 477 | m >= n && needle == &self[m-n..] |
1a4d82fc JJ |
478 | } |
479 | ||
85aaf69f | 480 | fn binary_search(&self, x: &T) -> Result<usize, usize> where T: Ord { |
1a4d82fc JJ |
481 | self.binary_search_by(|p| p.cmp(x)) |
482 | } | |
483 | ||
1a4d82fc | 484 | #[inline] |
9cc50fc6 SL |
485 | fn clone_from_slice(&mut self, src: &[T]) where T: Clone { |
486 | assert!(self.len() == src.len(), | |
487 | "destination and source slices have different lengths"); | |
488 | // NOTE: We need to explicitly slice them to the same length | |
489 | // for bounds checking to be elided, and the optimizer will | |
490 | // generate memcpy for simple cases (for example T = u8). | |
491 | let len = self.len(); | |
492 | let src = &src[..len]; | |
493 | for i in 0..len { | |
494 | self[i].clone_from(&src[i]); | |
1a4d82fc | 495 | } |
1a4d82fc | 496 | } |
7453a54e SL |
497 | |
498 | #[inline] | |
499 | fn copy_from_slice(&mut self, src: &[T]) where T: Copy { | |
500 | assert!(self.len() == src.len(), | |
501 | "destination and source slices have different lengths"); | |
502 | unsafe { | |
503 | ptr::copy_nonoverlapping( | |
504 | src.as_ptr(), self.as_mut_ptr(), self.len()); | |
505 | } | |
506 | } | |
a7813a04 XL |
507 | |
508 | #[inline] | |
5bcae85e SL |
509 | fn binary_search_by_key<'a, B, F>(&'a self, b: &B, mut f: F) -> Result<usize, usize> |
510 | where F: FnMut(&'a Self::Item) -> B, | |
a7813a04 XL |
511 | B: Ord |
512 | { | |
513 | self.binary_search_by(|k| f(k).cmp(b)) | |
514 | } | |
1a4d82fc JJ |
515 | } |
516 | ||
85aaf69f | 517 | #[stable(feature = "rust1", since = "1.0.0")] |
a7813a04 | 518 | #[rustc_on_unimplemented = "slice indices are of type `usize`"] |
85aaf69f | 519 | impl<T> ops::Index<usize> for [T] { |
1a4d82fc JJ |
520 | type Output = T; |
521 | ||
c34b1796 | 522 | fn index(&self, index: usize) -> &T { |
9e0c209e SL |
523 | // NB built-in indexing |
524 | &(*self)[index] | |
1a4d82fc JJ |
525 | } |
526 | } | |
527 | ||
85aaf69f | 528 | #[stable(feature = "rust1", since = "1.0.0")] |
a7813a04 | 529 | #[rustc_on_unimplemented = "slice indices are of type `usize`"] |
85aaf69f | 530 | impl<T> ops::IndexMut<usize> for [T] { |
c34b1796 AL |
531 | #[inline] |
532 | fn index_mut(&mut self, index: usize) -> &mut T { | |
9e0c209e SL |
533 | // NB built-in indexing |
534 | &mut (*self)[index] | |
1a4d82fc JJ |
535 | } |
536 | } | |
537 | ||
92a42be0 SL |
538 | #[inline(never)] |
539 | #[cold] | |
540 | fn slice_index_len_fail(index: usize, len: usize) -> ! { | |
541 | panic!("index {} out of range for slice of length {}", index, len); | |
542 | } | |
543 | ||
544 | #[inline(never)] | |
545 | #[cold] | |
546 | fn slice_index_order_fail(index: usize, end: usize) -> ! { | |
547 | panic!("slice index starts at {} but ends at {}", index, end); | |
548 | } | |
549 | ||
54a0048b SL |
550 | |
551 | /// Implements slicing with syntax `&self[begin .. end]`. | |
552 | /// | |
553 | /// Returns a slice of self for the index range [`begin`..`end`). | |
554 | /// | |
555 | /// This operation is `O(1)`. | |
556 | /// | |
557 | /// # Panics | |
558 | /// | |
559 | /// Requires that `begin <= end` and `end <= self.len()`, | |
560 | /// otherwise slicing will panic. | |
85aaf69f | 561 | #[stable(feature = "rust1", since = "1.0.0")] |
a7813a04 | 562 | #[rustc_on_unimplemented = "slice indices are of type `usize`"] |
85aaf69f | 563 | impl<T> ops::Index<ops::Range<usize>> for [T] { |
1a4d82fc | 564 | type Output = [T]; |
c34b1796 | 565 | |
1a4d82fc | 566 | #[inline] |
c34b1796 | 567 | fn index(&self, index: ops::Range<usize>) -> &[T] { |
92a42be0 SL |
568 | if index.start > index.end { |
569 | slice_index_order_fail(index.start, index.end); | |
570 | } else if index.end > self.len() { | |
571 | slice_index_len_fail(index.end, self.len()); | |
572 | } | |
1a4d82fc | 573 | unsafe { |
c34b1796 AL |
574 | from_raw_parts ( |
575 | self.as_ptr().offset(index.start as isize), | |
576 | index.end - index.start | |
577 | ) | |
1a4d82fc JJ |
578 | } |
579 | } | |
580 | } | |
54a0048b SL |
581 | |
582 | /// Implements slicing with syntax `&self[.. end]`. | |
583 | /// | |
584 | /// Returns a slice of self from the beginning until but not including | |
585 | /// the index `end`. | |
586 | /// | |
587 | /// Equivalent to `&self[0 .. end]` | |
85aaf69f | 588 | #[stable(feature = "rust1", since = "1.0.0")] |
a7813a04 | 589 | #[rustc_on_unimplemented = "slice indices are of type `usize`"] |
85aaf69f | 590 | impl<T> ops::Index<ops::RangeTo<usize>> for [T] { |
1a4d82fc | 591 | type Output = [T]; |
c34b1796 | 592 | |
1a4d82fc | 593 | #[inline] |
c34b1796 | 594 | fn index(&self, index: ops::RangeTo<usize>) -> &[T] { |
54a0048b | 595 | self.index(0 .. index.end) |
1a4d82fc JJ |
596 | } |
597 | } | |
54a0048b SL |
598 | |
599 | /// Implements slicing with syntax `&self[begin ..]`. | |
600 | /// | |
601 | /// Returns a slice of self from and including the index `begin` until the end. | |
602 | /// | |
603 | /// Equivalent to `&self[begin .. self.len()]` | |
85aaf69f | 604 | #[stable(feature = "rust1", since = "1.0.0")] |
a7813a04 | 605 | #[rustc_on_unimplemented = "slice indices are of type `usize`"] |
85aaf69f | 606 | impl<T> ops::Index<ops::RangeFrom<usize>> for [T] { |
1a4d82fc | 607 | type Output = [T]; |
c34b1796 | 608 | |
1a4d82fc | 609 | #[inline] |
c34b1796 | 610 | fn index(&self, index: ops::RangeFrom<usize>) -> &[T] { |
54a0048b | 611 | self.index(index.start .. self.len()) |
1a4d82fc JJ |
612 | } |
613 | } | |
54a0048b SL |
614 | |
615 | /// Implements slicing with syntax `&self[..]`. | |
616 | /// | |
3157f602 | 617 | /// Returns a slice of the whole slice. This operation cannot panic. |
54a0048b SL |
618 | /// |
619 | /// Equivalent to `&self[0 .. self.len()]` | |
85aaf69f SL |
620 | #[stable(feature = "rust1", since = "1.0.0")] |
621 | impl<T> ops::Index<RangeFull> for [T] { | |
1a4d82fc | 622 | type Output = [T]; |
c34b1796 | 623 | |
1a4d82fc | 624 | #[inline] |
c34b1796 | 625 | fn index(&self, _index: RangeFull) -> &[T] { |
1a4d82fc JJ |
626 | self |
627 | } | |
628 | } | |
629 | ||
54a0048b | 630 | #[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")] |
a7813a04 | 631 | #[rustc_on_unimplemented = "slice indices are of type `usize`"] |
54a0048b SL |
632 | impl<T> ops::Index<ops::RangeInclusive<usize>> for [T] { |
633 | type Output = [T]; | |
634 | ||
635 | #[inline] | |
636 | fn index(&self, index: ops::RangeInclusive<usize>) -> &[T] { | |
637 | match index { | |
638 | ops::RangeInclusive::Empty { .. } => &[], | |
639 | ops::RangeInclusive::NonEmpty { end, .. } if end == usize::max_value() => | |
640 | panic!("attempted to index slice up to maximum usize"), | |
641 | ops::RangeInclusive::NonEmpty { start, end } => | |
642 | self.index(start .. end+1) | |
643 | } | |
644 | } | |
645 | } | |
646 | #[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")] | |
a7813a04 | 647 | #[rustc_on_unimplemented = "slice indices are of type `usize`"] |
54a0048b SL |
648 | impl<T> ops::Index<ops::RangeToInclusive<usize>> for [T] { |
649 | type Output = [T]; | |
650 | ||
651 | #[inline] | |
652 | fn index(&self, index: ops::RangeToInclusive<usize>) -> &[T] { | |
653 | self.index(0...index.end) | |
654 | } | |
655 | } | |
656 | ||
657 | /// Implements mutable slicing with syntax `&mut self[begin .. end]`. | |
658 | /// | |
659 | /// Returns a slice of self for the index range [`begin`..`end`). | |
660 | /// | |
661 | /// This operation is `O(1)`. | |
662 | /// | |
663 | /// # Panics | |
664 | /// | |
665 | /// Requires that `begin <= end` and `end <= self.len()`, | |
666 | /// otherwise slicing will panic. | |
85aaf69f | 667 | #[stable(feature = "rust1", since = "1.0.0")] |
a7813a04 | 668 | #[rustc_on_unimplemented = "slice indices are of type `usize`"] |
85aaf69f | 669 | impl<T> ops::IndexMut<ops::Range<usize>> for [T] { |
1a4d82fc | 670 | #[inline] |
c34b1796 | 671 | fn index_mut(&mut self, index: ops::Range<usize>) -> &mut [T] { |
92a42be0 SL |
672 | if index.start > index.end { |
673 | slice_index_order_fail(index.start, index.end); | |
674 | } else if index.end > self.len() { | |
675 | slice_index_len_fail(index.end, self.len()); | |
676 | } | |
1a4d82fc | 677 | unsafe { |
c34b1796 AL |
678 | from_raw_parts_mut( |
679 | self.as_mut_ptr().offset(index.start as isize), | |
680 | index.end - index.start | |
681 | ) | |
1a4d82fc JJ |
682 | } |
683 | } | |
684 | } | |
54a0048b SL |
685 | |
686 | /// Implements mutable slicing with syntax `&mut self[.. end]`. | |
687 | /// | |
688 | /// Returns a slice of self from the beginning until but not including | |
689 | /// the index `end`. | |
690 | /// | |
691 | /// Equivalent to `&mut self[0 .. end]` | |
85aaf69f | 692 | #[stable(feature = "rust1", since = "1.0.0")] |
a7813a04 | 693 | #[rustc_on_unimplemented = "slice indices are of type `usize`"] |
85aaf69f | 694 | impl<T> ops::IndexMut<ops::RangeTo<usize>> for [T] { |
1a4d82fc | 695 | #[inline] |
c34b1796 | 696 | fn index_mut(&mut self, index: ops::RangeTo<usize>) -> &mut [T] { |
54a0048b | 697 | self.index_mut(0 .. index.end) |
1a4d82fc JJ |
698 | } |
699 | } | |
54a0048b SL |
700 | |
701 | /// Implements mutable slicing with syntax `&mut self[begin ..]`. | |
702 | /// | |
703 | /// Returns a slice of self from and including the index `begin` until the end. | |
704 | /// | |
705 | /// Equivalent to `&mut self[begin .. self.len()]` | |
85aaf69f | 706 | #[stable(feature = "rust1", since = "1.0.0")] |
a7813a04 | 707 | #[rustc_on_unimplemented = "slice indices are of type `usize`"] |
85aaf69f | 708 | impl<T> ops::IndexMut<ops::RangeFrom<usize>> for [T] { |
1a4d82fc | 709 | #[inline] |
c34b1796 | 710 | fn index_mut(&mut self, index: ops::RangeFrom<usize>) -> &mut [T] { |
1a4d82fc | 711 | let len = self.len(); |
54a0048b | 712 | self.index_mut(index.start .. len) |
1a4d82fc JJ |
713 | } |
714 | } | |
54a0048b SL |
715 | |
716 | /// Implements mutable slicing with syntax `&mut self[..]`. | |
717 | /// | |
718 | /// Returns a slice of the whole slice. This operation can not panic. | |
719 | /// | |
720 | /// Equivalent to `&mut self[0 .. self.len()]` | |
85aaf69f SL |
721 | #[stable(feature = "rust1", since = "1.0.0")] |
722 | impl<T> ops::IndexMut<RangeFull> for [T] { | |
1a4d82fc | 723 | #[inline] |
c34b1796 | 724 | fn index_mut(&mut self, _index: RangeFull) -> &mut [T] { |
1a4d82fc JJ |
725 | self |
726 | } | |
727 | } | |
728 | ||
54a0048b | 729 | #[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")] |
a7813a04 | 730 | #[rustc_on_unimplemented = "slice indices are of type `usize`"] |
54a0048b SL |
731 | impl<T> ops::IndexMut<ops::RangeInclusive<usize>> for [T] { |
732 | #[inline] | |
733 | fn index_mut(&mut self, index: ops::RangeInclusive<usize>) -> &mut [T] { | |
734 | match index { | |
735 | ops::RangeInclusive::Empty { .. } => &mut [], | |
736 | ops::RangeInclusive::NonEmpty { end, .. } if end == usize::max_value() => | |
737 | panic!("attempted to index slice up to maximum usize"), | |
738 | ops::RangeInclusive::NonEmpty { start, end } => | |
739 | self.index_mut(start .. end+1) | |
740 | } | |
741 | } | |
742 | } | |
743 | #[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")] | |
a7813a04 | 744 | #[rustc_on_unimplemented = "slice indices are of type `usize`"] |
54a0048b SL |
745 | impl<T> ops::IndexMut<ops::RangeToInclusive<usize>> for [T] { |
746 | #[inline] | |
747 | fn index_mut(&mut self, index: ops::RangeToInclusive<usize>) -> &mut [T] { | |
748 | self.index_mut(0...index.end) | |
749 | } | |
750 | } | |
1a4d82fc JJ |
751 | |
752 | //////////////////////////////////////////////////////////////////////////////// | |
753 | // Common traits | |
754 | //////////////////////////////////////////////////////////////////////////////// | |
755 | ||
85aaf69f | 756 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 757 | impl<'a, T> Default for &'a [T] { |
9e0c209e | 758 | /// Creates an empty slice. |
1a4d82fc JJ |
759 | fn default() -> &'a [T] { &[] } |
760 | } | |
761 | ||
b039eaaf SL |
762 | #[stable(feature = "mut_slice_default", since = "1.5.0")] |
763 | impl<'a, T> Default for &'a mut [T] { | |
9e0c209e | 764 | /// Creates a mutable empty slice. |
b039eaaf SL |
765 | fn default() -> &'a mut [T] { &mut [] } |
766 | } | |
767 | ||
1a4d82fc JJ |
768 | // |
769 | // Iterators | |
770 | // | |
771 | ||
85aaf69f SL |
772 | #[stable(feature = "rust1", since = "1.0.0")] |
773 | impl<'a, T> IntoIterator for &'a [T] { | |
774 | type Item = &'a T; | |
775 | type IntoIter = Iter<'a, T>; | |
776 | ||
777 | fn into_iter(self) -> Iter<'a, T> { | |
778 | self.iter() | |
779 | } | |
780 | } | |
781 | ||
782 | #[stable(feature = "rust1", since = "1.0.0")] | |
783 | impl<'a, T> IntoIterator for &'a mut [T] { | |
784 | type Item = &'a mut T; | |
785 | type IntoIter = IterMut<'a, T>; | |
786 | ||
787 | fn into_iter(self) -> IterMut<'a, T> { | |
788 | self.iter_mut() | |
789 | } | |
790 | } | |
791 | ||
d9579d0f AL |
792 | #[inline(always)] |
793 | fn size_from_ptr<T>(_: *const T) -> usize { | |
794 | mem::size_of::<T>() | |
795 | } | |
796 | ||
1a4d82fc JJ |
797 | // The shared definition of the `Iter` and `IterMut` iterators |
798 | macro_rules! iterator { | |
799 | (struct $name:ident -> $ptr:ty, $elem:ty) => { | |
85aaf69f | 800 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
801 | impl<'a, T> Iterator for $name<'a, T> { |
802 | type Item = $elem; | |
803 | ||
804 | #[inline] | |
805 | fn next(&mut self) -> Option<$elem> { | |
806 | // could be implemented with slices, but this avoids bounds checks | |
807 | unsafe { | |
d9579d0f AL |
808 | if mem::size_of::<T>() != 0 { |
809 | assume(!self.ptr.is_null()); | |
810 | assume(!self.end.is_null()); | |
811 | } | |
1a4d82fc JJ |
812 | if self.ptr == self.end { |
813 | None | |
814 | } else { | |
d9579d0f AL |
815 | let old = self.ptr; |
816 | self.ptr = slice_offset!(self.ptr, 1); | |
817 | Some(slice_ref!(old)) | |
1a4d82fc JJ |
818 | } |
819 | } | |
820 | } | |
821 | ||
822 | #[inline] | |
85aaf69f | 823 | fn size_hint(&self) -> (usize, Option<usize>) { |
d9579d0f | 824 | let diff = (self.end as usize).wrapping_sub(self.ptr as usize); |
1a4d82fc JJ |
825 | let size = mem::size_of::<T>(); |
826 | let exact = diff / (if size == 0 {1} else {size}); | |
827 | (exact, Some(exact)) | |
828 | } | |
d9579d0f AL |
829 | |
830 | #[inline] | |
831 | fn count(self) -> usize { | |
3157f602 | 832 | self.len() |
d9579d0f AL |
833 | } |
834 | ||
835 | #[inline] | |
836 | fn nth(&mut self, n: usize) -> Option<$elem> { | |
837 | // Call helper method. Can't put the definition here because mut versus const. | |
838 | self.iter_nth(n) | |
839 | } | |
840 | ||
841 | #[inline] | |
842 | fn last(mut self) -> Option<$elem> { | |
843 | self.next_back() | |
844 | } | |
1a4d82fc JJ |
845 | } |
846 | ||
85aaf69f | 847 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
848 | impl<'a, T> DoubleEndedIterator for $name<'a, T> { |
849 | #[inline] | |
850 | fn next_back(&mut self) -> Option<$elem> { | |
851 | // could be implemented with slices, but this avoids bounds checks | |
852 | unsafe { | |
d9579d0f AL |
853 | if mem::size_of::<T>() != 0 { |
854 | assume(!self.ptr.is_null()); | |
855 | assume(!self.end.is_null()); | |
856 | } | |
1a4d82fc JJ |
857 | if self.end == self.ptr { |
858 | None | |
859 | } else { | |
d9579d0f AL |
860 | self.end = slice_offset!(self.end, -1); |
861 | Some(slice_ref!(self.end)) | |
1a4d82fc JJ |
862 | } |
863 | } | |
864 | } | |
865 | } | |
866 | } | |
867 | } | |
868 | ||
869 | macro_rules! make_slice { | |
d9579d0f AL |
870 | ($start: expr, $end: expr) => {{ |
871 | let start = $start; | |
872 | let diff = ($end as usize).wrapping_sub(start as usize); | |
873 | if size_from_ptr(start) == 0 { | |
874 | // use a non-null pointer value | |
875 | unsafe { from_raw_parts(1 as *const _, diff) } | |
1a4d82fc | 876 | } else { |
d9579d0f AL |
877 | let len = diff / size_from_ptr(start); |
878 | unsafe { from_raw_parts(start, len) } | |
c34b1796 AL |
879 | } |
880 | }} | |
881 | } | |
882 | ||
883 | macro_rules! make_mut_slice { | |
d9579d0f AL |
884 | ($start: expr, $end: expr) => {{ |
885 | let start = $start; | |
886 | let diff = ($end as usize).wrapping_sub(start as usize); | |
887 | if size_from_ptr(start) == 0 { | |
888 | // use a non-null pointer value | |
889 | unsafe { from_raw_parts_mut(1 as *mut _, diff) } | |
c34b1796 | 890 | } else { |
d9579d0f AL |
891 | let len = diff / size_from_ptr(start); |
892 | unsafe { from_raw_parts_mut(start, len) } | |
1a4d82fc JJ |
893 | } |
894 | }} | |
895 | } | |
896 | ||
897 | /// Immutable slice iterator | |
a7813a04 | 898 | /// |
5bcae85e SL |
899 | /// This struct is created by the [`iter`] method on [slices]. |
900 | /// | |
a7813a04 XL |
901 | /// # Examples |
902 | /// | |
903 | /// Basic usage: | |
904 | /// | |
905 | /// ``` | |
906 | /// // First, we declare a type which has `iter` method to get the `Iter` struct (&[usize here]): | |
907 | /// let slice = &[1, 2, 3]; | |
908 | /// | |
909 | /// // Then, we iterate over it: | |
910 | /// for element in slice.iter() { | |
911 | /// println!("{}", element); | |
912 | /// } | |
913 | /// ``` | |
5bcae85e SL |
914 | /// |
915 | /// [`iter`]: ../../std/primitive.slice.html#method.iter | |
916 | /// [slices]: ../../std/primitive.slice.html | |
85aaf69f | 917 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
918 | pub struct Iter<'a, T: 'a> { |
919 | ptr: *const T, | |
920 | end: *const T, | |
85aaf69f | 921 | _marker: marker::PhantomData<&'a T>, |
1a4d82fc JJ |
922 | } |
923 | ||
54a0048b SL |
924 | #[stable(feature = "core_impl_debug", since = "1.9.0")] |
925 | impl<'a, T: 'a + fmt::Debug> fmt::Debug for Iter<'a, T> { | |
926 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
927 | f.debug_tuple("Iter") | |
928 | .field(&self.as_slice()) | |
929 | .finish() | |
930 | } | |
931 | } | |
932 | ||
92a42be0 | 933 | #[stable(feature = "rust1", since = "1.0.0")] |
c34b1796 | 934 | unsafe impl<'a, T: Sync> Sync for Iter<'a, T> {} |
92a42be0 | 935 | #[stable(feature = "rust1", since = "1.0.0")] |
c34b1796 AL |
936 | unsafe impl<'a, T: Sync> Send for Iter<'a, T> {} |
937 | ||
1a4d82fc JJ |
938 | impl<'a, T> Iter<'a, T> { |
939 | /// View the underlying data as a subslice of the original data. | |
940 | /// | |
941 | /// This has the same lifetime as the original slice, and so the | |
942 | /// iterator can continue to be used while this exists. | |
a7813a04 XL |
943 | /// |
944 | /// # Examples | |
945 | /// | |
946 | /// Basic usage: | |
947 | /// | |
948 | /// ``` | |
949 | /// // First, we declare a type which has the `iter` method to get the `Iter` | |
950 | /// // struct (&[usize here]): | |
951 | /// let slice = &[1, 2, 3]; | |
952 | /// | |
953 | /// // Then, we get the iterator: | |
954 | /// let mut iter = slice.iter(); | |
955 | /// // So if we print what `as_slice` method returns here, we have "[1, 2, 3]": | |
956 | /// println!("{:?}", iter.as_slice()); | |
957 | /// | |
958 | /// // Next, we move to the second element of the slice: | |
959 | /// iter.next(); | |
960 | /// // Now `as_slice` returns "[2, 3]": | |
961 | /// println!("{:?}", iter.as_slice()); | |
962 | /// ``` | |
e9174d1e | 963 | #[stable(feature = "iter_to_slice", since = "1.4.0")] |
1a4d82fc | 964 | pub fn as_slice(&self) -> &'a [T] { |
d9579d0f AL |
965 | make_slice!(self.ptr, self.end) |
966 | } | |
967 | ||
968 | // Helper function for Iter::nth | |
969 | fn iter_nth(&mut self, n: usize) -> Option<&'a T> { | |
970 | match self.as_slice().get(n) { | |
971 | Some(elem_ref) => unsafe { | |
972 | self.ptr = slice_offset!(self.ptr, (n as isize).wrapping_add(1)); | |
62682a34 | 973 | Some(elem_ref) |
d9579d0f AL |
974 | }, |
975 | None => { | |
976 | self.ptr = self.end; | |
977 | None | |
978 | } | |
979 | } | |
1a4d82fc JJ |
980 | } |
981 | } | |
982 | ||
1a4d82fc JJ |
983 | iterator!{struct Iter -> *const T, &'a T} |
984 | ||
85aaf69f | 985 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
986 | impl<'a, T> ExactSizeIterator for Iter<'a, T> {} |
987 | ||
9e0c209e SL |
988 | #[unstable(feature = "fused", issue = "35602")] |
989 | impl<'a, T> FusedIterator for Iter<'a, T> {} | |
990 | ||
85aaf69f | 991 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 992 | impl<'a, T> Clone for Iter<'a, T> { |
85aaf69f | 993 | fn clone(&self) -> Iter<'a, T> { Iter { ptr: self.ptr, end: self.end, _marker: self._marker } } |
1a4d82fc JJ |
994 | } |
995 | ||
9e0c209e SL |
996 | #[stable(feature = "slice_iter_as_ref", since = "1.12.0")] |
997 | impl<'a, T> AsRef<[T]> for Iter<'a, T> { | |
998 | fn as_ref(&self) -> &[T] { | |
999 | self.as_slice() | |
1000 | } | |
1001 | } | |
1002 | ||
1a4d82fc | 1003 | /// Mutable slice iterator. |
a7813a04 | 1004 | /// |
5bcae85e SL |
1005 | /// This struct is created by the [`iter_mut`] method on [slices]. |
1006 | /// | |
a7813a04 XL |
1007 | /// # Examples |
1008 | /// | |
1009 | /// Basic usage: | |
1010 | /// | |
1011 | /// ``` | |
1012 | /// // First, we declare a type which has `iter_mut` method to get the `IterMut` | |
1013 | /// // struct (&[usize here]): | |
1014 | /// let mut slice = &mut [1, 2, 3]; | |
1015 | /// | |
1016 | /// // Then, we iterate over it and increment each element value: | |
1017 | /// for element in slice.iter_mut() { | |
1018 | /// *element += 1; | |
1019 | /// } | |
1020 | /// | |
1021 | /// // We now have "[2, 3, 4]": | |
1022 | /// println!("{:?}", slice); | |
1023 | /// ``` | |
5bcae85e SL |
1024 | /// |
1025 | /// [`iter_mut`]: ../../std/primitive.slice.html#method.iter_mut | |
1026 | /// [slices]: ../../std/primitive.slice.html | |
85aaf69f | 1027 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
1028 | pub struct IterMut<'a, T: 'a> { |
1029 | ptr: *mut T, | |
1030 | end: *mut T, | |
85aaf69f | 1031 | _marker: marker::PhantomData<&'a mut T>, |
1a4d82fc JJ |
1032 | } |
1033 | ||
54a0048b SL |
1034 | #[stable(feature = "core_impl_debug", since = "1.9.0")] |
1035 | impl<'a, T: 'a + fmt::Debug> fmt::Debug for IterMut<'a, T> { | |
1036 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
1037 | f.debug_tuple("IterMut") | |
1038 | .field(&make_slice!(self.ptr, self.end)) | |
1039 | .finish() | |
1040 | } | |
1041 | } | |
1042 | ||
92a42be0 | 1043 | #[stable(feature = "rust1", since = "1.0.0")] |
c34b1796 | 1044 | unsafe impl<'a, T: Sync> Sync for IterMut<'a, T> {} |
92a42be0 | 1045 | #[stable(feature = "rust1", since = "1.0.0")] |
c34b1796 | 1046 | unsafe impl<'a, T: Send> Send for IterMut<'a, T> {} |
1a4d82fc | 1047 | |
1a4d82fc JJ |
1048 | impl<'a, T> IterMut<'a, T> { |
1049 | /// View the underlying data as a subslice of the original data. | |
1050 | /// | |
1051 | /// To avoid creating `&mut` references that alias, this is forced | |
1052 | /// to consume the iterator. Consider using the `Slice` and | |
1053 | /// `SliceMut` implementations for obtaining slices with more | |
1054 | /// restricted lifetimes that do not consume the iterator. | |
a7813a04 XL |
1055 | /// |
1056 | /// # Examples | |
1057 | /// | |
1058 | /// Basic usage: | |
1059 | /// | |
1060 | /// ``` | |
1061 | /// // First, we declare a type which has `iter_mut` method to get the `IterMut` | |
1062 | /// // struct (&[usize here]): | |
1063 | /// let mut slice = &mut [1, 2, 3]; | |
1064 | /// | |
1065 | /// { | |
1066 | /// // Then, we get the iterator: | |
1067 | /// let mut iter = slice.iter_mut(); | |
1068 | /// // We move to next element: | |
1069 | /// iter.next(); | |
1070 | /// // So if we print what `into_slice` method returns here, we have "[2, 3]": | |
1071 | /// println!("{:?}", iter.into_slice()); | |
1072 | /// } | |
1073 | /// | |
1074 | /// // Now let's modify a value of the slice: | |
1075 | /// { | |
1076 | /// // First we get back the iterator: | |
1077 | /// let mut iter = slice.iter_mut(); | |
1078 | /// // We change the value of the first element of the slice returned by the `next` method: | |
1079 | /// *iter.next().unwrap() += 1; | |
1080 | /// } | |
1081 | /// // Now slice is "[2, 2, 3]": | |
1082 | /// println!("{:?}", slice); | |
1083 | /// ``` | |
e9174d1e | 1084 | #[stable(feature = "iter_to_slice", since = "1.4.0")] |
1a4d82fc | 1085 | pub fn into_slice(self) -> &'a mut [T] { |
d9579d0f AL |
1086 | make_mut_slice!(self.ptr, self.end) |
1087 | } | |
1088 | ||
1089 | // Helper function for IterMut::nth | |
1090 | fn iter_nth(&mut self, n: usize) -> Option<&'a mut T> { | |
1091 | match make_mut_slice!(self.ptr, self.end).get_mut(n) { | |
1092 | Some(elem_ref) => unsafe { | |
1093 | self.ptr = slice_offset!(self.ptr, (n as isize).wrapping_add(1)); | |
62682a34 | 1094 | Some(elem_ref) |
d9579d0f AL |
1095 | }, |
1096 | None => { | |
1097 | self.ptr = self.end; | |
1098 | None | |
1099 | } | |
1100 | } | |
1a4d82fc JJ |
1101 | } |
1102 | } | |
1103 | ||
1104 | iterator!{struct IterMut -> *mut T, &'a mut T} | |
1105 | ||
85aaf69f | 1106 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
1107 | impl<'a, T> ExactSizeIterator for IterMut<'a, T> {} |
1108 | ||
9e0c209e SL |
1109 | #[unstable(feature = "fused", issue = "35602")] |
1110 | impl<'a, T> FusedIterator for IterMut<'a, T> {} | |
1111 | ||
1a4d82fc JJ |
1112 | /// An internal abstraction over the splitting iterators, so that |
1113 | /// splitn, splitn_mut etc can be implemented once. | |
54a0048b | 1114 | #[doc(hidden)] |
1a4d82fc JJ |
1115 | trait SplitIter: DoubleEndedIterator { |
1116 | /// Mark the underlying iterator as complete, extracting the remaining | |
1117 | /// portion of the slice. | |
1118 | fn finish(&mut self) -> Option<Self::Item>; | |
1119 | } | |
1120 | ||
1121 | /// An iterator over subslices separated by elements that match a predicate | |
1122 | /// function. | |
85aaf69f | 1123 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
1124 | pub struct Split<'a, T:'a, P> where P: FnMut(&T) -> bool { |
1125 | v: &'a [T], | |
1126 | pred: P, | |
1127 | finished: bool | |
1128 | } | |
1129 | ||
54a0048b SL |
1130 | #[stable(feature = "core_impl_debug", since = "1.9.0")] |
1131 | impl<'a, T: 'a + fmt::Debug, P> fmt::Debug for Split<'a, T, P> where P: FnMut(&T) -> bool { | |
1132 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
1133 | f.debug_struct("Split") | |
1134 | .field("v", &self.v) | |
1135 | .field("finished", &self.finished) | |
1136 | .finish() | |
1137 | } | |
1138 | } | |
1139 | ||
1a4d82fc | 1140 | // FIXME(#19839) Remove in favor of `#[derive(Clone)]` |
85aaf69f | 1141 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
1142 | impl<'a, T, P> Clone for Split<'a, T, P> where P: Clone + FnMut(&T) -> bool { |
1143 | fn clone(&self) -> Split<'a, T, P> { | |
1144 | Split { | |
1145 | v: self.v, | |
1146 | pred: self.pred.clone(), | |
1147 | finished: self.finished, | |
1148 | } | |
1149 | } | |
1150 | } | |
1151 | ||
85aaf69f | 1152 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
1153 | impl<'a, T, P> Iterator for Split<'a, T, P> where P: FnMut(&T) -> bool { |
1154 | type Item = &'a [T]; | |
1155 | ||
1156 | #[inline] | |
1157 | fn next(&mut self) -> Option<&'a [T]> { | |
1158 | if self.finished { return None; } | |
1159 | ||
1160 | match self.v.iter().position(|x| (self.pred)(x)) { | |
1161 | None => self.finish(), | |
1162 | Some(idx) => { | |
85aaf69f SL |
1163 | let ret = Some(&self.v[..idx]); |
1164 | self.v = &self.v[idx + 1..]; | |
1a4d82fc JJ |
1165 | ret |
1166 | } | |
1167 | } | |
1168 | } | |
1169 | ||
1170 | #[inline] | |
85aaf69f | 1171 | fn size_hint(&self) -> (usize, Option<usize>) { |
1a4d82fc JJ |
1172 | if self.finished { |
1173 | (0, Some(0)) | |
1174 | } else { | |
1175 | (1, Some(self.v.len() + 1)) | |
1176 | } | |
1177 | } | |
1178 | } | |
1179 | ||
85aaf69f | 1180 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
1181 | impl<'a, T, P> DoubleEndedIterator for Split<'a, T, P> where P: FnMut(&T) -> bool { |
1182 | #[inline] | |
1183 | fn next_back(&mut self) -> Option<&'a [T]> { | |
1184 | if self.finished { return None; } | |
1185 | ||
1186 | match self.v.iter().rposition(|x| (self.pred)(x)) { | |
1187 | None => self.finish(), | |
1188 | Some(idx) => { | |
85aaf69f SL |
1189 | let ret = Some(&self.v[idx + 1..]); |
1190 | self.v = &self.v[..idx]; | |
1a4d82fc JJ |
1191 | ret |
1192 | } | |
1193 | } | |
1194 | } | |
1195 | } | |
1196 | ||
1197 | impl<'a, T, P> SplitIter for Split<'a, T, P> where P: FnMut(&T) -> bool { | |
1198 | #[inline] | |
1199 | fn finish(&mut self) -> Option<&'a [T]> { | |
1200 | if self.finished { None } else { self.finished = true; Some(self.v) } | |
1201 | } | |
1202 | } | |
1203 | ||
9e0c209e SL |
1204 | #[unstable(feature = "fused", issue = "35602")] |
1205 | impl<'a, T, P> FusedIterator for Split<'a, T, P> where P: FnMut(&T) -> bool {} | |
1206 | ||
1a4d82fc JJ |
1207 | /// An iterator over the subslices of the vector which are separated |
1208 | /// by elements that match `pred`. | |
85aaf69f | 1209 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
1210 | pub struct SplitMut<'a, T:'a, P> where P: FnMut(&T) -> bool { |
1211 | v: &'a mut [T], | |
1212 | pred: P, | |
1213 | finished: bool | |
1214 | } | |
1215 | ||
54a0048b SL |
1216 | #[stable(feature = "core_impl_debug", since = "1.9.0")] |
1217 | impl<'a, T: 'a + fmt::Debug, P> fmt::Debug for SplitMut<'a, T, P> where P: FnMut(&T) -> bool { | |
1218 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
1219 | f.debug_struct("SplitMut") | |
1220 | .field("v", &self.v) | |
1221 | .field("finished", &self.finished) | |
1222 | .finish() | |
1223 | } | |
1224 | } | |
1225 | ||
1a4d82fc JJ |
1226 | impl<'a, T, P> SplitIter for SplitMut<'a, T, P> where P: FnMut(&T) -> bool { |
1227 | #[inline] | |
1228 | fn finish(&mut self) -> Option<&'a mut [T]> { | |
1229 | if self.finished { | |
1230 | None | |
1231 | } else { | |
1232 | self.finished = true; | |
1233 | Some(mem::replace(&mut self.v, &mut [])) | |
1234 | } | |
1235 | } | |
1236 | } | |
1237 | ||
85aaf69f | 1238 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
1239 | impl<'a, T, P> Iterator for SplitMut<'a, T, P> where P: FnMut(&T) -> bool { |
1240 | type Item = &'a mut [T]; | |
1241 | ||
1242 | #[inline] | |
1243 | fn next(&mut self) -> Option<&'a mut [T]> { | |
1244 | if self.finished { return None; } | |
1245 | ||
1246 | let idx_opt = { // work around borrowck limitations | |
1247 | let pred = &mut self.pred; | |
1248 | self.v.iter().position(|x| (*pred)(x)) | |
1249 | }; | |
1250 | match idx_opt { | |
1251 | None => self.finish(), | |
1252 | Some(idx) => { | |
1253 | let tmp = mem::replace(&mut self.v, &mut []); | |
1254 | let (head, tail) = tmp.split_at_mut(idx); | |
85aaf69f | 1255 | self.v = &mut tail[1..]; |
1a4d82fc JJ |
1256 | Some(head) |
1257 | } | |
1258 | } | |
1259 | } | |
1260 | ||
1261 | #[inline] | |
85aaf69f | 1262 | fn size_hint(&self) -> (usize, Option<usize>) { |
1a4d82fc JJ |
1263 | if self.finished { |
1264 | (0, Some(0)) | |
1265 | } else { | |
1266 | // if the predicate doesn't match anything, we yield one slice | |
1267 | // if it matches every element, we yield len+1 empty slices. | |
1268 | (1, Some(self.v.len() + 1)) | |
1269 | } | |
1270 | } | |
1271 | } | |
1272 | ||
85aaf69f | 1273 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
1274 | impl<'a, T, P> DoubleEndedIterator for SplitMut<'a, T, P> where |
1275 | P: FnMut(&T) -> bool, | |
1276 | { | |
1277 | #[inline] | |
1278 | fn next_back(&mut self) -> Option<&'a mut [T]> { | |
1279 | if self.finished { return None; } | |
1280 | ||
1281 | let idx_opt = { // work around borrowck limitations | |
1282 | let pred = &mut self.pred; | |
1283 | self.v.iter().rposition(|x| (*pred)(x)) | |
1284 | }; | |
1285 | match idx_opt { | |
1286 | None => self.finish(), | |
1287 | Some(idx) => { | |
1288 | let tmp = mem::replace(&mut self.v, &mut []); | |
1289 | let (head, tail) = tmp.split_at_mut(idx); | |
1290 | self.v = head; | |
85aaf69f | 1291 | Some(&mut tail[1..]) |
1a4d82fc JJ |
1292 | } |
1293 | } | |
1294 | } | |
1295 | } | |
1296 | ||
9e0c209e SL |
1297 | #[unstable(feature = "fused", issue = "35602")] |
1298 | impl<'a, T, P> FusedIterator for SplitMut<'a, T, P> where P: FnMut(&T) -> bool {} | |
1299 | ||
1a4d82fc JJ |
1300 | /// An private iterator over subslices separated by elements that |
1301 | /// match a predicate function, splitting at most a fixed number of | |
1302 | /// times. | |
54a0048b | 1303 | #[derive(Debug)] |
1a4d82fc JJ |
1304 | struct GenericSplitN<I> { |
1305 | iter: I, | |
85aaf69f | 1306 | count: usize, |
1a4d82fc JJ |
1307 | invert: bool |
1308 | } | |
1309 | ||
1310 | impl<T, I: SplitIter<Item=T>> Iterator for GenericSplitN<I> { | |
1311 | type Item = T; | |
1312 | ||
1313 | #[inline] | |
1314 | fn next(&mut self) -> Option<T> { | |
c34b1796 AL |
1315 | match self.count { |
1316 | 0 => None, | |
1317 | 1 => { self.count -= 1; self.iter.finish() } | |
1318 | _ => { | |
1319 | self.count -= 1; | |
1320 | if self.invert {self.iter.next_back()} else {self.iter.next()} | |
1321 | } | |
1a4d82fc JJ |
1322 | } |
1323 | } | |
1324 | ||
1325 | #[inline] | |
85aaf69f | 1326 | fn size_hint(&self) -> (usize, Option<usize>) { |
1a4d82fc | 1327 | let (lower, upper_opt) = self.iter.size_hint(); |
c34b1796 | 1328 | (lower, upper_opt.map(|upper| cmp::min(self.count, upper))) |
1a4d82fc JJ |
1329 | } |
1330 | } | |
1331 | ||
1332 | /// An iterator over subslices separated by elements that match a predicate | |
1333 | /// function, limited to a given number of splits. | |
85aaf69f | 1334 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
1335 | pub struct SplitN<'a, T: 'a, P> where P: FnMut(&T) -> bool { |
1336 | inner: GenericSplitN<Split<'a, T, P>> | |
1337 | } | |
1338 | ||
54a0048b SL |
1339 | #[stable(feature = "core_impl_debug", since = "1.9.0")] |
1340 | impl<'a, T: 'a + fmt::Debug, P> fmt::Debug for SplitN<'a, T, P> where P: FnMut(&T) -> bool { | |
1341 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
1342 | f.debug_struct("SplitN") | |
1343 | .field("inner", &self.inner) | |
1344 | .finish() | |
1345 | } | |
1346 | } | |
1347 | ||
1a4d82fc JJ |
1348 | /// An iterator over subslices separated by elements that match a |
1349 | /// predicate function, limited to a given number of splits, starting | |
1350 | /// from the end of the slice. | |
85aaf69f | 1351 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
1352 | pub struct RSplitN<'a, T: 'a, P> where P: FnMut(&T) -> bool { |
1353 | inner: GenericSplitN<Split<'a, T, P>> | |
1354 | } | |
1355 | ||
54a0048b SL |
1356 | #[stable(feature = "core_impl_debug", since = "1.9.0")] |
1357 | impl<'a, T: 'a + fmt::Debug, P> fmt::Debug for RSplitN<'a, T, P> where P: FnMut(&T) -> bool { | |
1358 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
1359 | f.debug_struct("RSplitN") | |
1360 | .field("inner", &self.inner) | |
1361 | .finish() | |
1362 | } | |
1363 | } | |
1364 | ||
1a4d82fc JJ |
1365 | /// An iterator over subslices separated by elements that match a predicate |
1366 | /// function, limited to a given number of splits. | |
85aaf69f | 1367 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
1368 | pub struct SplitNMut<'a, T: 'a, P> where P: FnMut(&T) -> bool { |
1369 | inner: GenericSplitN<SplitMut<'a, T, P>> | |
1370 | } | |
1371 | ||
54a0048b SL |
1372 | #[stable(feature = "core_impl_debug", since = "1.9.0")] |
1373 | impl<'a, T: 'a + fmt::Debug, P> fmt::Debug for SplitNMut<'a, T, P> where P: FnMut(&T) -> bool { | |
1374 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
1375 | f.debug_struct("SplitNMut") | |
1376 | .field("inner", &self.inner) | |
1377 | .finish() | |
1378 | } | |
1379 | } | |
1380 | ||
1a4d82fc JJ |
1381 | /// An iterator over subslices separated by elements that match a |
1382 | /// predicate function, limited to a given number of splits, starting | |
1383 | /// from the end of the slice. | |
85aaf69f | 1384 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
1385 | pub struct RSplitNMut<'a, T: 'a, P> where P: FnMut(&T) -> bool { |
1386 | inner: GenericSplitN<SplitMut<'a, T, P>> | |
1387 | } | |
1388 | ||
54a0048b SL |
1389 | #[stable(feature = "core_impl_debug", since = "1.9.0")] |
1390 | impl<'a, T: 'a + fmt::Debug, P> fmt::Debug for RSplitNMut<'a, T, P> where P: FnMut(&T) -> bool { | |
1391 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
1392 | f.debug_struct("RSplitNMut") | |
1393 | .field("inner", &self.inner) | |
1394 | .finish() | |
1395 | } | |
1396 | } | |
1397 | ||
1a4d82fc JJ |
1398 | macro_rules! forward_iterator { |
1399 | ($name:ident: $elem:ident, $iter_of:ty) => { | |
85aaf69f | 1400 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
1401 | impl<'a, $elem, P> Iterator for $name<'a, $elem, P> where |
1402 | P: FnMut(&T) -> bool | |
1403 | { | |
1404 | type Item = $iter_of; | |
1405 | ||
1406 | #[inline] | |
1407 | fn next(&mut self) -> Option<$iter_of> { | |
1408 | self.inner.next() | |
1409 | } | |
1410 | ||
1411 | #[inline] | |
85aaf69f | 1412 | fn size_hint(&self) -> (usize, Option<usize>) { |
1a4d82fc JJ |
1413 | self.inner.size_hint() |
1414 | } | |
1415 | } | |
9e0c209e SL |
1416 | |
1417 | #[unstable(feature = "fused", issue = "35602")] | |
1418 | impl<'a, $elem, P> FusedIterator for $name<'a, $elem, P> | |
1419 | where P: FnMut(&T) -> bool {} | |
1a4d82fc JJ |
1420 | } |
1421 | } | |
1422 | ||
1423 | forward_iterator! { SplitN: T, &'a [T] } | |
1424 | forward_iterator! { RSplitN: T, &'a [T] } | |
1425 | forward_iterator! { SplitNMut: T, &'a mut [T] } | |
1426 | forward_iterator! { RSplitNMut: T, &'a mut [T] } | |
1427 | ||
1428 | /// An iterator over overlapping subslices of length `size`. | |
54a0048b | 1429 | #[derive(Debug)] |
85aaf69f | 1430 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
1431 | pub struct Windows<'a, T:'a> { |
1432 | v: &'a [T], | |
85aaf69f | 1433 | size: usize |
1a4d82fc JJ |
1434 | } |
1435 | ||
c34b1796 AL |
1436 | // FIXME(#19839) Remove in favor of `#[derive(Clone)]` |
1437 | #[stable(feature = "rust1", since = "1.0.0")] | |
1438 | impl<'a, T> Clone for Windows<'a, T> { | |
1439 | fn clone(&self) -> Windows<'a, T> { | |
1440 | Windows { | |
1441 | v: self.v, | |
1442 | size: self.size, | |
1443 | } | |
1444 | } | |
1445 | } | |
1446 | ||
85aaf69f | 1447 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
1448 | impl<'a, T> Iterator for Windows<'a, T> { |
1449 | type Item = &'a [T]; | |
1450 | ||
1451 | #[inline] | |
1452 | fn next(&mut self) -> Option<&'a [T]> { | |
1453 | if self.size > self.v.len() { | |
1454 | None | |
1455 | } else { | |
85aaf69f | 1456 | let ret = Some(&self.v[..self.size]); |
1a4d82fc JJ |
1457 | self.v = &self.v[1..]; |
1458 | ret | |
1459 | } | |
1460 | } | |
1461 | ||
1462 | #[inline] | |
85aaf69f | 1463 | fn size_hint(&self) -> (usize, Option<usize>) { |
1a4d82fc JJ |
1464 | if self.size > self.v.len() { |
1465 | (0, Some(0)) | |
1466 | } else { | |
85aaf69f SL |
1467 | let size = self.v.len() - self.size + 1; |
1468 | (size, Some(size)) | |
1469 | } | |
1470 | } | |
e9174d1e SL |
1471 | |
1472 | #[inline] | |
1473 | fn count(self) -> usize { | |
3157f602 | 1474 | self.len() |
e9174d1e SL |
1475 | } |
1476 | ||
1477 | #[inline] | |
1478 | fn nth(&mut self, n: usize) -> Option<Self::Item> { | |
1479 | let (end, overflow) = self.size.overflowing_add(n); | |
1480 | if end > self.v.len() || overflow { | |
1481 | self.v = &[]; | |
1482 | None | |
1483 | } else { | |
1484 | let nth = &self.v[n..end]; | |
1485 | self.v = &self.v[n+1..]; | |
1486 | Some(nth) | |
1487 | } | |
1488 | } | |
1489 | ||
1490 | #[inline] | |
1491 | fn last(self) -> Option<Self::Item> { | |
1492 | if self.size > self.v.len() { | |
1493 | None | |
1494 | } else { | |
1495 | let start = self.v.len() - self.size; | |
1496 | Some(&self.v[start..]) | |
1497 | } | |
1498 | } | |
85aaf69f SL |
1499 | } |
1500 | ||
1501 | #[stable(feature = "rust1", since = "1.0.0")] | |
1502 | impl<'a, T> DoubleEndedIterator for Windows<'a, T> { | |
1503 | #[inline] | |
1504 | fn next_back(&mut self) -> Option<&'a [T]> { | |
1505 | if self.size > self.v.len() { | |
1506 | None | |
1507 | } else { | |
1508 | let ret = Some(&self.v[self.v.len()-self.size..]); | |
1509 | self.v = &self.v[..self.v.len()-1]; | |
1510 | ret | |
1511 | } | |
1512 | } | |
1513 | } | |
1514 | ||
1515 | #[stable(feature = "rust1", since = "1.0.0")] | |
1516 | impl<'a, T> ExactSizeIterator for Windows<'a, T> {} | |
1517 | ||
9e0c209e SL |
1518 | #[unstable(feature = "fused", issue = "35602")] |
1519 | impl<'a, T> FusedIterator for Windows<'a, T> {} | |
1520 | ||
1a4d82fc JJ |
1521 | /// An iterator over a slice in (non-overlapping) chunks (`size` elements at a |
1522 | /// time). | |
1523 | /// | |
1524 | /// When the slice len is not evenly divided by the chunk size, the last slice | |
1525 | /// of the iteration will be the remainder. | |
54a0048b | 1526 | #[derive(Debug)] |
85aaf69f | 1527 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
1528 | pub struct Chunks<'a, T:'a> { |
1529 | v: &'a [T], | |
85aaf69f | 1530 | size: usize |
1a4d82fc JJ |
1531 | } |
1532 | ||
c34b1796 AL |
1533 | // FIXME(#19839) Remove in favor of `#[derive(Clone)]` |
1534 | #[stable(feature = "rust1", since = "1.0.0")] | |
1535 | impl<'a, T> Clone for Chunks<'a, T> { | |
1536 | fn clone(&self) -> Chunks<'a, T> { | |
1537 | Chunks { | |
1538 | v: self.v, | |
1539 | size: self.size, | |
1540 | } | |
1541 | } | |
1542 | } | |
1543 | ||
85aaf69f | 1544 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
1545 | impl<'a, T> Iterator for Chunks<'a, T> { |
1546 | type Item = &'a [T]; | |
1547 | ||
1548 | #[inline] | |
1549 | fn next(&mut self) -> Option<&'a [T]> { | |
9346a6ac | 1550 | if self.v.is_empty() { |
1a4d82fc JJ |
1551 | None |
1552 | } else { | |
1553 | let chunksz = cmp::min(self.v.len(), self.size); | |
1554 | let (fst, snd) = self.v.split_at(chunksz); | |
1555 | self.v = snd; | |
1556 | Some(fst) | |
1557 | } | |
1558 | } | |
1559 | ||
1560 | #[inline] | |
85aaf69f | 1561 | fn size_hint(&self) -> (usize, Option<usize>) { |
9346a6ac | 1562 | if self.v.is_empty() { |
1a4d82fc JJ |
1563 | (0, Some(0)) |
1564 | } else { | |
1565 | let n = self.v.len() / self.size; | |
1566 | let rem = self.v.len() % self.size; | |
1567 | let n = if rem > 0 { n+1 } else { n }; | |
1568 | (n, Some(n)) | |
1569 | } | |
1570 | } | |
e9174d1e SL |
1571 | |
1572 | #[inline] | |
1573 | fn count(self) -> usize { | |
3157f602 | 1574 | self.len() |
e9174d1e SL |
1575 | } |
1576 | ||
1577 | #[inline] | |
1578 | fn nth(&mut self, n: usize) -> Option<Self::Item> { | |
1579 | let (start, overflow) = n.overflowing_mul(self.size); | |
1580 | if start >= self.v.len() || overflow { | |
1581 | self.v = &[]; | |
1582 | None | |
1583 | } else { | |
1584 | let end = match start.checked_add(self.size) { | |
1585 | Some(sum) => cmp::min(self.v.len(), sum), | |
1586 | None => self.v.len(), | |
1587 | }; | |
1588 | let nth = &self.v[start..end]; | |
1589 | self.v = &self.v[end..]; | |
1590 | Some(nth) | |
1591 | } | |
1592 | } | |
1593 | ||
1594 | #[inline] | |
1595 | fn last(self) -> Option<Self::Item> { | |
1596 | if self.v.is_empty() { | |
1597 | None | |
1598 | } else { | |
1599 | let start = (self.v.len() - 1) / self.size * self.size; | |
1600 | Some(&self.v[start..]) | |
1601 | } | |
1602 | } | |
1a4d82fc JJ |
1603 | } |
1604 | ||
85aaf69f | 1605 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
1606 | impl<'a, T> DoubleEndedIterator for Chunks<'a, T> { |
1607 | #[inline] | |
1608 | fn next_back(&mut self) -> Option<&'a [T]> { | |
9346a6ac | 1609 | if self.v.is_empty() { |
1a4d82fc JJ |
1610 | None |
1611 | } else { | |
1612 | let remainder = self.v.len() % self.size; | |
1613 | let chunksz = if remainder != 0 { remainder } else { self.size }; | |
1614 | let (fst, snd) = self.v.split_at(self.v.len() - chunksz); | |
1615 | self.v = fst; | |
1616 | Some(snd) | |
1617 | } | |
1618 | } | |
1619 | } | |
1620 | ||
85aaf69f SL |
1621 | #[stable(feature = "rust1", since = "1.0.0")] |
1622 | impl<'a, T> ExactSizeIterator for Chunks<'a, T> {} | |
1623 | ||
9e0c209e SL |
1624 | #[unstable(feature = "fused", issue = "35602")] |
1625 | impl<'a, T> FusedIterator for Chunks<'a, T> {} | |
1626 | ||
1a4d82fc JJ |
1627 | /// An iterator over a slice in (non-overlapping) mutable chunks (`size` |
1628 | /// elements at a time). When the slice len is not evenly divided by the chunk | |
1629 | /// size, the last slice of the iteration will be the remainder. | |
54a0048b | 1630 | #[derive(Debug)] |
85aaf69f | 1631 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
1632 | pub struct ChunksMut<'a, T:'a> { |
1633 | v: &'a mut [T], | |
85aaf69f | 1634 | chunk_size: usize |
1a4d82fc JJ |
1635 | } |
1636 | ||
85aaf69f | 1637 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
1638 | impl<'a, T> Iterator for ChunksMut<'a, T> { |
1639 | type Item = &'a mut [T]; | |
1640 | ||
1641 | #[inline] | |
1642 | fn next(&mut self) -> Option<&'a mut [T]> { | |
9346a6ac | 1643 | if self.v.is_empty() { |
1a4d82fc JJ |
1644 | None |
1645 | } else { | |
1646 | let sz = cmp::min(self.v.len(), self.chunk_size); | |
1647 | let tmp = mem::replace(&mut self.v, &mut []); | |
1648 | let (head, tail) = tmp.split_at_mut(sz); | |
1649 | self.v = tail; | |
1650 | Some(head) | |
1651 | } | |
1652 | } | |
1653 | ||
1654 | #[inline] | |
85aaf69f | 1655 | fn size_hint(&self) -> (usize, Option<usize>) { |
9346a6ac | 1656 | if self.v.is_empty() { |
1a4d82fc JJ |
1657 | (0, Some(0)) |
1658 | } else { | |
1659 | let n = self.v.len() / self.chunk_size; | |
1660 | let rem = self.v.len() % self.chunk_size; | |
1661 | let n = if rem > 0 { n + 1 } else { n }; | |
1662 | (n, Some(n)) | |
1663 | } | |
1664 | } | |
e9174d1e SL |
1665 | |
1666 | #[inline] | |
1667 | fn count(self) -> usize { | |
3157f602 | 1668 | self.len() |
e9174d1e SL |
1669 | } |
1670 | ||
1671 | #[inline] | |
1672 | fn nth(&mut self, n: usize) -> Option<&'a mut [T]> { | |
1673 | let (start, overflow) = n.overflowing_mul(self.chunk_size); | |
1674 | if start >= self.v.len() || overflow { | |
1675 | self.v = &mut []; | |
1676 | None | |
1677 | } else { | |
1678 | let end = match start.checked_add(self.chunk_size) { | |
1679 | Some(sum) => cmp::min(self.v.len(), sum), | |
1680 | None => self.v.len(), | |
1681 | }; | |
1682 | let tmp = mem::replace(&mut self.v, &mut []); | |
1683 | let (head, tail) = tmp.split_at_mut(end); | |
1684 | let (_, nth) = head.split_at_mut(start); | |
1685 | self.v = tail; | |
1686 | Some(nth) | |
1687 | } | |
1688 | } | |
1689 | ||
1690 | #[inline] | |
1691 | fn last(self) -> Option<Self::Item> { | |
1692 | if self.v.is_empty() { | |
1693 | None | |
1694 | } else { | |
1695 | let start = (self.v.len() - 1) / self.chunk_size * self.chunk_size; | |
1696 | Some(&mut self.v[start..]) | |
1697 | } | |
1698 | } | |
1a4d82fc JJ |
1699 | } |
1700 | ||
85aaf69f | 1701 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
1702 | impl<'a, T> DoubleEndedIterator for ChunksMut<'a, T> { |
1703 | #[inline] | |
1704 | fn next_back(&mut self) -> Option<&'a mut [T]> { | |
9346a6ac | 1705 | if self.v.is_empty() { |
1a4d82fc JJ |
1706 | None |
1707 | } else { | |
1708 | let remainder = self.v.len() % self.chunk_size; | |
1709 | let sz = if remainder != 0 { remainder } else { self.chunk_size }; | |
1710 | let tmp = mem::replace(&mut self.v, &mut []); | |
1711 | let tmp_len = tmp.len(); | |
1712 | let (head, tail) = tmp.split_at_mut(tmp_len - sz); | |
1713 | self.v = head; | |
1714 | Some(tail) | |
1715 | } | |
1716 | } | |
1717 | } | |
1718 | ||
85aaf69f SL |
1719 | #[stable(feature = "rust1", since = "1.0.0")] |
1720 | impl<'a, T> ExactSizeIterator for ChunksMut<'a, T> {} | |
1a4d82fc | 1721 | |
9e0c209e SL |
1722 | #[unstable(feature = "fused", issue = "35602")] |
1723 | impl<'a, T> FusedIterator for ChunksMut<'a, T> {} | |
1724 | ||
1a4d82fc JJ |
1725 | // |
1726 | // Free functions | |
1727 | // | |
1728 | ||
85aaf69f SL |
1729 | /// Forms a slice from a pointer and a length. |
1730 | /// | |
1731 | /// The `len` argument is the number of **elements**, not the number of bytes. | |
1732 | /// | |
b039eaaf | 1733 | /// # Safety |
c1a9b12d | 1734 | /// |
85aaf69f SL |
1735 | /// This function is unsafe as there is no guarantee that the given pointer is |
1736 | /// valid for `len` elements, nor whether the lifetime inferred is a suitable | |
1737 | /// lifetime for the returned slice. | |
1738 | /// | |
c1a9b12d SL |
1739 | /// `p` must be non-null, even for zero-length slices. |
1740 | /// | |
85aaf69f SL |
1741 | /// # Caveat |
1742 | /// | |
1743 | /// The lifetime for the returned slice is inferred from its usage. To | |
1744 | /// prevent accidental misuse, it's suggested to tie the lifetime to whichever | |
1745 | /// source lifetime is safe in the context, such as by providing a helper | |
1746 | /// function taking the lifetime of a host value for the slice, or by explicit | |
1747 | /// annotation. | |
1748 | /// | |
c34b1796 | 1749 | /// # Examples |
85aaf69f | 1750 | /// |
c34b1796 | 1751 | /// ``` |
85aaf69f SL |
1752 | /// use std::slice; |
1753 | /// | |
1754 | /// // manifest a slice out of thin air! | |
1755 | /// let ptr = 0x1234 as *const usize; | |
1756 | /// let amt = 10; | |
1757 | /// unsafe { | |
1758 | /// let slice = slice::from_raw_parts(ptr, amt); | |
1759 | /// } | |
1760 | /// ``` | |
1761 | #[inline] | |
c34b1796 | 1762 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f | 1763 | pub unsafe fn from_raw_parts<'a, T>(p: *const T, len: usize) -> &'a [T] { |
54a0048b | 1764 | mem::transmute(Repr { data: p, len: len }) |
85aaf69f SL |
1765 | } |
1766 | ||
1767 | /// Performs the same functionality as `from_raw_parts`, except that a mutable | |
1768 | /// slice is returned. | |
1769 | /// | |
1770 | /// This function is unsafe for the same reasons as `from_raw_parts`, as well | |
1771 | /// as not being able to provide a non-aliasing guarantee of the returned | |
1772 | /// mutable slice. | |
1773 | #[inline] | |
c34b1796 | 1774 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f | 1775 | pub unsafe fn from_raw_parts_mut<'a, T>(p: *mut T, len: usize) -> &'a mut [T] { |
54a0048b | 1776 | mem::transmute(Repr { data: p, len: len }) |
85aaf69f SL |
1777 | } |
1778 | ||
1a4d82fc | 1779 | // |
54a0048b | 1780 | // Comparison traits |
1a4d82fc JJ |
1781 | // |
1782 | ||
54a0048b SL |
1783 | extern { |
1784 | /// Call implementation provided memcmp | |
1785 | /// | |
1786 | /// Interprets the data as u8. | |
1787 | /// | |
1788 | /// Return 0 for equal, < 0 for less than and > 0 for greater | |
1789 | /// than. | |
1790 | // FIXME(#32610): Return type should be c_int | |
1791 | fn memcmp(s1: *const u8, s2: *const u8, n: usize) -> i32; | |
1792 | } | |
1a4d82fc | 1793 | |
54a0048b SL |
1794 | #[stable(feature = "rust1", since = "1.0.0")] |
1795 | impl<A, B> PartialEq<[B]> for [A] where A: PartialEq<B> { | |
1796 | fn eq(&self, other: &[B]) -> bool { | |
1797 | SlicePartialEq::equal(self, other) | |
1a4d82fc JJ |
1798 | } |
1799 | ||
54a0048b SL |
1800 | fn ne(&self, other: &[B]) -> bool { |
1801 | SlicePartialEq::not_equal(self, other) | |
1a4d82fc | 1802 | } |
54a0048b | 1803 | } |
1a4d82fc | 1804 | |
54a0048b SL |
1805 | #[stable(feature = "rust1", since = "1.0.0")] |
1806 | impl<T: Eq> Eq for [T] {} | |
1807 | ||
1808 | #[stable(feature = "rust1", since = "1.0.0")] | |
1809 | impl<T: Ord> Ord for [T] { | |
1810 | fn cmp(&self, other: &[T]) -> Ordering { | |
1811 | SliceOrd::compare(self, other) | |
1a4d82fc JJ |
1812 | } |
1813 | } | |
1814 | ||
54a0048b SL |
1815 | #[stable(feature = "rust1", since = "1.0.0")] |
1816 | impl<T: PartialOrd> PartialOrd for [T] { | |
1817 | fn partial_cmp(&self, other: &[T]) -> Option<Ordering> { | |
1818 | SlicePartialOrd::partial_compare(self, other) | |
1819 | } | |
1820 | } | |
1a4d82fc | 1821 | |
54a0048b SL |
1822 | #[doc(hidden)] |
1823 | // intermediate trait for specialization of slice's PartialEq | |
1824 | trait SlicePartialEq<B> { | |
1825 | fn equal(&self, other: &[B]) -> bool; | |
9e0c209e SL |
1826 | |
1827 | fn not_equal(&self, other: &[B]) -> bool { !self.equal(other) } | |
54a0048b | 1828 | } |
1a4d82fc | 1829 | |
54a0048b SL |
1830 | // Generic slice equality |
1831 | impl<A, B> SlicePartialEq<B> for [A] | |
1832 | where A: PartialEq<B> | |
1833 | { | |
1834 | default fn equal(&self, other: &[B]) -> bool { | |
c1a9b12d SL |
1835 | if self.len() != other.len() { |
1836 | return false; | |
1837 | } | |
1838 | ||
1839 | for i in 0..self.len() { | |
1840 | if !self[i].eq(&other[i]) { | |
1841 | return false; | |
1842 | } | |
1843 | } | |
1844 | ||
1845 | true | |
1a4d82fc | 1846 | } |
1a4d82fc JJ |
1847 | } |
1848 | ||
54a0048b SL |
1849 | // Use memcmp for bytewise equality when the types allow |
1850 | impl<A> SlicePartialEq<A> for [A] | |
1851 | where A: PartialEq<A> + BytewiseEquality | |
1852 | { | |
1853 | fn equal(&self, other: &[A]) -> bool { | |
1854 | if self.len() != other.len() { | |
1855 | return false; | |
1856 | } | |
3157f602 XL |
1857 | if self.as_ptr() == other.as_ptr() { |
1858 | return true; | |
1859 | } | |
54a0048b SL |
1860 | unsafe { |
1861 | let size = mem::size_of_val(self); | |
1862 | memcmp(self.as_ptr() as *const u8, | |
1863 | other.as_ptr() as *const u8, size) == 0 | |
1864 | } | |
1865 | } | |
54a0048b SL |
1866 | } |
1867 | ||
1868 | #[doc(hidden)] | |
1869 | // intermediate trait for specialization of slice's PartialOrd | |
1870 | trait SlicePartialOrd<B> { | |
1871 | fn partial_compare(&self, other: &[B]) -> Option<Ordering>; | |
1872 | } | |
1873 | ||
1874 | impl<A> SlicePartialOrd<A> for [A] | |
1875 | where A: PartialOrd | |
1876 | { | |
1877 | default fn partial_compare(&self, other: &[A]) -> Option<Ordering> { | |
b039eaaf SL |
1878 | let l = cmp::min(self.len(), other.len()); |
1879 | ||
1880 | // Slice to the loop iteration range to enable bound check | |
1881 | // elimination in the compiler | |
1882 | let lhs = &self[..l]; | |
1883 | let rhs = &other[..l]; | |
1884 | ||
1885 | for i in 0..l { | |
54a0048b SL |
1886 | match lhs[i].partial_cmp(&rhs[i]) { |
1887 | Some(Ordering::Equal) => (), | |
b039eaaf SL |
1888 | non_eq => return non_eq, |
1889 | } | |
1890 | } | |
1891 | ||
54a0048b | 1892 | self.len().partial_cmp(&other.len()) |
1a4d82fc JJ |
1893 | } |
1894 | } | |
1895 | ||
54a0048b SL |
1896 | impl SlicePartialOrd<u8> for [u8] { |
1897 | #[inline] | |
1898 | fn partial_compare(&self, other: &[u8]) -> Option<Ordering> { | |
1899 | Some(SliceOrd::compare(self, other)) | |
1900 | } | |
1901 | } | |
1902 | ||
1903 | #[doc(hidden)] | |
1904 | // intermediate trait for specialization of slice's Ord | |
1905 | trait SliceOrd<B> { | |
1906 | fn compare(&self, other: &[B]) -> Ordering; | |
1907 | } | |
1908 | ||
1909 | impl<A> SliceOrd<A> for [A] | |
1910 | where A: Ord | |
1911 | { | |
1912 | default fn compare(&self, other: &[A]) -> Ordering { | |
b039eaaf SL |
1913 | let l = cmp::min(self.len(), other.len()); |
1914 | ||
1915 | // Slice to the loop iteration range to enable bound check | |
1916 | // elimination in the compiler | |
1917 | let lhs = &self[..l]; | |
1918 | let rhs = &other[..l]; | |
1919 | ||
1920 | for i in 0..l { | |
54a0048b SL |
1921 | match lhs[i].cmp(&rhs[i]) { |
1922 | Ordering::Equal => (), | |
b039eaaf SL |
1923 | non_eq => return non_eq, |
1924 | } | |
1925 | } | |
1926 | ||
54a0048b SL |
1927 | self.len().cmp(&other.len()) |
1928 | } | |
1929 | } | |
1930 | ||
1931 | // memcmp compares a sequence of unsigned bytes lexicographically. | |
1932 | // this matches the order we want for [u8], but no others (not even [i8]). | |
1933 | impl SliceOrd<u8> for [u8] { | |
1934 | #[inline] | |
1935 | fn compare(&self, other: &[u8]) -> Ordering { | |
1936 | let order = unsafe { | |
1937 | memcmp(self.as_ptr(), other.as_ptr(), | |
1938 | cmp::min(self.len(), other.len())) | |
1939 | }; | |
1940 | if order == 0 { | |
1941 | self.len().cmp(&other.len()) | |
1942 | } else if order < 0 { | |
1943 | Less | |
1944 | } else { | |
1945 | Greater | |
1946 | } | |
1947 | } | |
1948 | } | |
1949 | ||
1950 | #[doc(hidden)] | |
1951 | /// Trait implemented for types that can be compared for equality using | |
1952 | /// their bytewise representation | |
1953 | trait BytewiseEquality { } | |
1954 | ||
1955 | macro_rules! impl_marker_for { | |
1956 | ($traitname:ident, $($ty:ty)*) => { | |
1957 | $( | |
1958 | impl $traitname for $ty { } | |
1959 | )* | |
1a4d82fc JJ |
1960 | } |
1961 | } | |
54a0048b SL |
1962 | |
1963 | impl_marker_for!(BytewiseEquality, | |
1964 | u8 i8 u16 i16 u32 i32 u64 i64 usize isize char bool); | |
3157f602 XL |
1965 | |
1966 | #[doc(hidden)] | |
1967 | unsafe impl<'a, T> TrustedRandomAccess for Iter<'a, T> { | |
1968 | unsafe fn get_unchecked(&mut self, i: usize) -> &'a T { | |
1969 | &*self.ptr.offset(i as isize) | |
1970 | } | |
1971 | } | |
1972 | ||
1973 | #[doc(hidden)] | |
1974 | unsafe impl<'a, T> TrustedRandomAccess for IterMut<'a, T> { | |
1975 | unsafe fn get_unchecked(&mut self, i: usize) -> &'a mut T { | |
1976 | &mut *self.ptr.offset(i as isize) | |
1977 | } | |
1978 | } |