into_sorted/stable/
functions.rs

1use core::cmp::Ordering;
2
3/// Sort an array by [`Ord`] and return it.
4///
5/// This function calls [`slice::sort`] under the hook.
6///
7/// [`slice::sort`]: https://doc.rust-lang.org/std/primitive.slice.html#method.sort
8#[inline]
9pub fn into_sorted<Item, Array>(mut array: Array) -> Array
10where
11    Array: AsMut<[Item]>,
12    Item: Ord,
13{
14    array.as_mut().sort();
15    array
16}
17
18/// Sort an array by a function and return it.
19///
20/// This function calls [`slice::sort_by`] under the hook.
21///
22/// [`slice::sort_by`]: https://doc.rust-lang.org/std/primitive.slice.html#method.sort_by
23#[inline]
24pub fn into_sorted_by<Item, Array, Order>(mut array: Array, order: Order) -> Array
25where
26    Array: AsMut<[Item]>,
27    Order: FnMut(&Item, &Item) -> Ordering,
28{
29    array.as_mut().sort_by(order);
30    array
31}
32
33/// Sort an array by a key extraction function and return it.
34///
35/// This function calls [`slice::sort_by_key`] under the hook.
36///
37/// [`slice::sort_by_key`]: https://doc.rust-lang.org/std/primitive.slice.html#method.sort_by_key
38#[inline]
39pub fn into_sorted_by_key<Item, Array, Key, GetKey>(mut array: Array, get_key: GetKey) -> Array
40where
41    Array: AsMut<[Item]>,
42    GetKey: FnMut(&Item) -> Key,
43    Key: Ord,
44{
45    array.as_mut().sort_by_key(get_key);
46    array
47}
48
49/// Sort an array by a key extraction function (which would be called at most once per element) and return it.
50///
51/// This function calls [`slice::sort_by_cached_key`] under the hook.
52///
53/// [`slice::sort_by_cached_key`]: https://doc.rust-lang.org/std/primitive.slice.html#method.sort_by_cached_key
54#[inline]
55pub fn into_sorted_by_cached_key<Item, Array, Key, GetKey>(
56    mut array: Array,
57    get_key: GetKey,
58) -> Array
59where
60    Array: AsMut<[Item]>,
61    GetKey: FnMut(&Item) -> Key,
62    Key: Ord,
63{
64    array.as_mut().sort_by_cached_key(get_key);
65    array
66}