Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add or_default to Entry APIs #44344

Merged
merged 3 commits into from
Sep 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/doc/unstable-book/src/library-features/entry-or-default.md
Original file line number Diff line number Diff line change
@@ -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.
27 changes: 27 additions & 0 deletions src/liballoc/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
27 changes: 27 additions & 0 deletions src/libstd/collections/hash/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down