push_trait/
ordered.rs

1//! Traits for pushing data to the front or back of a collection.
2
3use len_trait::LenMut;
4
5use base::Push;
6
7/// A trait for moving data onto the back of a collection.
8///
9/// Some collections have a particular location onto which new values are pushed, and the "back"
10/// refers to this canonical location. If you don't care where values are pushed, you should use
11/// [`Push`] by itself instead.
12///
13/// Collections which have two locations to push values should also implement [`PushFront`].
14///
15/// [`Push`]: ../base/trait.Push.html
16/// [`PushFront`]: trait.PushFront.html
17pub trait PushBack<T>: Push<T> {
18    /// Pushes the value onto the back, yielding the value that was pushed out, if any.
19    fn push_back(&mut self, val: T) -> Option<Self::PushedOut> {
20        self.push(val)
21    }
22}
23
24/// A trait for moving data onto the front of a collection.
25///
26/// Some collections have two locations to push values; for these collections, we assign one
27/// location as the "back" and the other as the "front." See [`PushBack`] for more information, or
28/// use [`Push`] by itself if you don't care where values are pushed.
29///
30/// [`PushBack`]: trait.PushBack.html
31/// [`Push`]: ../base/trait.Push.html
32pub trait PushFront<T>: PushBack<T> {
33    /// Pushes the value onto the front, yielding the value that was pushed out, if any.
34    fn push_front(&mut self, val: T) -> Option<Self::PushedOut>;
35}
36
37/// A trait for moving data to a specific index within a collection.
38///
39/// Unlike [`Push`], insertions must take a linear amount of time and space with respect to the
40/// length of both the collection and the inserted item.
41///
42/// [`Push`]: ../base/trait.Push.html
43pub trait Insert<T>: LenMut + PushBack<T> {
44    /// Inserts the value at the given position, yielding the value that was pushed out, if any.
45    fn insert(&mut self, index: usize, val: T) -> Option<Self::PushedOut>;
46}