Skip to content

Commit 30103a5

Browse files
committed
RawOccupiedEntryMut::replace_entry_with and RawEntryMut::and_replace_entry_with now return a RawEntryMut
1 parent b0cff2f commit 30103a5

File tree

1 file changed

+50
-16
lines changed

1 file changed

+50
-16
lines changed

src/map.rs

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,38 +1748,61 @@ impl<'a, K, V, S> RawEntryMut<'a, K, V, S> {
17481748
///
17491749
/// ```
17501750
/// use hashbrown::HashMap;
1751+
/// use hashbrown::hash_map::RawEntryMut;
17511752
///
17521753
/// let mut map: HashMap<&str, u32> = HashMap::new();
17531754
///
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+
/// }
17571764
///
17581765
/// map.insert("poneyland", 42);
17591766
///
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| {
17631771
/// assert_eq!(k, &"poneyland");
17641772
/// assert_eq!(v, 42);
17651773
/// Some(v + 1)
17661774
/// });
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+
///
17671784
/// assert_eq!(map["poneyland"], 43);
17681785
///
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+
/// }
17721795
///
17731796
/// assert!(!map.contains_key("poneyland"));
17741797
/// ```
17751798
#[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
17771800
where
17781801
F: FnOnce(&K, V) -> Option<V>,
17791802
{
17801803
match self {
17811804
RawEntryMut::Occupied(entry) => entry.replace_entry_with(f),
1782-
RawEntryMut::Vacant(_) => {}
1805+
RawEntryMut::Vacant(_) => self,
17831806
}
17841807
}
17851808
}
@@ -1879,15 +1902,26 @@ impl<'a, K, V, S> RawOccupiedEntryMut<'a, K, V, S> {
18791902
/// the entry and allows to replace or remove it based on the
18801903
/// value of the returned option.
18811904
#[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>
18831906
where
18841907
F: FnOnce(&K, V) -> Option<V>,
18851908
{
18861909
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+
}
18911925
}
18921926
}
18931927

0 commit comments

Comments
 (0)