into_sorted/stable/
methods.rs

1use super::functions::*;
2use core::cmp::Ordering;
3
4/// Utility methods to sort various types of arrays with a stable algorithm (requires allocation).
5pub trait IntoSorted<Item>: crate::sealed::IsArray<Item> {
6    /// Sort an array by [`Ord`] and return it.
7    ///
8    /// This function calls [`slice::sort`] under the hook.
9    ///
10    /// [`slice::sort`]: https://doc.rust-lang.org/std/primitive.slice.html#method.sort
11    fn into_sorted(self) -> Self
12    where
13        Item: Ord;
14
15    /// Sort an array by a function and return it.
16    ///
17    /// This function calls [`slice::sort_by`] under the hook.
18    ///
19    /// [`slice::sort_by`]: https://doc.rust-lang.org/std/primitive.slice.html#method.sort_by
20    fn into_sorted_by<Order>(self, order: Order) -> Self
21    where
22        Order: FnMut(&Item, &Item) -> Ordering;
23
24    /// Sort an array by a key extraction function and return it.
25    ///
26    /// This function calls [`slice::sort_by_key`] under the hook.
27    ///
28    /// [`slice::sort_by_key`]: https://doc.rust-lang.org/std/primitive.slice.html#method.sort_by_key
29    fn into_sorted_by_key<Key, GetKey>(self, get_key: GetKey) -> Self
30    where
31        GetKey: FnMut(&Item) -> Key,
32        Key: Ord;
33
34    /// Sort an array by a key extraction function and return it.
35    ///
36    /// This function calls [`slice::sort_by_cached_key`] under the hook.
37    ///
38    /// [`slice::sort_by_cached_key`]: https://doc.rust-lang.org/std/primitive.slice.html#method.sort_by_cached_key
39    fn into_sorted_by_cached_key<Key, GetKey>(self, get_key: GetKey) -> Self
40    where
41        GetKey: FnMut(&Item) -> Key,
42        Key: Ord;
43}
44
45impl<Item, Array> IntoSorted<Item> for Array
46where
47    Array: AsMut<[Item]> + Sized,
48{
49    #[inline]
50    fn into_sorted(self) -> Self
51    where
52        Item: Ord,
53    {
54        into_sorted(self)
55    }
56
57    #[inline]
58    fn into_sorted_by<Order>(self, order: Order) -> Self
59    where
60        Order: FnMut(&Item, &Item) -> Ordering,
61    {
62        into_sorted_by(self, order)
63    }
64
65    #[inline]
66    fn into_sorted_by_key<Key, GetKey>(self, get_key: GetKey) -> Self
67    where
68        GetKey: FnMut(&Item) -> Key,
69        Key: Ord,
70    {
71        into_sorted_by_key(self, get_key)
72    }
73
74    #[inline]
75    fn into_sorted_by_cached_key<Key, GetKey>(self, get_key: GetKey) -> Self
76    where
77        GetKey: FnMut(&Item) -> Key,
78        Key: Ord,
79    {
80        into_sorted_by_cached_key(self, get_key)
81    }
82}