@@ -1242,7 +1242,6 @@ impl<K: Debug, V: Debug, S> Debug for Entry<'_, K, V, S> {
1242
1242
///
1243
1243
/// [`Entry`]: enum.Entry.html
1244
1244
pub struct OccupiedEntry < ' a , K , V , S > {
1245
- #[ allow( dead_code) ] // remove when replace_ methods are implemented
1246
1245
key : Option < K > ,
1247
1246
elem : Bucket < ( K , V ) > ,
1248
1247
table : & ' a mut HashMap < K , V , S > ,
@@ -1863,6 +1862,67 @@ impl<'a, K, V, S> OccupiedEntry<'a, K, V, S> {
1863
1862
// restarting the iterator, though that would come at a performance penalty...
1864
1863
//
1865
1864
// when implemented, remove the #[allow(dead_code)] on OccupiedEntry::key
1865
+
1866
+ /// Replaces the entry, returning the old key and value. The new key in the hash map will be
1867
+ /// the key used to create this entry.
1868
+ ///
1869
+ /// # Examples
1870
+ ///
1871
+ /// ```
1872
+ /// use griddle::hash_map::{Entry, HashMap};
1873
+ /// use std::rc::Rc;
1874
+ ///
1875
+ /// let mut map: HashMap<Rc<String>, u32> = HashMap::new();
1876
+ /// map.insert(Rc::new("Stringthing".to_string()), 15);
1877
+ ///
1878
+ /// let my_key = Rc::new("Stringthing".to_string());
1879
+ ///
1880
+ /// if let Entry::Occupied(entry) = map.entry(my_key.clone()) {
1881
+ /// // Also replace the key with a handle to our other key.
1882
+ /// let (old_key, old_value): (Rc<String>, u32) = entry.replace_entry(16);
1883
+ /// }
1884
+ ///
1885
+ /// assert_eq!(map[&my_key], 16);
1886
+ /// ```
1887
+ #[ cfg_attr( feature = "inline-more" , inline) ]
1888
+ pub fn replace_entry ( self , value : V ) -> ( K , V ) {
1889
+ let entry = unsafe { self . elem . as_mut ( ) } ;
1890
+
1891
+ let old_key = mem:: replace ( & mut entry. 0 , self . key . unwrap ( ) ) ;
1892
+ let old_value = mem:: replace ( & mut entry. 1 , value) ;
1893
+
1894
+ ( old_key, old_value)
1895
+ }
1896
+
1897
+ /// Replaces the key in the hash map with the key used to create this entry.
1898
+ ///
1899
+ /// # Examples
1900
+ ///
1901
+ /// ```
1902
+ /// use griddle::hash_map::{Entry, HashMap};
1903
+ /// use std::rc::Rc;
1904
+ ///
1905
+ /// let mut map: HashMap<Rc<String>, u32> = HashMap::new();
1906
+ /// let mut known_strings: Vec<Rc<String>> = Vec::new();
1907
+ ///
1908
+ /// // Initialise known strings, run program, etc.
1909
+ ///
1910
+ /// reclaim_memory(&mut map, &known_strings);
1911
+ ///
1912
+ /// fn reclaim_memory(map: &mut HashMap<Rc<String>, u32>, known_strings: &[Rc<String>] ) {
1913
+ /// for s in known_strings {
1914
+ /// if let Entry::Occupied(entry) = map.entry(s.clone()) {
1915
+ /// // Replaces the entry's key with our version of it in `known_strings`.
1916
+ /// entry.replace_key();
1917
+ /// }
1918
+ /// }
1919
+ /// }
1920
+ /// ```
1921
+ #[ cfg_attr( feature = "inline-more" , inline) ]
1922
+ pub fn replace_key ( self ) -> K {
1923
+ let entry = unsafe { self . elem . as_mut ( ) } ;
1924
+ mem:: replace ( & mut entry. 0 , self . key . unwrap ( ) )
1925
+ }
1866
1926
}
1867
1927
1868
1928
impl < ' a , K , V , S > VacantEntry < ' a , K , V , S > {
0 commit comments