Skip to content

Commit

Permalink
alter retain's closure signature from &usize to usize
Browse files Browse the repository at this point in the history
`retain`'s signature is based on the signature for other collections,
which provide a borrowed value. In our case, an owned usize is more
convenient in many ways and this change provides that.

Matklad pointed out that `vector`'s signature is considered a bug and
`FnMut(&mut T)` is preferred. For mset, which has invariants that must
be enforced at runtime, I don't think we want `retain` to alter the
multiplicity. Instead we go with an owned usize for simplicity and
consistency with the rest of the interface
  • Loading branch information
lonnen committed Nov 13, 2020
1 parent 38051a5 commit 635ac5e
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,24 +674,24 @@ impl<T: Hash + Eq, S: BuildHasher> MultiSet<T, S> {
/// Retains only the elements specified by the predicate.
///
/// In other words, remove all pairs `(elem, multiplicty)` such that
/// `f(&k, &mut v)` returns `false`.
/// `f(&k, usize)` returns `false`.
///
/// # Examples
///
/// ```
/// use mset::MultiSet;
///
/// let mut mset: MultiSet<_> = vec!['a', 'b', 'c', 'b', 'a'].iter().cloned().collect();
/// mset.retain(|_, m: &usize| m % 2 == 0);
/// mset.retain(|_, m: usize| m % 2 == 0);
///
/// assert_eq!(mset.elements().len(), 2);
/// assert_eq!(mset.len(), 4);
/// ```
pub fn retain<F>(&mut self, mut f: F)
where
F: FnMut(&T, &usize) -> bool,
F: FnMut(&T, usize) -> bool,
{
self.elem_counts.retain(|e, m| f(e, m));
self.elem_counts.retain(|e, m| f(e, *m));
}

/// Clears the multiset, returning all elements in an iterator.
Expand Down

0 comments on commit 635ac5e

Please sign in to comment.