Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pop_unchecked #214

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/arrayvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,13 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
ArrayVecImpl::pop(self)
}

/// Remove the last element in the vector and return it.
///
/// It is up to the caller to ensure the the vector is not empty.
pub unsafe fn pop_unchecked(&mut self) -> T {
ArrayVecImpl::pop_unchecked(self)
}

/// Remove the element at `index` and swap the last element into its place.
///
/// This operation is O(1).
Expand Down
12 changes: 7 additions & 5 deletions src/arrayvec_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ pub(crate) trait ArrayVecImpl {
if self.len() == 0 {
return None;
}
unsafe {
let new_len = self.len() - 1;
self.set_len(new_len);
Some(ptr::read(self.as_ptr().add(new_len)))
}
unsafe { Some(self.pop_unchecked()) }
}

unsafe fn pop_unchecked(&mut self) -> Self::Item {
let new_len = self.len() - 1;
self.set_len(new_len);
ptr::read(self.as_ptr().add(new_len))
}

fn clear(&mut self) {
Expand Down