Rollup merge of #48166 - hedgehog1024:hedgehog1024-stabilize-entry_and_modify, r=alexcrichton

Stabilize 'entry_and_modify' feature

Stabilize `entry_and_modify` feature introduced by #44734.

Closes #44733
This commit is contained in:
kennytm 2018-02-25 15:54:42 +08:00 committed by GitHub
commit b443e57ce6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 2 additions and 81 deletions

View File

@ -1,77 +0,0 @@
# `entry_and_modify`
The tracking issue for this feature is: [#44733]
[#44733]: https://github.com/rust-lang/rust/issues/44733
------------------------
This introduces a new method for the Entry API of maps
(`std::collections::HashMap` and `std::collections::BTreeMap`), so that
occupied entries can be modified before any potential inserts into the
map.
For example:
```rust
#![feature(entry_and_modify)]
# fn main() {
use std::collections::HashMap;
struct Foo {
new: bool,
}
let mut map: HashMap<&str, Foo> = HashMap::new();
map.entry("quux")
.and_modify(|e| e.new = false)
.or_insert(Foo { new: true });
# }
```
This is not possible with the stable API alone since inserting a default
_before_ modifying the `new` field would mean we would lose the default state:
```rust
# fn main() {
use std::collections::HashMap;
struct Foo {
new: bool,
}
let mut map: HashMap<&str, Foo> = HashMap::new();
map.entry("quux").or_insert(Foo { new: true }).new = false;
# }
```
In the above code the `new` field will never be `true`, even though we only
intended to update that field to `false` for previously extant entries.
To achieve the same effect as `and_modify` we would have to manually match
against the `Occupied` and `Vacant` variants of the `Entry` enum, which is
a little less user-friendly, and much more verbose:
```rust
# fn main() {
use std::collections::HashMap;
use std::collections::hash_map::Entry;
struct Foo {
new: bool,
}
let mut map: HashMap<&str, Foo> = HashMap::new();
match map.entry("quux") {
Entry::Occupied(entry) => {
entry.into_mut().new = false;
},
Entry::Vacant(entry) => {
entry.insert(Foo { new: true });
},
};
# }
```

View File

@ -2114,7 +2114,6 @@ impl<'a, K: Ord, V> Entry<'a, K, V> {
/// # Examples
///
/// ```
/// #![feature(entry_and_modify)]
/// use std::collections::BTreeMap;
///
/// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
@ -2129,7 +2128,7 @@ impl<'a, K: Ord, V> Entry<'a, K, V> {
/// .or_insert(42);
/// assert_eq!(map["poneyland"], 43);
/// ```
#[unstable(feature = "entry_and_modify", issue = "44733")]
#[stable(feature = "entry_and_modify", since = "1.26.0")]
pub fn and_modify<F>(self, mut f: F) -> Self
where F: FnMut(&mut V)
{

View File

@ -2082,7 +2082,6 @@ impl<'a, K, V> Entry<'a, K, V> {
/// # Examples
///
/// ```
/// #![feature(entry_and_modify)]
/// use std::collections::HashMap;
///
/// let mut map: HashMap<&str, u32> = HashMap::new();
@ -2097,7 +2096,7 @@ impl<'a, K, V> Entry<'a, K, V> {
/// .or_insert(42);
/// assert_eq!(map["poneyland"], 43);
/// ```
#[unstable(feature = "entry_and_modify", issue = "44733")]
#[stable(feature = "entry_and_modify", since = "1.26.0")]
pub fn and_modify<F>(self, mut f: F) -> Self
where F: FnMut(&mut V)
{