Skip to content

Commit 8778483

Browse files
committed
feat: API additions
1 parent 7b290b7 commit 8778483

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/arrayvec.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -636,14 +636,29 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
636636
if self.len() < self.capacity() {
637637
Err(self)
638638
} else {
639-
unsafe {
640-
let self_ = ManuallyDrop::new(self);
641-
let array = ptr::read(self_.as_ptr() as *const [T; CAP]);
642-
Ok(array)
643-
}
639+
unsafe { Ok(self.into_inner_unchecked()) }
644640
}
645641
}
646642

643+
pub unsafe fn into_inner_unchecked(self) -> [T; CAP] {
644+
let self_ = ManuallyDrop::new(self);
645+
let array = ptr::read(self_.as_ptr() as *const [T; CAP]);
646+
array
647+
}
648+
649+
pub fn take(&mut self) -> Option<[T; CAP]> {
650+
if self.len() < self.capacity() {
651+
None
652+
} else {
653+
unsafe { Some(self.take_unchecked()) }
654+
}
655+
}
656+
657+
pub unsafe fn take_unchecked(&mut self) -> [T; CAP] {
658+
let data = std::mem::replace(self, Self::new());
659+
data.into_inner_unchecked()
660+
}
661+
647662
/// Return a slice containing all elements of the vector.
648663
pub fn as_slice(&self) -> &[T] {
649664
ArrayVecImpl::as_slice(self)

0 commit comments

Comments
 (0)