stupid_add/
lib.rs

1use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque};
2use std::hash::Hash;
3
4pub trait Add {
5  fn add(self, rhs: Self) -> Self;
6}
7
8impl Add for String {
9  fn add(self, rhs: String) -> String {
10    self + &rhs
11  }
12}
13
14impl Add for bool {
15  fn add(self, rhs: bool) -> bool {
16    self || rhs
17  }
18}
19
20impl Add for char {
21  fn add(self, rhs: char) -> char {
22    let new_char = self as u32 + rhs as u32;
23    std::char::from_u32(new_char).unwrap()
24  }
25}
26
27impl Add for u8 {
28  fn add(self, rhs: u8) -> u8 {
29    self + rhs
30  }
31}
32
33impl Add for u16 {
34  fn add(self, rhs: u16) -> u16 {
35    self + rhs
36  }
37}
38
39impl Add for u32 {
40  fn add(self, rhs: u32) -> u32 {
41    self + rhs
42  }
43}
44
45impl Add for u64 {
46  fn add(self, rhs: u64) -> u64 {
47    self + rhs
48  }
49}
50
51impl Add for u128 {
52  fn add(self, rhs: u128) -> u128 {
53    self + rhs
54  }
55}
56
57impl Add for usize {
58  fn add(self, rhs: usize) -> usize {
59    self + rhs
60  }
61}
62
63impl Add for i8 {
64  fn add(self, rhs: i8) -> i8 {
65    self + rhs
66  }
67}
68
69impl Add for i16 {
70  fn add(self, rhs: i16) -> i16 {
71    self + rhs
72  }
73}
74
75impl Add for i32 {
76  fn add(self, rhs: i32) -> i32 {
77    self + rhs
78  }
79}
80
81impl Add for i64 {
82  fn add(self, rhs: i64) -> i64 {
83    self + rhs
84  }
85}
86
87impl Add for i128 {
88  fn add(self, rhs: i128) -> i128 {
89    self + rhs
90  }
91}
92
93impl Add for isize {
94  fn add(self, rhs: isize) -> isize {
95    self + rhs
96  }
97}
98
99impl Add for f32 {
100  fn add(self, rhs: f32) -> f32 {
101    self + rhs
102  }
103}
104
105impl Add for f64 {
106  fn add(self, rhs: f64) -> f64 {
107    self + rhs
108  }
109}
110
111impl<T> Add for Vec<T>
112where
113  T: Add,
114{
115  fn add(mut self, rhs: Vec<T>) -> Vec<T> {
116    self.extend(rhs);
117    self
118  }
119}
120
121impl<T> Add for LinkedList<T>
122where
123  T: Add,
124{
125  fn add(mut self, rhs: LinkedList<T>) -> LinkedList<T> {
126    self.extend(rhs);
127    self
128  }
129}
130
131impl<T> Add for VecDeque<T>
132where
133  T: Add,
134{
135  fn add(mut self, rhs: VecDeque<T>) -> VecDeque<T> {
136    self.extend(rhs);
137    self
138  }
139}
140
141// impl<T> Add for BinaryHeap<T>
142// where
143//   T: Add + Ord,
144// {
145//   fn add(mut self, rhs: BinaryHeap<T>) -> BinaryHeap<T> {
146//     self.extend(rhs);
147//     self
148//   }
149// }
150
151impl<T> Add for HashSet<T>
152where
153  T: Add + Eq + Hash,
154{
155  fn add(mut self, rhs: HashSet<T>) -> HashSet<T> {
156    self.extend(rhs);
157    self
158  }
159}
160
161impl<T> Add for BTreeSet<T>
162where
163  T: Add + Ord,
164{
165  fn add(mut self, rhs: BTreeSet<T>) -> BTreeSet<T> {
166    self.extend(rhs);
167    self
168  }
169}
170
171impl<K, V> Add for HashMap<K, V>
172where
173  K: Add + Eq + Hash,
174  V: Add,
175{
176  fn add(mut self, rhs: HashMap<K, V>) -> HashMap<K, V> {
177    self.extend(rhs);
178    self
179  }
180}
181
182impl<K, V> Add for BTreeMap<K, V>
183where
184  K: Add + Ord,
185  V: Add,
186{
187  fn add(mut self, rhs: BTreeMap<K, V>) -> BTreeMap<K, V> {
188    self.extend(rhs);
189    self
190  }
191}
192
193impl<T> Add for Option<T>
194where
195  T: Add + std::ops::Add<Output = T>,
196{
197  fn add(self, rhs: Option<T>) -> Option<T> {
198    match (self, rhs) {
199      (Some(a), Some(b)) => Some(a + b),
200      (Some(a), None) => Some(a),
201      (None, Some(b)) => Some(b),
202      (None, None) => None,
203    }
204  }
205}
206
207impl Add for () {
208  fn add(self, _: ()) -> () {
209    ()
210  }
211}
212
213#[cfg(test)]
214mod tests {
215  use super::*;
216
217  #[test]
218  fn add_string() {
219    assert_eq!(
220      String::from("hello ").add("world".to_owned()),
221      "hello world"
222    );
223  }
224
225  #[test]
226  fn add_bool() {
227    assert_eq!(true.add(true), true);
228    assert_eq!(true.add(false), true);
229    assert_eq!(false.add(true), true);
230    assert_eq!(false.add(false), false);
231  }
232
233  #[test]
234  fn add_char() {
235    assert_eq!(char::from('a').add('b'), 'Ã');
236  }
237
238  #[test]
239  fn add_u8() {
240    assert_eq!(u8::from(1_u8).add(2), 3);
241  }
242
243  #[test]
244  fn add_u16() {
245    assert_eq!(u16::from(1_u16).add(2), 3);
246  }
247
248  #[test]
249  fn add_u32() {
250    assert_eq!(u32::from(1_u32).add(2), 3);
251  }
252
253  #[test]
254  fn add_u64() {
255    assert_eq!(u64::from(1_u64).add(2), 3);
256  }
257
258  #[test]
259  fn add_u128() {
260    assert_eq!(u128::from(1_u128).add(2), 3);
261  }
262
263  #[test]
264  fn add_usize() {
265    assert_eq!(usize::from(1_usize).add(2), 3);
266  }
267
268  #[test]
269  fn add_i8() {
270    assert_eq!(i8::from(1_i8).add(2), 3);
271  }
272
273  #[test]
274  fn add_i16() {
275    assert_eq!(i16::from(1_i16).add(2), 3);
276  }
277
278  #[test]
279  fn add_i32() {
280    assert_eq!(i32::from(1_i32).add(2), 3);
281  }
282
283  #[test]
284  fn add_i64() {
285    assert_eq!(i64::from(1_i64).add(2), 3);
286  }
287
288  #[test]
289  fn add_i128() {
290    assert_eq!(i128::from(1_i128).add(2), 3);
291  }
292
293  #[test]
294  fn add_isize() {
295    assert_eq!(isize::from(1_isize).add(2), 3);
296  }
297
298  #[test]
299  fn add_f32() {
300    assert_eq!(f32::from(1.0_f32).add(2.0), 3.0);
301  }
302
303  #[test]
304  fn add_f64() {
305    assert_eq!(f64::from(1.0_f64).add(2.0), 3.0);
306  }
307
308  #[test]
309  fn add_vec() {
310    assert_eq!(vec![1, 2].add(vec![3, 4]), vec![1, 2, 3, 4]);
311  }
312
313  #[test]
314  fn add_linked_list() {
315    let mut list = LinkedList::new();
316    list.push_back(1);
317    list.push_back(2);
318
319    let mut other_list = LinkedList::new();
320    other_list.push_back(3);
321    other_list.push_back(4);
322
323    assert_eq!(list.add(other_list), LinkedList::from([1, 2, 3, 4]));
324  }
325
326  #[test]
327  fn add_vec_deque() {
328    let mut deque = VecDeque::new();
329    deque.push_back(1);
330    deque.push_back(2);
331
332    let mut other_deque = VecDeque::new();
333    other_deque.push_back(3);
334    other_deque.push_back(4);
335
336    assert_eq!(deque.add(other_deque), vec![1, 2, 3, 4]);
337  }
338
339  // #[test]
340  // fn add_binary_heap() {
341  //   let mut heap = BinaryHeap::new();
342  //   heap.push(1);
343  //   heap.push(2);
344
345  //   let mut other_heap = BinaryHeap::new();
346  //   other_heap.push(3);
347  //   other_heap.push(4);
348
349  //   let mut result = BinaryHeap::new();
350  //   result.push(1);
351  //   result.push(2);
352  //   result.push(3);
353  //   result.push(4);
354  //   assert_eq!(heap.add(other_heap), result);
355  // }
356
357  #[test]
358  fn add_hash_set() {
359    let mut set = HashSet::new();
360    set.insert(1);
361    set.insert(2);
362
363    let mut other_set = HashSet::new();
364    other_set.insert(3);
365    other_set.insert(4);
366
367    assert_eq!(set.add(other_set), HashSet::from([1, 2, 3, 4]));
368  }
369
370  #[test]
371  fn add_btree_set() {
372    let mut set = BTreeSet::new();
373    set.insert(1);
374    set.insert(2);
375
376    let mut other_set = BTreeSet::new();
377    other_set.insert(3);
378    other_set.insert(4);
379
380    assert_eq!(set.add(other_set), BTreeSet::from([1, 2, 3, 4]));
381  }
382
383  #[test]
384  fn add_hash_map() {
385    let mut map = HashMap::new();
386    map.insert(1, 2);
387    map.insert(2, 3);
388
389    let mut other_map = HashMap::new();
390    other_map.insert(3, 4);
391    other_map.insert(4, 5);
392
393    let mut expected = HashMap::new();
394    expected.insert(1, 2);
395    expected.insert(2, 3);
396    expected.insert(3, 4);
397    expected.insert(4, 5);
398
399    assert_eq!(map.add(other_map), expected);
400  }
401
402  #[test]
403  fn add_btree_map() {
404    let mut map = BTreeMap::new();
405    map.insert(1, 2);
406    map.insert(2, 3);
407
408    let mut other_map = BTreeMap::new();
409    other_map.insert(3, 4);
410    other_map.insert(4, 5);
411
412    let mut expected = BTreeMap::new();
413    expected.insert(1, 2);
414    expected.insert(2, 3);
415    expected.insert(3, 4);
416    expected.insert(4, 5);
417
418    assert_eq!(map.add(other_map), expected);
419  }
420
421  #[test]
422  fn add_option() {
423    assert_eq!(Some(1).add(Some(2)), Some(3));
424    assert_eq!(Some(1).add(None), Some(1));
425    assert_eq!(None::<i32>.add(Some(2)), Some(2));
426    assert_eq!(None::<i32>.add(None), None);
427  }
428
429  #[test]
430  fn add_unit() {
431    assert_eq!(().add(()), ());
432  }
433}