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