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