Factor out push_unchecked()

This commit is contained in:
Niklas Fiekas
2017-05-13 18:16:06 +02:00
parent adf08656cc
commit d0ebfcd51f
+29 -3
View File
@@ -164,10 +164,8 @@ impl<A: Array> ArrayVec<A> {
/// ```
pub fn push(&mut self, element: A::Item) -> Option<A::Item> {
if self.len() < A::capacity() {
let len = self.len();
unsafe {
ptr::write(self.get_unchecked_mut(len), element);
self.set_len(len + 1);
self.push_unchecked(element);
}
None
} else {
@@ -175,6 +173,34 @@ impl<A: Array> ArrayVec<A> {
}
}
/// Push `element` to the end of the vector without checking the capacity.
///
/// It is up to the caller to ensure the capacity of the vector is
/// sufficiently large.
///
/// # Examples
///
/// ```
/// use arrayvec::ArrayVec;
///
/// let mut array = ArrayVec::<[_; 2]>::new();
///
/// if array.len() + 2 <= array.capacity() {
/// unsafe {
/// array.push_unchecked(1);
/// array.push_unchecked(2);
/// }
/// }
///
/// assert_eq!(&array[..], &[1, 2]);
/// ```
#[inline]
pub unsafe fn push_unchecked(&mut self, element: A::Item) {
let len = self.len();
ptr::write(self.get_unchecked_mut(len), element);
self.set_len(len + 1);
}
/// Insert `element` in position `index`.
///
/// Shift up all elements after `index`. If any is pushed out, it is returned.