containers_rs/
interfaces.rs

1use crate::generic_containers::*;
2use core::borrow::Borrow;
3use core::ops::{Index, IndexMut};
4
5/// Trait for a container indexed by a value that implements `Copy` and `Eq`.
6pub trait CopyMap<K, V>: Container
7where
8    K: Copy + Eq,
9{
10    /// Get a reference to a value from this map.
11    ///
12    /// Takes a key by value
13    /// and returns a reference to the corresponding data,
14    /// or `None` if the key doesn't exist in the map.
15    fn get(&self, key: K) -> Option<&V>;
16
17    /// Get a mutable reference to a value from this map.
18    ///
19    /// Takes a key by value
20    /// and returns a mutable reference to the corresponding data,
21    /// or `None` if the key doesn't exist in the map.
22    fn get_mut(&mut self, key: K) -> Option<&mut V>;
23
24    /// Inserts a key-value pair into the map.
25    ///
26    /// If the map did not have this key present, None is returned.
27    ///
28    /// If the map did have this key present, the value is updated, and the old
29    /// value is returned. Note that the key itself isn't necessarily updated.
30    fn insert(&mut self, k: K, v: V) -> Option<V>;
31}
32
33/// Trait for a container indexed by a value that implements `Eq`.
34pub trait Map<K, V>: Container
35where
36    K: Eq,
37{
38    /// Returns a reference to a value from this Map.
39    ///
40    /// Takes a key by reference
41    /// and returns a reference to the corresponding data,
42    /// or `None` if the key doesn't exist in the map.
43    fn get<Q: ?Sized>(&self, key: &Q) -> Option<&V>
44    where
45        K: Borrow<Q>,
46        Q: Eq;
47
48    /// Returns a mutable reference to a value in this map.
49    ///
50    /// Takes a key by reference
51    /// and returns a mutable reference to the corresponding data,
52    /// or `None` if the key doesn't exist in the map.
53    fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut V>
54    where
55        K: Borrow<Q>,
56        Q: Eq;
57
58    /// Inserts a key-value pair into the map.
59    ///
60    /// If the map did not have this key present, None is returned.
61    ///
62    /// If the map did have this key present, the value is updated, and the old
63    /// value is returned. Note that the key itself isn't necessarily updated.
64    fn insert(&mut self, k: K, v: V) -> Option<V>;
65}
66
67/// Key-value map that can dynamically change size, indexed by a key that implements
68/// `Copy`.
69pub trait CopyDictionary<K, V>: CopyMap<K, V> + DynamicContainer
70where
71    K: Copy + Eq,
72{
73    /// Returns true if this container contains the key.
74    fn contains(&self, key: K) -> bool {
75        match self.get(key) {
76            Some(_) => true,
77            None => false,
78        }
79    }
80
81    /// Removes an item from this container using the associated key,
82    /// and returns it (if it existed).
83    fn remove(&mut self, k: K) -> Option<V>;
84}
85
86/// Key-value map that can dynamically change size.
87pub trait Dictionary<K, V>: Map<K, V> + DynamicContainer
88where
89    K: Eq,
90{
91    /// Returns true if this container contains the key.
92    fn contains<Q: ?Sized>(&self, key: &Q) -> bool
93    where
94        K: Borrow<Q>,
95        Q: Eq,
96    {
97        match self.get(key) {
98            Some(_) => true,
99            None => false,
100        }
101    }
102
103    /// Removes an item from this container using the associated key,
104    /// and returns it (if it existed).
105    fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<V>
106    where
107        K: Borrow<Q>,
108        Q: Eq;
109}
110
111/// Statically-sized array of values.
112pub trait Array<V>:
113    CopyMap<usize, V> + Index<usize, Output = V> + IndexMut<usize, Output = V>
114{
115}
116
117/// Dynamically-sized array of values.
118pub trait DynamicArray<V>:
119    CopyMap<usize, V> + DynamicContainer + Index<usize, Output = V> + IndexMut<usize, Output = V>
120{
121}