Skip to content

Commit 5e4f858

Browse files
committed
Update according suggested changes
1 parent 9b33825 commit 5e4f858

File tree

1 file changed

+37
-12
lines changed

1 file changed

+37
-12
lines changed

src/map.rs

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,10 @@ impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
755755
/// Clears the map, returning all key-value pairs as an iterator. Keeps the
756756
/// allocated memory for reuse.
757757
///
758+
/// If the returned iterator is dropped before being fully consumed, it
759+
/// drops the remaining key-value pairs. The returned iterator keeps a
760+
/// mutable borrow on the vector to optimize its implementation.
761+
///
758762
/// # Examples
759763
///
760764
/// ```
@@ -773,7 +777,18 @@ impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
773777
/// // As we can see, the map is empty and contains no element.
774778
/// assert!(a.is_empty() && a.len() == 0);
775779
/// // But map capacity is equal to old one.
776-
/// assert!(a.capacity() == capacity_before_drain);
780+
/// assert_eq!(a.capacity(), capacity_before_drain);
781+
///
782+
/// let mut a = HashMap::new();
783+
/// a.insert(1, "a");
784+
/// a.insert(2, "b");
785+
///
786+
/// { // Iterator is dropped without being consumed.
787+
/// let d = a.drain();
788+
/// }
789+
///
790+
/// // But the map is empty even if we do not use Drain iterator.
791+
/// assert!(a.is_empty());
777792
/// ```
778793
#[cfg_attr(feature = "inline-more", inline)]
779794
pub fn drain(&mut self) -> Drain<'_, K, V, A> {
@@ -785,7 +800,8 @@ impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
785800
/// Retains only the elements specified by the predicate. Keeps the
786801
/// allocated memory for reuse.
787802
///
788-
/// In other words, remove all pairs `(k, v)` such that `f(&k,&mut v)` returns `false`.
803+
/// In other words, remove all pairs `(k, v)` such that `f(&k, &mut v)` returns `false`.
804+
/// The elements are visited in unsorted (and unspecified) order.
789805
///
790806
/// # Examples
791807
///
@@ -801,7 +817,7 @@ impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
801817
/// // We can see, that the number of elements inside map is changed.
802818
/// assert_eq!(map.len(), 4);
803819
/// // But map capacity is equal to old one.
804-
/// assert!(map.capacity() == capacity_before_retain);
820+
/// assert_eq!(map.capacity(), capacity_before_retain);
805821
///
806822
/// let mut vec: Vec<(i32, i32)> = map.iter().map(|(&k, &v)| (k, v)).collect();
807823
/// vec.sort_unstable();
@@ -825,12 +841,19 @@ impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
825841
/// Drains elements which are true under the given predicate,
826842
/// and returns an iterator over the removed items.
827843
///
828-
/// In other words, move all pairs `(k, v)` such that `f(&k,&mut v)` returns `true` out
844+
/// In other words, move all pairs `(k, v)` such that `f(&k, &mut v)` returns `true` out
829845
/// into another iterator.
830846
///
847+
/// Note that `drain_filter` lets you mutate every value in the filter closure, regardless of
848+
/// whether you choose to keep or remove it.
849+
///
831850
/// When the returned DrainedFilter is dropped, any remaining elements that satisfy
832851
/// the predicate are dropped from the table.
833852
///
853+
/// It is unspecified how many more elements will be subjected to the closure
854+
/// if a panic occurs in the closure, or a panic occurs while dropping an element,
855+
/// or if the `DrainFilter` value is leaked.
856+
///
834857
/// Keeps the allocated memory for reuse.
835858
///
836859
/// # Examples
@@ -851,6 +874,16 @@ impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
851874
/// assert_eq!(odds, vec![1, 3, 5, 7]);
852875
/// // Map capacity is equal to old one.
853876
/// assert_eq!(map.capacity(), capacity_before_drain_filter);
877+
///
878+
/// let mut map: HashMap<i32, i32> = (0..8).map(|x| (x, x)).collect();
879+
///
880+
/// { // Iterator is dropped without being consumed.
881+
/// let d = map.drain_filter(|k, _v| k % 2 != 0);
882+
/// }
883+
///
884+
/// // But the map lens have been reduced by half
885+
/// // even if we do not use DrainFilter iterator.
886+
/// assert_eq!(map.len(), 4);
854887
/// ```
855888
#[cfg_attr(feature = "inline-more", inline)]
856889
pub fn drain_filter<F>(&mut self, f: F) -> DrainFilter<'_, K, V, F, A>
@@ -1020,14 +1053,6 @@ where
10201053
/// },
10211054
/// _ => panic!(),
10221055
/// }
1023-
///
1024-
/// match map.try_reserve(usize::MAX / 64) {
1025-
/// Err(error) => match error {
1026-
/// TryReserveError::AllocError { .. } => {}
1027-
/// _ => panic!("TryReserveError::CapacityOverflow ?"),
1028-
/// },
1029-
/// _ => panic!(),
1030-
/// }
10311056
/// # }
10321057
/// # fn main() {
10331058
/// # #[cfg(not(miri))]

0 commit comments

Comments
 (0)