FEAT: Rename .remove_opt() to .pop_at()

This commit is contained in:
bluss
2017-08-05 18:22:06 +02:00
parent cf76a83a7f
commit 313ebe8335
2 changed files with 23 additions and 8 deletions
+7 -8
View File
@@ -403,7 +403,7 @@ impl<A: Array> ArrayVec<A> {
/// assert_eq!(&array[..], &[2, 3]); /// assert_eq!(&array[..], &[2, 3]);
/// ``` /// ```
pub fn remove(&mut self, index: usize) -> A::Item { pub fn remove(&mut self, index: usize) -> A::Item {
self.remove_opt(index) self.pop_at(index)
.unwrap_or_else(|| { .unwrap_or_else(|| {
panic_oob!("remove", index, self.len()) panic_oob!("remove", index, self.len())
}) })
@@ -411,22 +411,21 @@ impl<A: Array> ArrayVec<A> {
/// Remove the element at `index` and shift down the following elements. /// Remove the element at `index` and shift down the following elements.
/// ///
/// This is a checked version of `.remove(index)`. Returns `None` if the /// This is a checked version of `.remove(index)`. Returns `None` if there
/// index is greater or equal to the length of the vector. Otherwise, return /// is no element at `index`. Otherwise, return the element inside `Some`.
/// the element inside `Some`.
/// ///
/// ``` /// ```
/// use arrayvec::ArrayVec; /// use arrayvec::ArrayVec;
/// ///
/// let mut array = ArrayVec::from([1, 2, 3]); /// let mut array = ArrayVec::from([1, 2, 3]);
/// ///
/// assert!(array.remove_opt(0).is_some()); /// assert!(array.pop_at(0).is_some());
/// assert_eq!(&array[..], &[2, 3]); /// assert_eq!(&array[..], &[2, 3]);
/// ///
/// assert!(array.remove_opt(2).is_none()); /// assert!(array.pop_at(2).is_none());
/// assert!(array.remove_opt(10).is_none()); /// assert!(array.pop_at(10).is_none());
/// ``` /// ```
pub fn remove_opt(&mut self, index: usize) -> Option<A::Item> { pub fn pop_at(&mut self, index: usize) -> Option<A::Item> {
if index >= self.len() { if index >= self.len() {
None None
} else { } else {
+16
View File
@@ -434,3 +434,19 @@ fn test_drop_in_insert() {
} }
assert_eq!(flag.get(), 3); assert_eq!(flag.get(), 3);
} }
#[test]
fn test_pop_at() {
let mut v = ArrayVec::<[String; 4]>::new();
let s = String::from;
v.push(s("a"));
v.push(s("b"));
v.push(s("c"));
v.push(s("d"));
assert_eq!(v.pop_at(4), None);
assert_eq!(v.pop_at(1), Some(s("b")));
assert_eq!(v.pop_at(1), Some(s("c")));
assert_eq!(v.pop_at(2), None);
assert_eq!(&v[..], &["a", "d"]);
}