]> git.lizzy.rs Git - rust.git/blob - src/liballoc/slice.rs
Rollup merge of #73269 - mzohreva:mz/sgx-wait-timeout, r=jethrogb
[rust.git] / src / liballoc / slice.rs
1 //! A dynamically-sized view into a contiguous sequence, `[T]`.
2 //!
3 //! *[See also the slice primitive type](../../std/primitive.slice.html).*
4 //!
5 //! Slices are a view into a block of memory represented as a pointer and a
6 //! length.
7 //!
8 //! ```
9 //! // slicing a Vec
10 //! let vec = vec![1, 2, 3];
11 //! let int_slice = &vec[..];
12 //! // coercing an array to a slice
13 //! let str_slice: &[&str] = &["one", "two", "three"];
14 //! ```
15 //!
16 //! Slices are either mutable or shared. The shared slice type is `&[T]`,
17 //! while the mutable slice type is `&mut [T]`, where `T` represents the element
18 //! type. For example, you can mutate the block of memory that a mutable slice
19 //! points to:
20 //!
21 //! ```
22 //! let x = &mut [1, 2, 3];
23 //! x[1] = 7;
24 //! assert_eq!(x, &[1, 7, 3]);
25 //! ```
26 //!
27 //! Here are some of the things this module contains:
28 //!
29 //! ## Structs
30 //!
31 //! There are several structs that are useful for slices, such as [`Iter`], which
32 //! represents iteration over a slice.
33 //!
34 //! ## Trait Implementations
35 //!
36 //! There are several implementations of common traits for slices. Some examples
37 //! include:
38 //!
39 //! * [`Clone`]
40 //! * [`Eq`], [`Ord`] - for slices whose element type are [`Eq`] or [`Ord`].
41 //! * [`Hash`] - for slices whose element type is [`Hash`].
42 //!
43 //! ## Iteration
44 //!
45 //! The slices implement `IntoIterator`. The iterator yields references to the
46 //! slice elements.
47 //!
48 //! ```
49 //! let numbers = &[0, 1, 2];
50 //! for n in numbers {
51 //!     println!("{} is a number!", n);
52 //! }
53 //! ```
54 //!
55 //! The mutable slice yields mutable references to the elements:
56 //!
57 //! ```
58 //! let mut scores = [7, 8, 9];
59 //! for score in &mut scores[..] {
60 //!     *score += 1;
61 //! }
62 //! ```
63 //!
64 //! This iterator yields mutable references to the slice's elements, so while
65 //! the element type of the slice is `i32`, the element type of the iterator is
66 //! `&mut i32`.
67 //!
68 //! * [`.iter`] and [`.iter_mut`] are the explicit methods to return the default
69 //!   iterators.
70 //! * Further methods that return iterators are [`.split`], [`.splitn`],
71 //!   [`.chunks`], [`.windows`] and more.
72 //!
73 //! [`Clone`]: ../../std/clone/trait.Clone.html
74 //! [`Eq`]: ../../std/cmp/trait.Eq.html
75 //! [`Ord`]: ../../std/cmp/trait.Ord.html
76 //! [`Iter`]: struct.Iter.html
77 //! [`Hash`]: ../../std/hash/trait.Hash.html
78 //! [`.iter`]: ../../std/primitive.slice.html#method.iter
79 //! [`.iter_mut`]: ../../std/primitive.slice.html#method.iter_mut
80 //! [`.split`]: ../../std/primitive.slice.html#method.split
81 //! [`.splitn`]: ../../std/primitive.slice.html#method.splitn
82 //! [`.chunks`]: ../../std/primitive.slice.html#method.chunks
83 //! [`.windows`]: ../../std/primitive.slice.html#method.windows
84 #![stable(feature = "rust1", since = "1.0.0")]
85 // Many of the usings in this module are only used in the test configuration.
86 // It's cleaner to just turn off the unused_imports warning than to fix them.
87 #![cfg_attr(test, allow(unused_imports, dead_code))]
88
89 use core::borrow::{Borrow, BorrowMut};
90 use core::cmp::Ordering::{self, Less};
91 use core::mem::{self, size_of};
92 use core::ptr;
93
94 use crate::borrow::ToOwned;
95 use crate::boxed::Box;
96 use crate::vec::Vec;
97
98 #[stable(feature = "slice_get_slice", since = "1.28.0")]
99 pub use core::slice::SliceIndex;
100 #[stable(feature = "from_ref", since = "1.28.0")]
101 pub use core::slice::{from_mut, from_ref};
102 #[stable(feature = "rust1", since = "1.0.0")]
103 pub use core::slice::{from_raw_parts, from_raw_parts_mut};
104 #[stable(feature = "rust1", since = "1.0.0")]
105 pub use core::slice::{Chunks, Windows};
106 #[stable(feature = "chunks_exact", since = "1.31.0")]
107 pub use core::slice::{ChunksExact, ChunksExactMut};
108 #[stable(feature = "rust1", since = "1.0.0")]
109 pub use core::slice::{ChunksMut, Split, SplitMut};
110 #[stable(feature = "rust1", since = "1.0.0")]
111 pub use core::slice::{Iter, IterMut};
112 #[stable(feature = "rchunks", since = "1.31.0")]
113 pub use core::slice::{RChunks, RChunksExact, RChunksExactMut, RChunksMut};
114 #[stable(feature = "slice_rsplit", since = "1.27.0")]
115 pub use core::slice::{RSplit, RSplitMut};
116 #[stable(feature = "rust1", since = "1.0.0")]
117 pub use core::slice::{RSplitN, RSplitNMut, SplitN, SplitNMut};
118
119 ////////////////////////////////////////////////////////////////////////////////
120 // Basic slice extension methods
121 ////////////////////////////////////////////////////////////////////////////////
122
123 // HACK(japaric) needed for the implementation of `vec!` macro during testing
124 // N.B., see the `hack` module in this file for more details.
125 #[cfg(test)]
126 pub use hack::into_vec;
127
128 // HACK(japaric) needed for the implementation of `Vec::clone` during testing
129 // N.B., see the `hack` module in this file for more details.
130 #[cfg(test)]
131 pub use hack::to_vec;
132
133 // HACK(japaric): With cfg(test) `impl [T]` is not available, these three
134 // functions are actually methods that are in `impl [T]` but not in
135 // `core::slice::SliceExt` - we need to supply these functions for the
136 // `test_permutations` test
137 mod hack {
138     use crate::boxed::Box;
139     use crate::vec::Vec;
140
141     // We shouldn't add inline attribute to this since this is used in
142     // `vec!` macro mostly and causes perf regression. See #71204 for
143     // discussion and perf results.
144     pub fn into_vec<T>(b: Box<[T]>) -> Vec<T> {
145         unsafe {
146             let len = b.len();
147             let b = Box::into_raw(b);
148             Vec::from_raw_parts(b as *mut T, len, len)
149         }
150     }
151
152     #[inline]
153     pub fn to_vec<T>(s: &[T]) -> Vec<T>
154     where
155         T: Clone,
156     {
157         let mut vec = Vec::with_capacity(s.len());
158         vec.extend_from_slice(s);
159         vec
160     }
161 }
162
163 #[lang = "slice_alloc"]
164 #[cfg(not(test))]
165 impl<T> [T] {
166     /// Sorts the slice.
167     ///
168     /// This sort is stable (i.e., does not reorder equal elements) and `O(n * log(n))` worst-case.
169     ///
170     /// When applicable, unstable sorting is preferred because it is generally faster than stable
171     /// sorting and it doesn't allocate auxiliary memory.
172     /// See [`sort_unstable`](#method.sort_unstable).
173     ///
174     /// # Current implementation
175     ///
176     /// The current algorithm is an adaptive, iterative merge sort inspired by
177     /// [timsort](https://en.wikipedia.org/wiki/Timsort).
178     /// It is designed to be very fast in cases where the slice is nearly sorted, or consists of
179     /// two or more sorted sequences concatenated one after another.
180     ///
181     /// Also, it allocates temporary storage half the size of `self`, but for short slices a
182     /// non-allocating insertion sort is used instead.
183     ///
184     /// # Examples
185     ///
186     /// ```
187     /// let mut v = [-5, 4, 1, -3, 2];
188     ///
189     /// v.sort();
190     /// assert!(v == [-5, -3, 1, 2, 4]);
191     /// ```
192     #[stable(feature = "rust1", since = "1.0.0")]
193     #[inline]
194     pub fn sort(&mut self)
195     where
196         T: Ord,
197     {
198         merge_sort(self, |a, b| a.lt(b));
199     }
200
201     /// Sorts the slice with a comparator function.
202     ///
203     /// This sort is stable (i.e., does not reorder equal elements) and `O(n * log(n))` worst-case.
204     ///
205     /// The comparator function must define a total ordering for the elements in the slice. If
206     /// the ordering is not total, the order of the elements is unspecified. An order is a
207     /// total order if it is (for all `a`, `b` and `c`):
208     ///
209     /// * total and antisymmetric: exactly one of `a < b`, `a == b` or `a > b` is true, and
210     /// * transitive, `a < b` and `b < c` implies `a < c`. The same must hold for both `==` and `>`.
211     ///
212     /// For example, while [`f64`] doesn't implement [`Ord`] because `NaN != NaN`, we can use
213     /// `partial_cmp` as our sort function when we know the slice doesn't contain a `NaN`.
214     ///
215     /// ```
216     /// let mut floats = [5f64, 4.0, 1.0, 3.0, 2.0];
217     /// floats.sort_by(|a, b| a.partial_cmp(b).unwrap());
218     /// assert_eq!(floats, [1.0, 2.0, 3.0, 4.0, 5.0]);
219     /// ```
220     ///
221     /// When applicable, unstable sorting is preferred because it is generally faster than stable
222     /// sorting and it doesn't allocate auxiliary memory.
223     /// See [`sort_unstable_by`](#method.sort_unstable_by).
224     ///
225     /// # Current implementation
226     ///
227     /// The current algorithm is an adaptive, iterative merge sort inspired by
228     /// [timsort](https://en.wikipedia.org/wiki/Timsort).
229     /// It is designed to be very fast in cases where the slice is nearly sorted, or consists of
230     /// two or more sorted sequences concatenated one after another.
231     ///
232     /// Also, it allocates temporary storage half the size of `self`, but for short slices a
233     /// non-allocating insertion sort is used instead.
234     ///
235     /// # Examples
236     ///
237     /// ```
238     /// let mut v = [5, 4, 1, 3, 2];
239     /// v.sort_by(|a, b| a.cmp(b));
240     /// assert!(v == [1, 2, 3, 4, 5]);
241     ///
242     /// // reverse sorting
243     /// v.sort_by(|a, b| b.cmp(a));
244     /// assert!(v == [5, 4, 3, 2, 1]);
245     /// ```
246     #[stable(feature = "rust1", since = "1.0.0")]
247     #[inline]
248     pub fn sort_by<F>(&mut self, mut compare: F)
249     where
250         F: FnMut(&T, &T) -> Ordering,
251     {
252         merge_sort(self, |a, b| compare(a, b) == Less);
253     }
254
255     /// Sorts the slice with a key extraction function.
256     ///
257     /// This sort is stable (i.e., does not reorder equal elements) and `O(m * n * log(n))`
258     /// worst-case, where the key function is `O(m)`.
259     ///
260     /// For expensive key functions (e.g. functions that are not simple property accesses or
261     /// basic operations), [`sort_by_cached_key`](#method.sort_by_cached_key) is likely to be
262     /// significantly faster, as it does not recompute element keys.
263     ///
264     /// When applicable, unstable sorting is preferred because it is generally faster than stable
265     /// sorting and it doesn't allocate auxiliary memory.
266     /// See [`sort_unstable_by_key`](#method.sort_unstable_by_key).
267     ///
268     /// # Current implementation
269     ///
270     /// The current algorithm is an adaptive, iterative merge sort inspired by
271     /// [timsort](https://en.wikipedia.org/wiki/Timsort).
272     /// It is designed to be very fast in cases where the slice is nearly sorted, or consists of
273     /// two or more sorted sequences concatenated one after another.
274     ///
275     /// Also, it allocates temporary storage half the size of `self`, but for short slices a
276     /// non-allocating insertion sort is used instead.
277     ///
278     /// # Examples
279     ///
280     /// ```
281     /// let mut v = [-5i32, 4, 1, -3, 2];
282     ///
283     /// v.sort_by_key(|k| k.abs());
284     /// assert!(v == [1, 2, -3, 4, -5]);
285     /// ```
286     #[stable(feature = "slice_sort_by_key", since = "1.7.0")]
287     #[inline]
288     pub fn sort_by_key<K, F>(&mut self, mut f: F)
289     where
290         F: FnMut(&T) -> K,
291         K: Ord,
292     {
293         merge_sort(self, |a, b| f(a).lt(&f(b)));
294     }
295
296     /// Sorts the slice with a key extraction function.
297     ///
298     /// During sorting, the key function is called only once per element.
299     ///
300     /// This sort is stable (i.e., does not reorder equal elements) and `O(m * n + n * log(n))`
301     /// worst-case, where the key function is `O(m)`.
302     ///
303     /// For simple key functions (e.g., functions that are property accesses or
304     /// basic operations), [`sort_by_key`](#method.sort_by_key) is likely to be
305     /// faster.
306     ///
307     /// # Current implementation
308     ///
309     /// The current algorithm is based on [pattern-defeating quicksort][pdqsort] by Orson Peters,
310     /// which combines the fast average case of randomized quicksort with the fast worst case of
311     /// heapsort, while achieving linear time on slices with certain patterns. It uses some
312     /// randomization to avoid degenerate cases, but with a fixed seed to always provide
313     /// deterministic behavior.
314     ///
315     /// In the worst case, the algorithm allocates temporary storage in a `Vec<(K, usize)>` the
316     /// length of the slice.
317     ///
318     /// # Examples
319     ///
320     /// ```
321     /// let mut v = [-5i32, 4, 32, -3, 2];
322     ///
323     /// v.sort_by_cached_key(|k| k.to_string());
324     /// assert!(v == [-3, -5, 2, 32, 4]);
325     /// ```
326     ///
327     /// [pdqsort]: https://github.com/orlp/pdqsort
328     #[stable(feature = "slice_sort_by_cached_key", since = "1.34.0")]
329     #[inline]
330     pub fn sort_by_cached_key<K, F>(&mut self, f: F)
331     where
332         F: FnMut(&T) -> K,
333         K: Ord,
334     {
335         // Helper macro for indexing our vector by the smallest possible type, to reduce allocation.
336         macro_rules! sort_by_key {
337             ($t:ty, $slice:ident, $f:ident) => {{
338                 let mut indices: Vec<_> =
339                     $slice.iter().map($f).enumerate().map(|(i, k)| (k, i as $t)).collect();
340                 // The elements of `indices` are unique, as they are indexed, so any sort will be
341                 // stable with respect to the original slice. We use `sort_unstable` here because
342                 // it requires less memory allocation.
343                 indices.sort_unstable();
344                 for i in 0..$slice.len() {
345                     let mut index = indices[i].1;
346                     while (index as usize) < i {
347                         index = indices[index as usize].1;
348                     }
349                     indices[i].1 = index;
350                     $slice.swap(i, index as usize);
351                 }
352             }};
353         }
354
355         let sz_u8 = mem::size_of::<(K, u8)>();
356         let sz_u16 = mem::size_of::<(K, u16)>();
357         let sz_u32 = mem::size_of::<(K, u32)>();
358         let sz_usize = mem::size_of::<(K, usize)>();
359
360         let len = self.len();
361         if len < 2 {
362             return;
363         }
364         if sz_u8 < sz_u16 && len <= (u8::MAX as usize) {
365             return sort_by_key!(u8, self, f);
366         }
367         if sz_u16 < sz_u32 && len <= (u16::MAX as usize) {
368             return sort_by_key!(u16, self, f);
369         }
370         if sz_u32 < sz_usize && len <= (u32::MAX as usize) {
371             return sort_by_key!(u32, self, f);
372         }
373         sort_by_key!(usize, self, f)
374     }
375
376     /// Copies `self` into a new `Vec`.
377     ///
378     /// # Examples
379     ///
380     /// ```
381     /// let s = [10, 40, 30];
382     /// let x = s.to_vec();
383     /// // Here, `s` and `x` can be modified independently.
384     /// ```
385     #[rustc_conversion_suggestion]
386     #[stable(feature = "rust1", since = "1.0.0")]
387     #[inline]
388     pub fn to_vec(&self) -> Vec<T>
389     where
390         T: Clone,
391     {
392         // N.B., see the `hack` module in this file for more details.
393         hack::to_vec(self)
394     }
395
396     /// Converts `self` into a vector without clones or allocation.
397     ///
398     /// The resulting vector can be converted back into a box via
399     /// `Vec<T>`'s `into_boxed_slice` method.
400     ///
401     /// # Examples
402     ///
403     /// ```
404     /// let s: Box<[i32]> = Box::new([10, 40, 30]);
405     /// let x = s.into_vec();
406     /// // `s` cannot be used anymore because it has been converted into `x`.
407     ///
408     /// assert_eq!(x, vec![10, 40, 30]);
409     /// ```
410     #[stable(feature = "rust1", since = "1.0.0")]
411     #[inline]
412     pub fn into_vec(self: Box<Self>) -> Vec<T> {
413         // N.B., see the `hack` module in this file for more details.
414         hack::into_vec(self)
415     }
416
417     /// Creates a vector by repeating a slice `n` times.
418     ///
419     /// # Panics
420     ///
421     /// This function will panic if the capacity would overflow.
422     ///
423     /// # Examples
424     ///
425     /// Basic usage:
426     ///
427     /// ```
428     /// assert_eq!([1, 2].repeat(3), vec![1, 2, 1, 2, 1, 2]);
429     /// ```
430     ///
431     /// A panic upon overflow:
432     ///
433     /// ```should_panic
434     /// // this will panic at runtime
435     /// b"0123456789abcdef".repeat(usize::MAX);
436     /// ```
437     #[stable(feature = "repeat_generic_slice", since = "1.40.0")]
438     pub fn repeat(&self, n: usize) -> Vec<T>
439     where
440         T: Copy,
441     {
442         if n == 0 {
443             return Vec::new();
444         }
445
446         // If `n` is larger than zero, it can be split as
447         // `n = 2^expn + rem (2^expn > rem, expn >= 0, rem >= 0)`.
448         // `2^expn` is the number represented by the leftmost '1' bit of `n`,
449         // and `rem` is the remaining part of `n`.
450
451         // Using `Vec` to access `set_len()`.
452         let capacity = self.len().checked_mul(n).expect("capacity overflow");
453         let mut buf = Vec::with_capacity(capacity);
454
455         // `2^expn` repetition is done by doubling `buf` `expn`-times.
456         buf.extend(self);
457         {
458             let mut m = n >> 1;
459             // If `m > 0`, there are remaining bits up to the leftmost '1'.
460             while m > 0 {
461                 // `buf.extend(buf)`:
462                 unsafe {
463                     ptr::copy_nonoverlapping(
464                         buf.as_ptr(),
465                         (buf.as_mut_ptr() as *mut T).add(buf.len()),
466                         buf.len(),
467                     );
468                     // `buf` has capacity of `self.len() * n`.
469                     let buf_len = buf.len();
470                     buf.set_len(buf_len * 2);
471                 }
472
473                 m >>= 1;
474             }
475         }
476
477         // `rem` (`= n - 2^expn`) repetition is done by copying
478         // first `rem` repetitions from `buf` itself.
479         let rem_len = capacity - buf.len(); // `self.len() * rem`
480         if rem_len > 0 {
481             // `buf.extend(buf[0 .. rem_len])`:
482             unsafe {
483                 // This is non-overlapping since `2^expn > rem`.
484                 ptr::copy_nonoverlapping(
485                     buf.as_ptr(),
486                     (buf.as_mut_ptr() as *mut T).add(buf.len()),
487                     rem_len,
488                 );
489                 // `buf.len() + rem_len` equals to `buf.capacity()` (`= self.len() * n`).
490                 buf.set_len(capacity);
491             }
492         }
493         buf
494     }
495
496     /// Flattens a slice of `T` into a single value `Self::Output`.
497     ///
498     /// # Examples
499     ///
500     /// ```
501     /// assert_eq!(["hello", "world"].concat(), "helloworld");
502     /// assert_eq!([[1, 2], [3, 4]].concat(), [1, 2, 3, 4]);
503     /// ```
504     #[stable(feature = "rust1", since = "1.0.0")]
505     pub fn concat<Item: ?Sized>(&self) -> <Self as Concat<Item>>::Output
506     where
507         Self: Concat<Item>,
508     {
509         Concat::concat(self)
510     }
511
512     /// Flattens a slice of `T` into a single value `Self::Output`, placing a
513     /// given separator between each.
514     ///
515     /// # Examples
516     ///
517     /// ```
518     /// assert_eq!(["hello", "world"].join(" "), "hello world");
519     /// assert_eq!([[1, 2], [3, 4]].join(&0), [1, 2, 0, 3, 4]);
520     /// assert_eq!([[1, 2], [3, 4]].join(&[0, 0][..]), [1, 2, 0, 0, 3, 4]);
521     /// ```
522     #[stable(feature = "rename_connect_to_join", since = "1.3.0")]
523     pub fn join<Separator>(&self, sep: Separator) -> <Self as Join<Separator>>::Output
524     where
525         Self: Join<Separator>,
526     {
527         Join::join(self, sep)
528     }
529
530     /// Flattens a slice of `T` into a single value `Self::Output`, placing a
531     /// given separator between each.
532     ///
533     /// # Examples
534     ///
535     /// ```
536     /// # #![allow(deprecated)]
537     /// assert_eq!(["hello", "world"].connect(" "), "hello world");
538     /// assert_eq!([[1, 2], [3, 4]].connect(&0), [1, 2, 0, 3, 4]);
539     /// ```
540     #[stable(feature = "rust1", since = "1.0.0")]
541     #[rustc_deprecated(since = "1.3.0", reason = "renamed to join")]
542     pub fn connect<Separator>(&self, sep: Separator) -> <Self as Join<Separator>>::Output
543     where
544         Self: Join<Separator>,
545     {
546         Join::join(self, sep)
547     }
548 }
549
550 #[lang = "slice_u8_alloc"]
551 #[cfg(not(test))]
552 impl [u8] {
553     /// Returns a vector containing a copy of this slice where each byte
554     /// is mapped to its ASCII upper case equivalent.
555     ///
556     /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
557     /// but non-ASCII letters are unchanged.
558     ///
559     /// To uppercase the value in-place, use [`make_ascii_uppercase`].
560     ///
561     /// [`make_ascii_uppercase`]: #method.make_ascii_uppercase
562     #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
563     #[inline]
564     pub fn to_ascii_uppercase(&self) -> Vec<u8> {
565         let mut me = self.to_vec();
566         me.make_ascii_uppercase();
567         me
568     }
569
570     /// Returns a vector containing a copy of this slice where each byte
571     /// is mapped to its ASCII lower case equivalent.
572     ///
573     /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
574     /// but non-ASCII letters are unchanged.
575     ///
576     /// To lowercase the value in-place, use [`make_ascii_lowercase`].
577     ///
578     /// [`make_ascii_lowercase`]: #method.make_ascii_lowercase
579     #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
580     #[inline]
581     pub fn to_ascii_lowercase(&self) -> Vec<u8> {
582         let mut me = self.to_vec();
583         me.make_ascii_lowercase();
584         me
585     }
586 }
587
588 ////////////////////////////////////////////////////////////////////////////////
589 // Extension traits for slices over specific kinds of data
590 ////////////////////////////////////////////////////////////////////////////////
591
592 /// Helper trait for [`[T]::concat`](../../std/primitive.slice.html#method.concat).
593 ///
594 /// Note: the `Item` type parameter is not used in this trait,
595 /// but it allows impls to be more generic.
596 /// Without it, we get this error:
597 ///
598 /// ```error
599 /// error[E0207]: the type parameter `T` is not constrained by the impl trait, self type, or predica
600 ///    --> src/liballoc/slice.rs:608:6
601 ///     |
602 /// 608 | impl<T: Clone, V: Borrow<[T]>> Concat for [V] {
603 ///     |      ^ unconstrained type parameter
604 /// ```
605 ///
606 /// This is because there could exist `V` types with multiple `Borrow<[_]>` impls,
607 /// such that multiple `T` types would apply:
608 ///
609 /// ```
610 /// # #[allow(dead_code)]
611 /// pub struct Foo(Vec<u32>, Vec<String>);
612 ///
613 /// impl std::borrow::Borrow<[u32]> for Foo {
614 ///     fn borrow(&self) -> &[u32] { &self.0 }
615 /// }
616 ///
617 /// impl std::borrow::Borrow<[String]> for Foo {
618 ///     fn borrow(&self) -> &[String] { &self.1 }
619 /// }
620 /// ```
621 #[unstable(feature = "slice_concat_trait", issue = "27747")]
622 pub trait Concat<Item: ?Sized> {
623     #[unstable(feature = "slice_concat_trait", issue = "27747")]
624     /// The resulting type after concatenation
625     type Output;
626
627     /// Implementation of [`[T]::concat`](../../std/primitive.slice.html#method.concat)
628     #[unstable(feature = "slice_concat_trait", issue = "27747")]
629     fn concat(slice: &Self) -> Self::Output;
630 }
631
632 /// Helper trait for [`[T]::join`](../../std/primitive.slice.html#method.join)
633 #[unstable(feature = "slice_concat_trait", issue = "27747")]
634 pub trait Join<Separator> {
635     #[unstable(feature = "slice_concat_trait", issue = "27747")]
636     /// The resulting type after concatenation
637     type Output;
638
639     /// Implementation of [`[T]::join`](../../std/primitive.slice.html#method.join)
640     #[unstable(feature = "slice_concat_trait", issue = "27747")]
641     fn join(slice: &Self, sep: Separator) -> Self::Output;
642 }
643
644 #[unstable(feature = "slice_concat_ext", issue = "27747")]
645 impl<T: Clone, V: Borrow<[T]>> Concat<T> for [V] {
646     type Output = Vec<T>;
647
648     fn concat(slice: &Self) -> Vec<T> {
649         let size = slice.iter().map(|slice| slice.borrow().len()).sum();
650         let mut result = Vec::with_capacity(size);
651         for v in slice {
652             result.extend_from_slice(v.borrow())
653         }
654         result
655     }
656 }
657
658 #[unstable(feature = "slice_concat_ext", issue = "27747")]
659 impl<T: Clone, V: Borrow<[T]>> Join<&T> for [V] {
660     type Output = Vec<T>;
661
662     fn join(slice: &Self, sep: &T) -> Vec<T> {
663         let mut iter = slice.iter();
664         let first = match iter.next() {
665             Some(first) => first,
666             None => return vec![],
667         };
668         let size = slice.iter().map(|v| v.borrow().len()).sum::<usize>() + slice.len() - 1;
669         let mut result = Vec::with_capacity(size);
670         result.extend_from_slice(first.borrow());
671
672         for v in iter {
673             result.push(sep.clone());
674             result.extend_from_slice(v.borrow())
675         }
676         result
677     }
678 }
679
680 #[unstable(feature = "slice_concat_ext", issue = "27747")]
681 impl<T: Clone, V: Borrow<[T]>> Join<&[T]> for [V] {
682     type Output = Vec<T>;
683
684     fn join(slice: &Self, sep: &[T]) -> Vec<T> {
685         let mut iter = slice.iter();
686         let first = match iter.next() {
687             Some(first) => first,
688             None => return vec![],
689         };
690         let size =
691             slice.iter().map(|v| v.borrow().len()).sum::<usize>() + sep.len() * (slice.len() - 1);
692         let mut result = Vec::with_capacity(size);
693         result.extend_from_slice(first.borrow());
694
695         for v in iter {
696             result.extend_from_slice(sep);
697             result.extend_from_slice(v.borrow())
698         }
699         result
700     }
701 }
702
703 ////////////////////////////////////////////////////////////////////////////////
704 // Standard trait implementations for slices
705 ////////////////////////////////////////////////////////////////////////////////
706
707 #[stable(feature = "rust1", since = "1.0.0")]
708 impl<T> Borrow<[T]> for Vec<T> {
709     fn borrow(&self) -> &[T] {
710         &self[..]
711     }
712 }
713
714 #[stable(feature = "rust1", since = "1.0.0")]
715 impl<T> BorrowMut<[T]> for Vec<T> {
716     fn borrow_mut(&mut self) -> &mut [T] {
717         &mut self[..]
718     }
719 }
720
721 #[stable(feature = "rust1", since = "1.0.0")]
722 impl<T: Clone> ToOwned for [T] {
723     type Owned = Vec<T>;
724     #[cfg(not(test))]
725     fn to_owned(&self) -> Vec<T> {
726         self.to_vec()
727     }
728
729     #[cfg(test)]
730     fn to_owned(&self) -> Vec<T> {
731         hack::to_vec(self)
732     }
733
734     fn clone_into(&self, target: &mut Vec<T>) {
735         // drop anything in target that will not be overwritten
736         target.truncate(self.len());
737
738         // target.len <= self.len due to the truncate above, so the
739         // slices here are always in-bounds.
740         let (init, tail) = self.split_at(target.len());
741
742         // reuse the contained values' allocations/resources.
743         target.clone_from_slice(init);
744         target.extend_from_slice(tail);
745     }
746 }
747
748 ////////////////////////////////////////////////////////////////////////////////
749 // Sorting
750 ////////////////////////////////////////////////////////////////////////////////
751
752 /// Inserts `v[0]` into pre-sorted sequence `v[1..]` so that whole `v[..]` becomes sorted.
753 ///
754 /// This is the integral subroutine of insertion sort.
755 fn insert_head<T, F>(v: &mut [T], is_less: &mut F)
756 where
757     F: FnMut(&T, &T) -> bool,
758 {
759     if v.len() >= 2 && is_less(&v[1], &v[0]) {
760         unsafe {
761             // There are three ways to implement insertion here:
762             //
763             // 1. Swap adjacent elements until the first one gets to its final destination.
764             //    However, this way we copy data around more than is necessary. If elements are big
765             //    structures (costly to copy), this method will be slow.
766             //
767             // 2. Iterate until the right place for the first element is found. Then shift the
768             //    elements succeeding it to make room for it and finally place it into the
769             //    remaining hole. This is a good method.
770             //
771             // 3. Copy the first element into a temporary variable. Iterate until the right place
772             //    for it is found. As we go along, copy every traversed element into the slot
773             //    preceding it. Finally, copy data from the temporary variable into the remaining
774             //    hole. This method is very good. Benchmarks demonstrated slightly better
775             //    performance than with the 2nd method.
776             //
777             // All methods were benchmarked, and the 3rd showed best results. So we chose that one.
778             let mut tmp = mem::ManuallyDrop::new(ptr::read(&v[0]));
779
780             // Intermediate state of the insertion process is always tracked by `hole`, which
781             // serves two purposes:
782             // 1. Protects integrity of `v` from panics in `is_less`.
783             // 2. Fills the remaining hole in `v` in the end.
784             //
785             // Panic safety:
786             //
787             // If `is_less` panics at any point during the process, `hole` will get dropped and
788             // fill the hole in `v` with `tmp`, thus ensuring that `v` still holds every object it
789             // initially held exactly once.
790             let mut hole = InsertionHole { src: &mut *tmp, dest: &mut v[1] };
791             ptr::copy_nonoverlapping(&v[1], &mut v[0], 1);
792
793             for i in 2..v.len() {
794                 if !is_less(&v[i], &*tmp) {
795                     break;
796                 }
797                 ptr::copy_nonoverlapping(&v[i], &mut v[i - 1], 1);
798                 hole.dest = &mut v[i];
799             }
800             // `hole` gets dropped and thus copies `tmp` into the remaining hole in `v`.
801         }
802     }
803
804     // When dropped, copies from `src` into `dest`.
805     struct InsertionHole<T> {
806         src: *mut T,
807         dest: *mut T,
808     }
809
810     impl<T> Drop for InsertionHole<T> {
811         fn drop(&mut self) {
812             unsafe {
813                 ptr::copy_nonoverlapping(self.src, self.dest, 1);
814             }
815         }
816     }
817 }
818
819 /// Merges non-decreasing runs `v[..mid]` and `v[mid..]` using `buf` as temporary storage, and
820 /// stores the result into `v[..]`.
821 ///
822 /// # Safety
823 ///
824 /// The two slices must be non-empty and `mid` must be in bounds. Buffer `buf` must be long enough
825 /// to hold a copy of the shorter slice. Also, `T` must not be a zero-sized type.
826 unsafe fn merge<T, F>(v: &mut [T], mid: usize, buf: *mut T, is_less: &mut F)
827 where
828     F: FnMut(&T, &T) -> bool,
829 {
830     let len = v.len();
831     let v = v.as_mut_ptr();
832     let (v_mid, v_end) = unsafe { (v.add(mid), v.add(len)) };
833
834     // The merge process first copies the shorter run into `buf`. Then it traces the newly copied
835     // run and the longer run forwards (or backwards), comparing their next unconsumed elements and
836     // copying the lesser (or greater) one into `v`.
837     //
838     // As soon as the shorter run is fully consumed, the process is done. If the longer run gets
839     // consumed first, then we must copy whatever is left of the shorter run into the remaining
840     // hole in `v`.
841     //
842     // Intermediate state of the process is always tracked by `hole`, which serves two purposes:
843     // 1. Protects integrity of `v` from panics in `is_less`.
844     // 2. Fills the remaining hole in `v` if the longer run gets consumed first.
845     //
846     // Panic safety:
847     //
848     // If `is_less` panics at any point during the process, `hole` will get dropped and fill the
849     // hole in `v` with the unconsumed range in `buf`, thus ensuring that `v` still holds every
850     // object it initially held exactly once.
851     let mut hole;
852
853     if mid <= len - mid {
854         // The left run is shorter.
855         unsafe {
856             ptr::copy_nonoverlapping(v, buf, mid);
857             hole = MergeHole { start: buf, end: buf.add(mid), dest: v };
858         }
859
860         // Initially, these pointers point to the beginnings of their arrays.
861         let left = &mut hole.start;
862         let mut right = v_mid;
863         let out = &mut hole.dest;
864
865         while *left < hole.end && right < v_end {
866             // Consume the lesser side.
867             // If equal, prefer the left run to maintain stability.
868             unsafe {
869                 let to_copy = if is_less(&*right, &**left) {
870                     get_and_increment(&mut right)
871                 } else {
872                     get_and_increment(left)
873                 };
874                 ptr::copy_nonoverlapping(to_copy, get_and_increment(out), 1);
875             }
876         }
877     } else {
878         // The right run is shorter.
879         unsafe {
880             ptr::copy_nonoverlapping(v_mid, buf, len - mid);
881             hole = MergeHole { start: buf, end: buf.add(len - mid), dest: v_mid };
882         }
883
884         // Initially, these pointers point past the ends of their arrays.
885         let left = &mut hole.dest;
886         let right = &mut hole.end;
887         let mut out = v_end;
888
889         while v < *left && buf < *right {
890             // Consume the greater side.
891             // If equal, prefer the right run to maintain stability.
892             unsafe {
893                 let to_copy = if is_less(&*right.offset(-1), &*left.offset(-1)) {
894                     decrement_and_get(left)
895                 } else {
896                     decrement_and_get(right)
897                 };
898                 ptr::copy_nonoverlapping(to_copy, decrement_and_get(&mut out), 1);
899             }
900         }
901     }
902     // Finally, `hole` gets dropped. If the shorter run was not fully consumed, whatever remains of
903     // it will now be copied into the hole in `v`.
904
905     unsafe fn get_and_increment<T>(ptr: &mut *mut T) -> *mut T {
906         let old = *ptr;
907         *ptr = unsafe { ptr.offset(1) };
908         old
909     }
910
911     unsafe fn decrement_and_get<T>(ptr: &mut *mut T) -> *mut T {
912         *ptr = unsafe { ptr.offset(-1) };
913         *ptr
914     }
915
916     // When dropped, copies the range `start..end` into `dest..`.
917     struct MergeHole<T> {
918         start: *mut T,
919         end: *mut T,
920         dest: *mut T,
921     }
922
923     impl<T> Drop for MergeHole<T> {
924         fn drop(&mut self) {
925             // `T` is not a zero-sized type, so it's okay to divide by its size.
926             let len = (self.end as usize - self.start as usize) / mem::size_of::<T>();
927             unsafe {
928                 ptr::copy_nonoverlapping(self.start, self.dest, len);
929             }
930         }
931     }
932 }
933
934 /// This merge sort borrows some (but not all) ideas from TimSort, which is described in detail
935 /// [here](http://svn.python.org/projects/python/trunk/Objects/listsort.txt).
936 ///
937 /// The algorithm identifies strictly descending and non-descending subsequences, which are called
938 /// natural runs. There is a stack of pending runs yet to be merged. Each newly found run is pushed
939 /// onto the stack, and then some pairs of adjacent runs are merged until these two invariants are
940 /// satisfied:
941 ///
942 /// 1. for every `i` in `1..runs.len()`: `runs[i - 1].len > runs[i].len`
943 /// 2. for every `i` in `2..runs.len()`: `runs[i - 2].len > runs[i - 1].len + runs[i].len`
944 ///
945 /// The invariants ensure that the total running time is `O(n * log(n))` worst-case.
946 fn merge_sort<T, F>(v: &mut [T], mut is_less: F)
947 where
948     F: FnMut(&T, &T) -> bool,
949 {
950     // Slices of up to this length get sorted using insertion sort.
951     const MAX_INSERTION: usize = 20;
952     // Very short runs are extended using insertion sort to span at least this many elements.
953     const MIN_RUN: usize = 10;
954
955     // Sorting has no meaningful behavior on zero-sized types.
956     if size_of::<T>() == 0 {
957         return;
958     }
959
960     let len = v.len();
961
962     // Short arrays get sorted in-place via insertion sort to avoid allocations.
963     if len <= MAX_INSERTION {
964         if len >= 2 {
965             for i in (0..len - 1).rev() {
966                 insert_head(&mut v[i..], &mut is_less);
967             }
968         }
969         return;
970     }
971
972     // Allocate a buffer to use as scratch memory. We keep the length 0 so we can keep in it
973     // shallow copies of the contents of `v` without risking the dtors running on copies if
974     // `is_less` panics. When merging two sorted runs, this buffer holds a copy of the shorter run,
975     // which will always have length at most `len / 2`.
976     let mut buf = Vec::with_capacity(len / 2);
977
978     // In order to identify natural runs in `v`, we traverse it backwards. That might seem like a
979     // strange decision, but consider the fact that merges more often go in the opposite direction
980     // (forwards). According to benchmarks, merging forwards is slightly faster than merging
981     // backwards. To conclude, identifying runs by traversing backwards improves performance.
982     let mut runs = vec![];
983     let mut end = len;
984     while end > 0 {
985         // Find the next natural run, and reverse it if it's strictly descending.
986         let mut start = end - 1;
987         if start > 0 {
988             start -= 1;
989             unsafe {
990                 if is_less(v.get_unchecked(start + 1), v.get_unchecked(start)) {
991                     while start > 0 && is_less(v.get_unchecked(start), v.get_unchecked(start - 1)) {
992                         start -= 1;
993                     }
994                     v[start..end].reverse();
995                 } else {
996                     while start > 0 && !is_less(v.get_unchecked(start), v.get_unchecked(start - 1))
997                     {
998                         start -= 1;
999                     }
1000                 }
1001             }
1002         }
1003
1004         // Insert some more elements into the run if it's too short. Insertion sort is faster than
1005         // merge sort on short sequences, so this significantly improves performance.
1006         while start > 0 && end - start < MIN_RUN {
1007             start -= 1;
1008             insert_head(&mut v[start..end], &mut is_less);
1009         }
1010
1011         // Push this run onto the stack.
1012         runs.push(Run { start, len: end - start });
1013         end = start;
1014
1015         // Merge some pairs of adjacent runs to satisfy the invariants.
1016         while let Some(r) = collapse(&runs) {
1017             let left = runs[r + 1];
1018             let right = runs[r];
1019             unsafe {
1020                 merge(
1021                     &mut v[left.start..right.start + right.len],
1022                     left.len,
1023                     buf.as_mut_ptr(),
1024                     &mut is_less,
1025                 );
1026             }
1027             runs[r] = Run { start: left.start, len: left.len + right.len };
1028             runs.remove(r + 1);
1029         }
1030     }
1031
1032     // Finally, exactly one run must remain in the stack.
1033     debug_assert!(runs.len() == 1 && runs[0].start == 0 && runs[0].len == len);
1034
1035     // Examines the stack of runs and identifies the next pair of runs to merge. More specifically,
1036     // if `Some(r)` is returned, that means `runs[r]` and `runs[r + 1]` must be merged next. If the
1037     // algorithm should continue building a new run instead, `None` is returned.
1038     //
1039     // TimSort is infamous for its buggy implementations, as described here:
1040     // http://envisage-project.eu/timsort-specification-and-verification/
1041     //
1042     // The gist of the story is: we must enforce the invariants on the top four runs on the stack.
1043     // Enforcing them on just top three is not sufficient to ensure that the invariants will still
1044     // hold for *all* runs in the stack.
1045     //
1046     // This function correctly checks invariants for the top four runs. Additionally, if the top
1047     // run starts at index 0, it will always demand a merge operation until the stack is fully
1048     // collapsed, in order to complete the sort.
1049     #[inline]
1050     fn collapse(runs: &[Run]) -> Option<usize> {
1051         let n = runs.len();
1052         if n >= 2
1053             && (runs[n - 1].start == 0
1054                 || runs[n - 2].len <= runs[n - 1].len
1055                 || (n >= 3 && runs[n - 3].len <= runs[n - 2].len + runs[n - 1].len)
1056                 || (n >= 4 && runs[n - 4].len <= runs[n - 3].len + runs[n - 2].len))
1057         {
1058             if n >= 3 && runs[n - 3].len < runs[n - 1].len { Some(n - 3) } else { Some(n - 2) }
1059         } else {
1060             None
1061         }
1062     }
1063
1064     #[derive(Clone, Copy)]
1065     struct Run {
1066         start: usize,
1067         len: usize,
1068     }
1069 }