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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
use crate::map::{MapStorage, OccupiedEntry, VacantEntry};
/// A view into a single entry in a map, which may either be vacant or occupied.
///
/// This enum is constructed from the [`entry`][crate::Map::entry] method on [`Map`][crate::Map].
pub enum Entry<'a, S: 'a, K, V>
where
S: MapStorage<K, V>,
{
/// An occupied entry.
Occupied(S::Occupied<'a>),
/// A vacant entry.
Vacant(S::Vacant<'a>),
}
impl<'a, S: 'a, K, V> Entry<'a, S, K, V>
where
S: MapStorage<K, V>,
{
/// Ensures a value is in the entry by inserting the default if empty,
/// and returns a mutable reference to the value in the entry.
///
/// # Examples
///
/// ```
/// use fixed_map::{Key, Map};
///
/// #[derive(Clone, Copy, Key)]
/// enum MyKey {
/// First,
/// Second,
/// }
///
/// let mut map: Map<MyKey, i32> = Map::new();
///
/// map.entry(MyKey::First).or_insert(3);
/// assert_eq!(map.get(MyKey::First), Some(&3));
///
/// *map.entry(MyKey::First).or_insert(10) *= 2;
/// assert_eq!(map.get(MyKey::First), Some(&6));
/// ```
///
/// Using a composite key:
///
/// ```
/// use fixed_map::{Key, Map};
///
/// #[derive(Clone, Copy, Key)]
/// enum MyKey {
/// First(bool),
/// Second,
/// }
///
/// let mut map: Map<MyKey, i32> = Map::new();
///
/// map.entry(MyKey::First(false)).or_insert(3);
/// assert_eq!(map.get(MyKey::First(false)), Some(&3));
///
/// *map.entry(MyKey::First(false)).or_insert(10) *= 2;
/// assert_eq!(map.get(MyKey::First(false)), Some(&6));
/// ```
#[inline]
pub fn or_insert(self, default: V) -> &'a mut V {
match self {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => entry.insert(default),
}
}
/// Ensures a value is in the entry by inserting the result of the default function if empty,
/// and returns a mutable reference to the value in the entry.
///
/// # Examples
///
/// ```
/// use fixed_map::{Key, Map};
///
/// #[derive(Clone, Copy, Key)]
/// enum MyKey {
/// First,
/// Second,
/// }
///
/// let mut map: Map<MyKey, String> = Map::new();
///
/// map.entry(MyKey::First).or_insert_with(|| format!("{}", 3));
/// assert_eq!(map.get(MyKey::First), Some(&"3".to_string()));
/// ```
///
/// Using a composite key:
///
/// ```
/// use fixed_map::{Key, Map};
///
/// #[derive(Clone, Copy, Key)]
/// enum MyKey {
/// First(bool),
/// Second,
/// }
///
/// let mut map: Map<MyKey, String> = Map::new();
///
/// map.entry(MyKey::First(false)).or_insert_with(|| format!("{}", 3));
/// assert_eq!(map.get(MyKey::First(false)), Some(&"3".to_string()));
/// ```
#[inline]
pub fn or_insert_with<F>(self, default: F) -> &'a mut V
where
F: FnOnce() -> V,
{
match self {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => entry.insert(default()),
}
}
/// Ensures a value is in the entry by inserting, if empty, the result of the default function.
/// This method allows for generating key-derived values for insertion by providing the default
/// function a copy of the key that was passed to the `.entry(key)` method call.
///
/// # Examples
///
/// ```
/// use fixed_map::{Key, Map};
///
/// #[derive(Clone, Copy, Key, Debug)]
/// enum MyKey {
/// First,
/// Second,
/// }
///
/// let mut map: Map<MyKey, String> = Map::new();
///
/// map.entry(MyKey::First).or_insert_with_key(|k| format!("{:?} = {}", k, 3));
/// assert_eq!(map.get(MyKey::First), Some(&"First = 3".to_string()));
/// ```
///
/// Using a composite key:
///
/// ```
/// use fixed_map::{Key, Map};
///
/// #[derive(Clone, Copy, Key, Debug)]
/// enum MyKey {
/// First(bool),
/// Second,
/// }
///
/// let mut map: Map<MyKey, String> = Map::new();
///
/// map.entry(MyKey::First(false)).or_insert_with_key(|k| format!("{:?} = {}", k, 3));
/// assert_eq!(map.get(MyKey::First(false)), Some(&"First(false) = 3".to_string()));
/// ```
#[inline]
pub fn or_insert_with_key<F>(self, default: F) -> &'a mut V
where
F: FnOnce(K) -> V,
{
match self {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => {
let value = default(entry.key());
entry.insert(value)
}
}
}
/// Returns a copy of this entry's key.
///
/// # Examples
///
/// ```
/// use fixed_map::{Key, Map};
///
/// #[derive(Clone, Copy, Key, Debug, PartialEq)]
/// enum MyKey {
/// First,
/// Second,
/// }
///
/// let mut map: Map<MyKey, i32> = Map::new();
/// assert_eq!(map.entry(MyKey::First).key(), MyKey::First);
/// ```
///
/// Using a composite key:
///
/// ```
/// use fixed_map::{Key, Map};
///
/// #[derive(Clone, Copy, Key, Debug, PartialEq)]
/// enum MyKey {
/// First(bool),
/// Second,
/// }
///
/// let mut map: Map<MyKey, i32> = Map::new();
/// assert_eq!(map.entry(MyKey::First(false)).key(), MyKey::First(false));
/// ```
#[inline]
pub fn key(&self) -> K {
match self {
Entry::Occupied(entry) => entry.key(),
Entry::Vacant(entry) => entry.key(),
}
}
/// Provides in-place mutable access to an occupied entry before any
/// potential inserts into the map.
///
/// # Examples
///
/// ```
/// use fixed_map::{Key, Map};
///
/// #[derive(Clone, Copy, Key)]
/// enum MyKey {
/// First,
/// Second,
/// }
///
/// let mut map: Map<MyKey, i32> = Map::new();
///
/// map.entry(MyKey::First)
/// .and_modify(|e| { *e += 1 })
/// .or_insert(42);
/// assert_eq!(map.get(MyKey::First), Some(&42));
///
/// map.entry(MyKey::First)
/// .and_modify(|e| { *e += 1 })
/// .or_insert(42);
/// assert_eq!(map.get(MyKey::First), Some(&43));
/// ```
///
/// Using a composite key:
///
/// ```
/// use fixed_map::{Key, Map};
///
/// #[derive(Clone, Copy, Key)]
/// enum MyKey {
/// First(bool),
/// Second,
/// }
///
/// let mut map: Map<MyKey, i32> = Map::new();
///
/// map.entry(MyKey::First(true))
/// .and_modify(|e| { *e += 1 })
/// .or_insert(42);
/// assert_eq!(map.get(MyKey::First(true)), Some(&42));
///
/// map.entry(MyKey::First(true))
/// .and_modify(|e| { *e += 1 })
/// .or_insert(42);
/// assert_eq!(map.get(MyKey::First(true)), Some(&43));
/// ```
#[inline]
#[must_use]
pub fn and_modify<F>(self, f: F) -> Self
where
F: FnOnce(&mut V),
{
match self {
Entry::Occupied(mut entry) => {
f(entry.get_mut());
Entry::Occupied(entry)
}
Entry::Vacant(entry) => Entry::Vacant(entry),
}
}
/// Ensures a value is in the entry by inserting the default value if empty,
/// and returns a mutable reference to the value in the entry.
///
/// # Examples
///
/// ```
/// use fixed_map::{Key, Map};
///
/// #[derive(Clone, Copy, Key)]
/// enum MyKey {
/// First,
/// Second,
/// }
///
/// let mut map: Map<MyKey, i32> = Map::new();
///
/// map.entry(MyKey::First).or_default();
/// assert_eq!(map.get(MyKey::First), Some(&0));
/// ```
///
/// Using a composite key:
///
/// ```
/// use fixed_map::{Key, Map};
///
/// #[derive(Clone, Copy, Key)]
/// enum MyKey {
/// First(bool),
/// Second,
/// }
///
/// let mut map: Map<MyKey, i32> = Map::new();
///
/// map.entry(MyKey::First(false)).or_default();
/// assert_eq!(map.get(MyKey::First(false)), Some(&0));
/// ```
#[inline]
pub fn or_default(self) -> &'a mut V
where
V: Default,
{
match self {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => entry.insert(Default::default()),
}
}
}