Skip to content

Commit ab6be3d

Browse files
committed
Added dedup_by_key
1 parent a97c06b commit ab6be3d

1 file changed

Lines changed: 19 additions & 6 deletions

File tree

src/arrayvec.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -740,16 +740,18 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
740740
pub fn as_mut_ptr(&mut self) -> *mut T {
741741
ArrayVecImpl::as_mut_ptr(self)
742742
}
743-
}
744743

745-
impl<T: PartialEq, const CAP: usize> ArrayVec<T, CAP> {
746-
/// Removes consecutive repeated elements in the vector according to the
747-
/// [`PartialEq`] trait implementation.
744+
/// Removes all but the first of consecutive elements in the vector that resolve to the same
745+
/// key.
748746
///
749747
/// If the vector is sorted, this removes all duplicates.
750748
#[inline]
751-
pub fn dedup(&mut self) {
752-
self.dedup_by(|a, b| a == b)
749+
pub fn dedup_by_key<F, K>(&mut self, mut key: F)
750+
where
751+
F: FnMut(&mut T) -> K,
752+
K: PartialEq,
753+
{
754+
self.dedup_by(|a, b| key(a) == key(b))
753755
}
754756

755757
/// Removes all but the first of consecutive elements in the vector satisfying a given equality
@@ -890,6 +892,17 @@ impl<T: PartialEq, const CAP: usize> ArrayVec<T, CAP> {
890892
}
891893
}
892894

895+
impl<T: PartialEq, const CAP: usize> ArrayVec<T, CAP> {
896+
/// Removes consecutive repeated elements in the vector according to the
897+
/// [`PartialEq`] trait implementation.
898+
///
899+
/// If the vector is sorted, this removes all duplicates.
900+
#[inline]
901+
pub fn dedup(&mut self) {
902+
self.dedup_by(|a, b| a == b)
903+
}
904+
}
905+
893906
impl<T, const CAP: usize> ArrayVecImpl for ArrayVec<T, CAP> {
894907
type Item = T;
895908
const CAPACITY: usize = CAP;

0 commit comments

Comments
 (0)