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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#![cfg_attr(test, feature(plugin))]
#![cfg_attr(test, plugin(clippy))]
#![cfg_attr(feature = "nightly", feature(never_type))]

#[cfg(feature = "nightly")]
type Nothing = !;

/// Represents that no value is ever pushed out on a push.
///
/// Currently, the `!` type is only available on nightly. As a result, when the `nightly` feature
/// isn't enabled, this enum is used in place of `!` to represent that no object can ever be pushed
/// out when a push happens.
#[cfg(not(feature = "nightly"))]
pub enum Nothing {}

use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque};
use std::hash::Hash;

/// A mutable collection onto which items can be added to an arbitrary location in the collection.
pub trait Push<T> {
    /// Type of value that gets pushed out, if any.
    ///
    /// For some collections, a value may be "pushed out" after a value is pushed.
    type PushedOut;

    /// Adds the value to the collection.
    fn push(&mut self, val: T) -> Option<Self::PushedOut>;
}

/// A mutable collection onto which items can be added "at the beginning."
///
/// "At the beginning" is defined in a way such that truncating the collection to the item's length
/// after a push will retain the item.
pub trait PushFront<T>: Push<T> {
    /// Adds the value to the front of the collection.
    fn push_front(&mut self, val: T) -> Option<Self::PushedOut>;
}

/// A mutable collection onto which items can be added "at the end."
///
/// "At the end" is defined in a way such that truncating the collection down by the item's length
/// after a push will remove the item.
pub trait PushBack<T>: Push<T> {
    /// Adds the value to the back of the collection.
    fn push_back(&mut self, val: T) -> Option<Self::PushedOut>;
}

/// Alternative to `Push<&T>`.
pub trait PushRef<T: ?Sized> {
    /// Type of value that gets pushed out, if any.
    type PushedOut;

    /// Adds the value to the collection.
    fn push_ref(&mut self, val: &T) -> Option<Self::PushedOut>;
}

/// Alternative to `PushFront<&T>`.
pub trait PushRefFront<T: ?Sized>: PushRef<T> {
    /// Adds the value to the front of the collection.
    fn push_ref_front(&mut self, val: &T) -> Option<Self::PushedOut>;
}

/// Alternative to `PushBack<&T>`.
pub trait PushRefBack<T: ?Sized>: PushRef<T> {
    /// Adds the value to the back of the collection.
    fn push_ref_back(&mut self, val: &T) -> Option<Self::PushedOut>;
}

/// Represents a pushed value that has been dropped.
///
/// Some collections do not actually push a value if another value exists in this place. These
/// collections will return `Some(PushedVal)` to represent that, even though the pushed value was
/// dropped, it was "pushed out" instead of being pushed.
pub struct PushedVal;

impl<K: Ord, V> Push<(K, V)> for BTreeMap<K, V> {
    type PushedOut = V;
    fn push(&mut self, (key, val): (K, V)) -> Option<V> {
        BTreeMap::insert(self, key, val)
    }
}

impl<T: Ord> Push<T> for BTreeSet<T> {
    type PushedOut = PushedVal;
    fn push(&mut self, val: T) -> Option<PushedVal> {
        if BTreeSet::insert(self, val) {
            None
        } else {
            Some(PushedVal)
        }
    }
}

impl<T: Ord> Push<T> for BinaryHeap<T> {
    type PushedOut = Nothing;
    fn push(&mut self, val: T) -> Option<Nothing> {
        BinaryHeap::push(self, val);
        None
    }
}

impl<K: Hash + Eq, V> Push<(K, V)> for HashMap<K, V> {
    type PushedOut = V;
    fn push(&mut self, (key, val): (K, V)) -> Option<V> {
        HashMap::insert(self, key, val)
    }
}

impl<T: Hash + Eq> Push<T> for HashSet<T> {
    type PushedOut = PushedVal;
    fn push(&mut self, val: T) -> Option<PushedVal> {
        if HashSet::insert(self, val) {
            None
        } else {
            Some(PushedVal)
        }
    }
}

impl<T> Push<T> for LinkedList<T> {
    type PushedOut = Nothing;
    fn push(&mut self, val: T) -> Option<Nothing> {
        LinkedList::push_back(self, val);
        None
    }
}

impl<T> Push<LinkedList<T>> for LinkedList<T> {
    type PushedOut = Nothing;
    fn push(&mut self, mut val: LinkedList<T>) -> Option<Nothing> {
        LinkedList::append(self, &mut val);
        None
    }
}

impl Push<char> for String {
    type PushedOut = Nothing;
    fn push(&mut self, val: char) -> Option<Nothing> {
        String::push(self, val);
        None
    }
}

impl PushRef<str> for String {
    type PushedOut = Nothing;
    fn push_ref(&mut self, val: &str) -> Option<Nothing> {
        String::push_str(self, val);
        None
    }
}

impl<T> Push<T> for Vec<T> {
    type PushedOut = Nothing;
    fn push(&mut self, val: T) -> Option<Nothing> {
        Vec::push(self, val);
        None
    }
}

impl<T: Clone> PushRef<[T]> for Vec<T> {
    type PushedOut = Nothing;
    fn push_ref(&mut self, val: &[T]) -> Option<Nothing> {
        Vec::extend_from_slice(self, val);
        None
    }
}

impl<T> Push<T> for VecDeque<T> {
    type PushedOut = Nothing;
    fn push(&mut self, val: T) -> Option<Nothing> {
        VecDeque::push_back(self, val);
        None
    }
}

impl<T> PushFront<T> for LinkedList<T> {
    fn push_front(&mut self, val: T) -> Option<Nothing> {
        LinkedList::push_front(self, val);
        None
    }
}

impl<T> PushBack<T> for LinkedList<T> {
    fn push_back(&mut self, val: T) -> Option<Nothing> {
        LinkedList::push_back(self, val);
        None
    }
}

impl<T> PushFront<LinkedList<T>> for LinkedList<T> {
    fn push_front(&mut self, mut val: LinkedList<T>) -> Option<Nothing> {
        LinkedList::append(&mut val, self);
        *self = val;
        None
    }
}

impl<T> PushBack<LinkedList<T>> for LinkedList<T> {
    fn push_back(&mut self, mut val: LinkedList<T>) -> Option<Nothing> {
        LinkedList::append(self, &mut val);
        None
    }
}

impl PushBack<char> for String {
    fn push_back(&mut self, val: char) -> Option<Nothing> {
        String::push(self, val);
        None
    }
}

impl PushRefBack<str> for String {
    fn push_ref_back(&mut self, val: &str) -> Option<Nothing> {
        String::push_str(self, val);
        None
    }
}

impl<T> PushBack<T> for Vec<T> {
    fn push_back(&mut self, val: T) -> Option<Nothing> {
        Vec::push(self, val);
        None
    }
}

impl<T: Clone> PushRefBack<[T]> for Vec<T> {
    fn push_ref_back(&mut self, val: &[T]) -> Option<Nothing> {
        Vec::extend_from_slice(self, val);
        None
    }
}

impl<T> PushFront<T> for VecDeque<T> {
    fn push_front(&mut self, val: T) -> Option<Nothing> {
        VecDeque::push_front(self, val);
        None
    }
}

impl<T> PushBack<T> for VecDeque<T> {
    fn push_back(&mut self, val: T) -> Option<Nothing> {
        VecDeque::push_back(self, val);
        None
    }
}