1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//! [`std::vec::Vec`] related extensions.

use std::cmp::Ordering;

/// Extension methods for [`std::vec::Vec`].
pub trait VecExt<T> {
    /// Same behaviour as [`Vec::sort`][vec_sort],
    /// but returns itself in a form consistent with chained calls.
    ///
    /// [vec_sort]: std::vec::Vec#method.sort
    fn sorted(self) -> Self
    where
        T: Ord;

    /// Same behaviour as [`Vec::sort_by`][vec_sort_by],
    /// but returns itself in a form consistent with chained calls.
    ///
    /// [vec_sort_by]: std::vec::Vec#method.sort_by
    fn sorted_by(self, compare: impl FnMut(&T, &T) -> Ordering) -> Self;

    /// Same behaviour as [`Vec::sort_by_key`][vec_sort_by_key],
    /// but returns itself in a form consistent with chained calls.
    ///
    /// [vec_sort_by_key]: std::vec::Vec#method.sort_by_key
    fn sorted_by_key<K>(self, f: impl FnMut(&T) -> K) -> Self
    where
        K: Ord;

    /// Same behaviour as [`Vec::sort_by_cached_key`][vec_sort_by_cached_key],
    /// but returns itself in a form consistent with chained calls.
    ///
    /// [vec_sort_by_cached_key]: std::vec::Vec#method.sort_by_cached_key
    fn sorted_by_cached_key<K>(self, f: impl FnMut(&T) -> K) -> Self
    where
        K: Ord;

    /// Same behaviour as [`Vec::sort_unstable`][vec_sort_unstable],
    /// but returns itself in a form consistent with chained calls.
    ///
    /// [vec_sort_unstable]: std::vec::Vec#method.sort_unstable
    fn sorted_unstable(self) -> Self
    where
        T: Ord;

    /// Same behaviour as [`Vec::sort_unstable_by`][vec_sort_unstable_by],
    /// but returns itself in a form consistent with chained calls.
    ///
    /// [vec_sort_unstable_by]: std::vec::Vec#method.sort_unstable_by
    fn sorted_unstable_by(self, compare: impl FnMut(&T, &T) -> Ordering) -> Self;

    /// Same behaviour as [`Vec::sort_unstable_by_key`][vec_sort_unstable_by_key],
    /// but returns itself in a form consistent with chained calls.
    ///
    /// [vec_sort_unstable_by_key]: std::vec::Vec#method.sort_unstable_by_key
    fn sorted_unstable_by_key<K>(self, f: impl FnMut(&T) -> K) -> Self
    where
        K: Ord;

    /// Same behaviour as [`Vec::reverse`][vec_reverse],
    /// but returns itself in a form consistent with chained calls.
    ///
    /// [vec_reverse]: std::vec::Vec#method.reverse
    fn reversed(self) -> Self;
}

impl<T> VecExt<T> for Vec<T> {
    fn sorted(mut self) -> Self
    where
        T: Ord,
    {
        self.sort();
        self
    }

    fn sorted_by(mut self, compare: impl FnMut(&T, &T) -> Ordering) -> Self {
        self.sort_by(compare);
        self
    }

    fn sorted_by_key<K>(mut self, f: impl FnMut(&T) -> K) -> Self
    where
        K: Ord,
    {
        self.sort_by_key(f);
        self
    }

    fn sorted_by_cached_key<K>(mut self, f: impl FnMut(&T) -> K) -> Self
    where
        K: Ord,
    {
        self.sort_by_cached_key(f);
        self
    }

    fn sorted_unstable(mut self) -> Self
    where
        T: Ord,
    {
        self.sort_unstable();
        self
    }

    fn sorted_unstable_by(mut self, compare: impl FnMut(&T, &T) -> Ordering) -> Self {
        self.sort_unstable_by(compare);
        self
    }

    fn sorted_unstable_by_key<K>(mut self, f: impl FnMut(&T) -> K) -> Self
    where
        K: Ord,
    {
        self.sort_unstable_by_key(f);
        self
    }

    fn reversed(mut self) -> Self {
        self.reverse();
        self
    }
}