@@ -1748,38 +1748,61 @@ impl<'a, K, V, S> RawEntryMut<'a, K, V, S> {
1748
1748
///
1749
1749
/// ```
1750
1750
/// use hashbrown::HashMap;
1751
+ /// use hashbrown::hash_map::RawEntryMut;
1751
1752
///
1752
1753
/// let mut map: HashMap<&str, u32> = HashMap::new();
1753
1754
///
1754
- /// map.raw_entry_mut()
1755
- /// .from_key("poneyland")
1756
- /// .and_replace_entry_with(|_k, _v| unreachable!());
1755
+ /// let entry = map
1756
+ /// .raw_entry_mut()
1757
+ /// .from_key("poneyland")
1758
+ /// .and_replace_entry_with(|_k, _v| panic!());
1759
+ ///
1760
+ /// match entry {
1761
+ /// RawEntryMut::Vacant(_) => {},
1762
+ /// RawEntryMut::Occupied(_) => panic!(),
1763
+ /// }
1757
1764
///
1758
1765
/// map.insert("poneyland", 42);
1759
1766
///
1760
- /// map.raw_entry_mut()
1761
- /// .from_key("poneyland")
1762
- /// .and_replace_entry_with(|k, v| {
1767
+ /// let entry = map
1768
+ /// .raw_entry_mut()
1769
+ /// .from_key("poneyland")
1770
+ /// .and_replace_entry_with(|k, v| {
1763
1771
/// assert_eq!(k, &"poneyland");
1764
1772
/// assert_eq!(v, 42);
1765
1773
/// Some(v + 1)
1766
1774
/// });
1775
+ ///
1776
+ /// match entry {
1777
+ /// RawEntryMut::Occupied(e) => {
1778
+ /// assert_eq!(e.key(), &"poneyland");
1779
+ /// assert_eq!(e.get(), &43);
1780
+ /// },
1781
+ /// RawEntryMut::Vacant(_) => panic!(),
1782
+ /// }
1783
+ ///
1767
1784
/// assert_eq!(map["poneyland"], 43);
1768
1785
///
1769
- /// map.raw_entry_mut()
1770
- /// .from_key("poneyland")
1771
- /// .and_replace_entry_with(|_k, _v| None);
1786
+ /// let entry = map
1787
+ /// .raw_entry_mut()
1788
+ /// .from_key("poneyland")
1789
+ /// .and_replace_entry_with(|_k, _v| None);
1790
+ ///
1791
+ /// match entry {
1792
+ /// RawEntryMut::Vacant(_) => {},
1793
+ /// RawEntryMut::Occupied(_) => panic!(),
1794
+ /// }
1772
1795
///
1773
1796
/// assert!(!map.contains_key("poneyland"));
1774
1797
/// ```
1775
1798
#[ cfg_attr( feature = "inline-more" , inline) ]
1776
- pub fn and_replace_entry_with < F > ( self , f : F )
1799
+ pub fn and_replace_entry_with < F > ( self , f : F ) -> Self
1777
1800
where
1778
1801
F : FnOnce ( & K , V ) -> Option < V > ,
1779
1802
{
1780
1803
match self {
1781
1804
RawEntryMut :: Occupied ( entry) => entry. replace_entry_with ( f) ,
1782
- RawEntryMut :: Vacant ( _) => { }
1805
+ RawEntryMut :: Vacant ( _) => self ,
1783
1806
}
1784
1807
}
1785
1808
}
@@ -1879,15 +1902,26 @@ impl<'a, K, V, S> RawOccupiedEntryMut<'a, K, V, S> {
1879
1902
/// the entry and allows to replace or remove it based on the
1880
1903
/// value of the returned option.
1881
1904
#[ cfg_attr( feature = "inline-more" , inline) ]
1882
- pub fn replace_entry_with < F > ( self , f : F )
1905
+ pub fn replace_entry_with < F > ( self , f : F ) -> RawEntryMut < ' a , K , V , S >
1883
1906
where
1884
1907
F : FnOnce ( & K , V ) -> Option < V > ,
1885
1908
{
1886
1909
unsafe {
1887
- self . table . replace_bucket_with ( self . elem , |( key, value) | {
1888
- f ( & key, value) . map ( |new_value| ( key, new_value) )
1889
- } )
1890
- } ;
1910
+ let still_occupied = self
1911
+ . table
1912
+ . replace_bucket_with ( self . elem . clone ( ) , |( key, value) | {
1913
+ f ( & key, value) . map ( |new_value| ( key, new_value) )
1914
+ } ) ;
1915
+
1916
+ if still_occupied {
1917
+ RawEntryMut :: Occupied ( self )
1918
+ } else {
1919
+ RawEntryMut :: Vacant ( RawVacantEntryMut {
1920
+ table : self . table ,
1921
+ hash_builder : self . hash_builder ,
1922
+ } )
1923
+ }
1924
+ }
1891
1925
}
1892
1926
}
1893
1927
0 commit comments