about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2017-09-12 09:54:59 +0000
committerbors <bors@rust-lang.org>2017-09-12 09:54:59 +0000
commit817e1b81e230d599585f860cdcad96c5ed83b93e (patch)
treec8b45bc0eaa2d61bf3b1dbbd941b41dbefcc9f31
parent8fc0fc8c2d9718f8b664afca8e28636a6099e2b8 (diff)
parent9389d26ba261119bec1dc5336d17ce23c34785b7 (diff)
downloadrust-817e1b81e230d599585f860cdcad96c5ed83b93e.tar.gz
rust-817e1b81e230d599585f860cdcad96c5ed83b93e.zip
Auto merge of #44344 - jonhoo:entry_or_default, r=BurntSushi
Add or_default to Entry APIs

As argued for in #44324, this PR adds a new `or_default` method to the various `Entry` APIs (currently just for `BTreeMap` and `HashMap`) when `V: Default`. This method is effectively a shorthand for `or_insert_with(Default::default)`.
-rw-r--r--src/doc/unstable-book/src/library-features/entry-or-default.md13
-rw-r--r--src/liballoc/btree/map.rs27
-rw-r--r--src/libstd/collections/hash/map.rs27
3 files changed, 67 insertions, 0 deletions
diff --git a/src/doc/unstable-book/src/library-features/entry-or-default.md b/src/doc/unstable-book/src/library-features/entry-or-default.md
new file mode 100644
index 00000000000..f8c8a2a7a71
--- /dev/null
+++ b/src/doc/unstable-book/src/library-features/entry-or-default.md
@@ -0,0 +1,13 @@
+# `entry_or_default`
+
+The tracking issue for this feature is: [#44324]
+
+[#44324]: https://github.com/rust-lang/rust/issues/44324
+
+------------------------
+
+The `entry_or_default` feature adds a new method to `hash_map::Entry`
+and `btree_map::Entry`, `or_default`, when `V: Default`. This method is
+semantically identical to `or_insert_with(Default::default)`, and will
+insert the default value for the type if no entry exists for the current
+key.
diff --git a/src/liballoc/btree/map.rs b/src/liballoc/btree/map.rs
index f733c3332e2..4c93fead172 100644
--- a/src/liballoc/btree/map.rs
+++ b/src/liballoc/btree/map.rs
@@ -2104,6 +2104,33 @@ impl<'a, K: Ord, V> Entry<'a, K, V> {
     }
 }
 
+impl<'a, K: Ord, V: Default> Entry<'a, K, V> {
+    #[unstable(feature = "entry_or_default", issue = "44324")]
+    /// 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
+    ///
+    /// ```
+    /// #![feature(entry_or_default)]
+    /// # fn main() {
+    /// use std::collections::BTreeMap;
+    ///
+    /// let mut map: BTreeMap<&str, Option<usize>> = BTreeMap::new();
+    /// map.entry("poneyland").or_default();
+    ///
+    /// assert_eq!(map["poneyland"], None);
+    /// # }
+    /// ```
+    pub fn or_default(self) -> &'a mut V {
+        match self {
+            Occupied(entry) => entry.into_mut(),
+            Vacant(entry) => entry.insert(Default::default()),
+        }
+    }
+
+}
+
 impl<'a, K: Ord, V> VacantEntry<'a, K, V> {
     /// Gets a reference to the key that would be used when inserting a value
     /// through the VacantEntry.
diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs
index 16b0c709986..fbb69ca9749 100644
--- a/src/libstd/collections/hash/map.rs
+++ b/src/libstd/collections/hash/map.rs
@@ -2001,6 +2001,33 @@ impl<'a, K, V> Entry<'a, K, V> {
     }
 }
 
+impl<'a, K, V: Default> Entry<'a, K, V> {
+    #[unstable(feature = "entry_or_default", issue = "44324")]
+    /// 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
+    ///
+    /// ```
+    /// #![feature(entry_or_default)]
+    /// # fn main() {
+    /// use std::collections::HashMap;
+    ///
+    /// let mut map: HashMap<&str, Option<u32>> = HashMap::new();
+    /// map.entry("poneyland").or_default();
+    ///
+    /// assert_eq!(map["poneyland"], None);
+    /// # }
+    /// ```
+    pub fn or_default(self) -> &'a mut V {
+        match self {
+            Occupied(entry) => entry.into_mut(),
+            Vacant(entry) => entry.insert(Default::default()),
+        }
+    }
+
+}
+
 impl<'a, K, V> OccupiedEntry<'a, K, V> {
     /// Gets a reference to the key in the entry.
     ///