arrayvec: Reorder method pop

This commit is contained in:
bluss
2016-02-17 23:08:46 +01:00
parent 07b2ca2e1f
commit 49be63d9b8
+25 -25
View File
@@ -155,31 +155,6 @@ impl<A: Array> ArrayVec<A> {
}
}
/// Remove the last element in the vector.
///
/// Return `Some(` *element* `)` if the vector is non-empty, else `None`.
///
/// ```
/// use arrayvec::ArrayVec;
///
/// let mut array = ArrayVec::<[_; 2]>::new();
///
/// array.push(1);
///
/// assert_eq!(array.pop(), Some(1));
/// assert_eq!(array.pop(), None);
/// ```
pub fn pop(&mut self) -> Option<A::Item> {
if self.len() == 0 {
return None
}
unsafe {
let new_len = self.len() - 1;
self.set_len(new_len);
Some(ptr::read(self.get_unchecked_mut(new_len)))
}
}
/// Insert `element` in position `index`.
///
/// Shift up all elements after `index`. If any is pushed out, it is returned.
@@ -225,6 +200,31 @@ impl<A: Array> ArrayVec<A> {
ret
}
/// Remove the last element in the vector.
///
/// Return `Some(` *element* `)` if the vector is non-empty, else `None`.
///
/// ```
/// use arrayvec::ArrayVec;
///
/// let mut array = ArrayVec::<[_; 2]>::new();
///
/// array.push(1);
///
/// assert_eq!(array.pop(), Some(1));
/// assert_eq!(array.pop(), None);
/// ```
pub fn pop(&mut self) -> Option<A::Item> {
if self.len() == 0 {
return None
}
unsafe {
let new_len = self.len() - 1;
self.set_len(new_len);
Some(ptr::read(self.get_unchecked_mut(new_len)))
}
}
/// Remove the element at `index` and swap the last element into its place.
///
/// This operation is O(1).