Factor out push_unchecked()
This commit is contained in:
+29
-3
@@ -164,10 +164,8 @@ impl<A: Array> ArrayVec<A> {
|
|||||||
/// ```
|
/// ```
|
||||||
pub fn push(&mut self, element: A::Item) -> Option<A::Item> {
|
pub fn push(&mut self, element: A::Item) -> Option<A::Item> {
|
||||||
if self.len() < A::capacity() {
|
if self.len() < A::capacity() {
|
||||||
let len = self.len();
|
|
||||||
unsafe {
|
unsafe {
|
||||||
ptr::write(self.get_unchecked_mut(len), element);
|
self.push_unchecked(element);
|
||||||
self.set_len(len + 1);
|
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
} else {
|
} 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`.
|
/// Insert `element` in position `index`.
|
||||||
///
|
///
|
||||||
/// Shift up all elements after `index`. If any is pushed out, it is returned.
|
/// Shift up all elements after `index`. If any is pushed out, it is returned.
|
||||||
|
|||||||
Reference in New Issue
Block a user