diff --git a/src/map.rs b/src/map.rs index c373d5958..e7ea8b9da 100644 --- a/src/map.rs +++ b/src/map.rs @@ -3527,6 +3527,38 @@ impl<'a, K, V, S, A: Allocator> Entry<'a, K, V, S, A> { } } + /// Ensures a value is in the entry by inserting the default if empty, + /// and returns an [`OccupiedEntry`]. + /// + /// # Examples + /// + /// ``` + /// use hashbrown::HashMap; + /// + /// let mut map: HashMap<&str, u32> = HashMap::new(); + /// + /// // nonexistent key + /// let entry = map.entry("poneyland").or_insert_entry(3); + /// assert_eq!(entry.key(), &"poneyland"); + /// assert_eq!(entry.get(), &3); + /// + /// // existing key + /// let mut entry = map.entry("poneyland").or_insert_entry(10); + /// assert_eq!(entry.key(), &"poneyland"); + /// assert_eq!(entry.get(), &3); + /// ``` + #[cfg_attr(feature = "inline-more", inline)] + pub fn or_insert_entry(self, default: V) -> OccupiedEntry<'a, K, V, S, A> + where + K: Hash, + S: BuildHasher, + { + match self { + Entry::Occupied(entry) => entry, + Entry::Vacant(entry) => entry.insert_entry(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. /// @@ -4328,6 +4360,39 @@ impl<'a, 'b, K, Q: ?Sized, V: Default, S, A: Allocator> EntryRef<'a, 'b, K, Q, V EntryRef::Vacant(entry) => entry.insert(Default::default()), } } + + /// Ensures a value is in the entry by inserting the default value if empty, + /// and returns an [`OccupiedEntry`]. + /// + /// # Examples + /// + /// ``` + /// use hashbrown::HashMap; + /// + /// let mut map: HashMap> = HashMap::new(); + /// + /// // nonexistent key + /// let entry = map.entry_ref("poneyland").or_default_entry(); + /// assert_eq!(entry.key(), &"poneyland"); + /// assert_eq!(entry.get(), &None); + /// + /// // existing key + /// map.insert("horseland".to_string(), Some(3)); + /// let entry = map.entry_ref("horseland").or_default_entry(); + /// assert_eq!(entry.key(), &"horseland"); + /// assert_eq!(entry.get(), &Some(3)); + /// ``` + #[cfg_attr(feature = "inline-more", inline)] + pub fn or_default_entry(self) -> OccupiedEntry<'a, K, V, S, A> + where + K: Hash + From<&'b Q>, + S: BuildHasher, + { + match self { + EntryRef::Occupied(entry) => entry, + EntryRef::Vacant(entry) => entry.insert_entry(Default::default()), + } + } } impl<'a, 'b, K, Q: ?Sized, V, S, A: Allocator> VacantEntryRef<'a, 'b, K, Q, V, S, A> {