From 6146d7e2473104a530a2f70b27786c38620f5440 Mon Sep 17 00:00:00 2001 From: Ronno Das Date: Fri, 21 Aug 2026 11:53:08 +0530 Subject: [PATCH 1/2] Add ArrayVec::pop_if() --- src/arrayvec.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/arrayvec.rs b/src/arrayvec.rs index f646b08..3178a71 100644 --- a/src/arrayvec.rs +++ b/src/arrayvec.rs @@ -350,6 +350,41 @@ impl ArrayVec { ArrayVecImpl::pop(self) } + /// Removes and returns the last element in the vector if the predicate returns `true`, or + /// [`None`] if the predicate returns `false` or the vector is empty (the predicate will not be + /// called in that case). + /// + /// # Examples + /// + /// ``` + /// use arrayvec::ArrayVec; + /// + /// let mut array = ArrayVec::::new(); + /// + /// assert_eq!(array.pop_if(|_| panic!()), None); + /// + /// array.push(1); + /// array.push(2); + /// + /// let pred = |x: &mut i32| *x % 2 == 0; + /// + /// assert_eq!(array.pop_if(pred), Some(2)); + /// assert_eq!(&array, [1].as_slice()); + /// assert_eq!(array.pop_if(pred), None); + /// ``` + pub fn pop_if(&mut self, predicate: impl FnOnce(&mut T) -> bool) -> Option { + if predicate(self.last_mut()?) { + // SAFETY: `last_mut()` must have returned `Some`, so `self.len() > 0` + unsafe { + let new_len = self.len() - 1; + self.set_len(new_len); + Some(ptr::read(self.as_ptr().add(new_len))) + } + } else { + None + } + } + /// Remove the element at `index` and swap the last element into its place. /// /// This operation is O(1). From 7147373a939c95b60e7d66f01cfd7f84d122f76e Mon Sep 17 00:00:00 2001 From: Ronno Das Date: Fri, 21 Aug 2026 12:06:44 +0530 Subject: [PATCH 2/2] rewrite slice equality for Rust 1.51 --- src/arrayvec.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/arrayvec.rs b/src/arrayvec.rs index 3178a71..a63e133 100644 --- a/src/arrayvec.rs +++ b/src/arrayvec.rs @@ -369,7 +369,7 @@ impl ArrayVec { /// let pred = |x: &mut i32| *x % 2 == 0; /// /// assert_eq!(array.pop_if(pred), Some(2)); - /// assert_eq!(&array, [1].as_slice()); + /// assert_eq!(&array[..], &[1]); /// assert_eq!(array.pop_if(pred), None); /// ``` pub fn pop_if(&mut self, predicate: impl FnOnce(&mut T) -> bool) -> Option {