valord_map/entry/mod.rs
1mod raw;
2pub use raw::RawEntry;
3
4use crate::OrdBy;
5use std::hash::Hash;
6
7/// Entry for an existing key-value pair in an [`ValordMap`][crate::ValordMap]
8/// or a vacant location to insert one.
9pub enum Entry<'v, T, K, V>
10where
11 T: Ord + Clone,
12 K: Hash + Eq,
13 V: OrdBy<Target = T>,
14{
15 /// Existing slot with equivalent key.
16 Occupied(RawEntry<'v, T, K, V>),
17 /// Vacant slot (no equivalent key in the map).
18 Vacant(RawEntry<'v, T, K, V>),
19}
20
21impl<'v, T, K, V> Entry<'v, T, K, V>
22where
23 T: Ord + Clone,
24 K: Hash + Eq,
25 V: OrdBy<Target = T>,
26{
27 /// Inserts `default` value if the entry is vacant, and returns a mutable reference to the value.
28 ///
29 /// # Examples
30 ///
31 /// ```
32 /// use valord_map::ValordMap;
33 ///
34 /// let mut map = ValordMap::new();
35 /// map.entry("key").or_insert("value");
36 ///
37 /// assert_eq!(map.get(&"key"), Some(&"value"));
38 /// ```
39 pub fn or_insert(&mut self, default: V) -> &mut V {
40 match self {
41 Entry::Occupied(entry) => entry.get_mut_with_key().1,
42 Entry::Vacant(entry) => {
43 entry.valord.free_indexs.pop_front();
44 entry.insert(default)
45 }
46 }
47 }
48
49 /// Inserts a value produced by the function `default` if the entry is vacant,
50 /// and returns a mutable reference to the value.
51 ///
52 /// # Examples
53 ///
54 /// ```
55 /// use valord_map::ValordMap;
56 ///
57 /// let mut map = ValordMap::new();
58 /// map.entry("key").or_insert_with(|| "value");
59 ///
60 /// assert_eq!(map.get(&"key"), Some(&"value"));
61 /// ```
62 pub fn or_insert_with<F: FnOnce() -> V>(&mut self, default: F) -> &mut V {
63 match self {
64 Entry::Occupied(entry) => entry.get_mut_with_key().1,
65 Entry::Vacant(entry) => {
66 entry.valord.free_indexs.pop_front();
67 entry.insert(default())
68 }
69 }
70 }
71
72 /// Inserts a value produced by the function `default` with the given key if the entry is vacant,
73 /// and returns a mutable reference to the value.
74 ///
75 /// # Examples
76 ///
77 /// ```
78 /// use valord_map::ValordMap;
79 ///
80 /// let mut map = ValordMap::new();
81 /// map.entry("key").or_insert_with_key(|key| format!("value for {}", key));
82 ///
83 /// assert_eq!(map.get(&"key"), Some(&"value for key".to_string()));
84 /// ```
85 pub fn or_insert_with_key<F: FnOnce(&K) -> V>(&mut self, default: F) -> &mut V {
86 match self {
87 Entry::Occupied(entry) => entry.get_mut_with_key().1,
88 Entry::Vacant(entry) => {
89 entry.valord.free_indexs.pop_front();
90 entry.insert_with_key(default)
91 }
92 }
93 }
94
95 /// Modifies the entry if it is occupied with the function `f`, and returns the entry.
96 ///
97 /// # Examples
98 ///
99 /// ```
100 /// use valord_map::ValordMap;
101 ///
102 /// let mut map = ValordMap::new();
103 /// map.entry("key").and_modify(|v| *v = "new value").or_insert("value");
104 ///
105 /// assert_eq!(map.get(&"key"), Some(&"value"));
106 ///
107 /// map.entry("key").and_modify(|v| *v = "new value").or_insert("value");
108 ///
109 /// assert_eq!(map.get(&"key"), Some(&"new value"));
110 /// ```
111 pub fn and_modify<F>(mut self, f: F) -> Self
112 where
113 F: FnOnce(&mut V),
114 {
115 if let Entry::Occupied(entry) = &mut self {
116 f(entry.get_mut_with_key().1);
117 }
118 self
119 }
120}
121
122impl<'v, T, K, V> Entry<'v, T, K, V>
123where
124 T: Ord + Clone,
125 K: Hash + Eq,
126 V: OrdBy<Target = T> + Default,
127{
128 /// Inserts the default value if the entry is vacant, and returns a mutable reference to the value.
129 ///
130 /// # Examples
131 ///
132 /// ```
133 /// use valord_map::ValordMap;
134 ///
135 /// let mut map: ValordMap<usize, &str, usize> = ValordMap::new();
136 /// map.entry("key").or_default();
137 ///
138 /// assert_eq!(map.get(&"key"), Some(&Default::default()));
139 /// ```
140 pub fn or_default(&mut self) -> &mut V {
141 match self {
142 Entry::Occupied(entry) => entry.get_mut_with_key().1,
143 Entry::Vacant(entry) => {
144 entry.valord.free_indexs.pop_front();
145 entry.insert(V::default())
146 }
147 }
148 }
149}