diff options
| author | Alexis <a.beingessner@gmail.com> | 2015-03-01 09:50:08 -0500 |
|---|---|---|
| committer | Alexis <a.beingessner@gmail.com> | 2015-03-26 21:36:06 -0400 |
| commit | 1c35953cf8baa2e721401ba6354f0bd9a9f2abaf (patch) | |
| tree | 221cff7b67f02a6fa1d30974705cd245473863cc /src/libstd | |
| parent | 199bdcfeff5cfafd1f8e8ff583d7209272469879 (diff) | |
| download | rust-1c35953cf8baa2e721401ba6354f0bd9a9f2abaf.tar.gz rust-1c35953cf8baa2e721401ba6354f0bd9a9f2abaf.zip | |
entry API v3: replace Entry::get with Entry::default and Entry::default_with
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/collections/hash/map.rs | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index f9558b85825..b3557529a66 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -23,7 +23,7 @@ use hash::{Hash, SipHasher}; use iter::{self, Iterator, ExactSizeIterator, IntoIterator, IteratorExt, FromIterator, Extend, Map}; use marker::Sized; use mem::{self, replace}; -use ops::{Deref, FnMut, Index}; +use ops::{Deref, FnMut, FnOnce, Index}; use option::Option::{self, Some, None}; use rand::{self, Rng}; use result::Result::{self, Ok, Err}; @@ -1488,12 +1488,36 @@ impl<'a, K, V> Entry<'a, K, V> { /// Returns a mutable reference to the entry if occupied, or the VacantEntry if vacant. #[unstable(feature = "std_misc", reason = "will soon be replaced by or_insert")] + #[deprecated(since = "1.0", + reason = "replaced with more ergonomic `default` and `default_with`")] pub fn get(self) -> Result<&'a mut V, VacantEntry<'a, K, V>> { match self { Occupied(entry) => Ok(entry.into_mut()), Vacant(entry) => Err(entry), } } + + #[unstable(feature = "collections", + reason = "matches entry v3 specification, waiting for dust to settle")] + /// Ensures a value is in the entry by inserting the default if empty, and returns + /// a mutable reference to the value in the entry. + pub fn default(self, default: V) -> &'a mut V { + match self { + Occupied(entry) => entry.into_mut(), + Vacant(entry) => entry.insert(default), + } + } + + #[unstable(feature = "collections", + reason = "matches entry v3 specification, waiting for dust to settle")] + /// 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. + pub fn default_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V { + match self { + Occupied(entry) => entry.into_mut(), + Vacant(entry) => entry.insert(default()), + } + } } impl<'a, K, V> OccupiedEntry<'a, K, V> { |
