From 770fdeddb4ca7cedca41a391d4b283069e1d9a8c Mon Sep 17 00:00:00 2001 From: hedgehog1024 Date: Mon, 12 Feb 2018 22:19:37 +0300 Subject: [PATCH 1/4] Stabilize 'entry_and_modify' feature for BTreeMap --- src/liballoc/btree/map.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/liballoc/btree/map.rs b/src/liballoc/btree/map.rs index b320bed54320a..b98489c516a05 100644 --- a/src/liballoc/btree/map.rs +++ b/src/liballoc/btree/map.rs @@ -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.25.0")] pub fn and_modify(self, mut f: F) -> Self where F: FnMut(&mut V) { From 862132be72d4de87330e31d53489b8c718a6663e Mon Sep 17 00:00:00 2001 From: hedgehog1024 Date: Mon, 12 Feb 2018 22:25:03 +0300 Subject: [PATCH 2/4] Stabilize 'entry_and_modify' feature for HashMap --- src/libstd/collections/hash/map.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index 82a687ae5e493..80e52123a0177 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -2066,7 +2066,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(); @@ -2081,7 +2080,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.25.0")] pub fn and_modify(self, mut f: F) -> Self where F: FnMut(&mut V) { From 4360dfa126ba5d26a520e8a0d2dd9680081dad0d Mon Sep 17 00:00:00 2001 From: hedgehog1024 Date: Mon, 12 Feb 2018 22:27:33 +0300 Subject: [PATCH 3/4] Delete information about 'entry_and_modify' from Unstable book --- .../src/library-features/entry-and-modify.md | 77 ------------------- 1 file changed, 77 deletions(-) delete mode 100644 src/doc/unstable-book/src/library-features/entry-and-modify.md diff --git a/src/doc/unstable-book/src/library-features/entry-and-modify.md b/src/doc/unstable-book/src/library-features/entry-and-modify.md deleted file mode 100644 index 1280c71e83c92..0000000000000 --- a/src/doc/unstable-book/src/library-features/entry-and-modify.md +++ /dev/null @@ -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 }); - }, -}; -# } -``` From 0aa753ba30254631ce05f37cd2ff0dd428d931c5 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Sat, 24 Feb 2018 21:06:29 -0800 Subject: [PATCH 4/4] 1.25.0 -> 1.26.- --- src/liballoc/btree/map.rs | 2 +- src/libstd/collections/hash/map.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/liballoc/btree/map.rs b/src/liballoc/btree/map.rs index b98489c516a05..618ef81fdd981 100644 --- a/src/liballoc/btree/map.rs +++ b/src/liballoc/btree/map.rs @@ -2128,7 +2128,7 @@ impl<'a, K: Ord, V> Entry<'a, K, V> { /// .or_insert(42); /// assert_eq!(map["poneyland"], 43); /// ``` - #[stable(feature = "entry_and_modify", since = "1.25.0")] + #[stable(feature = "entry_and_modify", since = "1.26.0")] pub fn and_modify(self, mut f: F) -> Self where F: FnMut(&mut V) { diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index 80e52123a0177..b63a45ecade43 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -2080,7 +2080,7 @@ impl<'a, K, V> Entry<'a, K, V> { /// .or_insert(42); /// assert_eq!(map["poneyland"], 43); /// ``` - #[stable(feature = "entry_and_modify", since = "1.25.0")] + #[stable(feature = "entry_and_modify", since = "1.26.0")] pub fn and_modify(self, mut f: F) -> Self where F: FnMut(&mut V) {