Skip to content

Commit 16c8e4d

Browse files
committed
tests + docs
1 parent 6dc46b3 commit 16c8e4d

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/arrayvec.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,12 +640,25 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
640640
}
641641
}
642642

643+
/// Return the inner fixed size array.
644+
///
645+
/// Safety:
646+
/// This operation is safe if and only if length equals capacity.
643647
pub unsafe fn into_inner_unchecked(self) -> [T; CAP] {
644648
let self_ = ManuallyDrop::new(self);
645649
let array = ptr::read(self_.as_ptr() as *const [T; CAP]);
646650
array
647651
}
648652

653+
/// Returns the ArrayVec, replacing the original with a new empty ArrayVec.
654+
///
655+
/// ```
656+
/// use arrayvec::ArrayVec;
657+
///
658+
/// let mut v = ArrayVec::from([0, 1, 2, 3]);
659+
/// assert_eq!([0, 1, 2, 3], v.take().into_inner().unwrap());
660+
/// assert!(v.is_empty());
661+
/// ```
649662
pub fn take(&mut self) -> Self {
650663
mem::replace(self, Self::new())
651664
}

tests/tests.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,21 @@ fn test_drop() {
153153
assert_eq!(flag.get(), 3);
154154
}
155155

156+
// test take
157+
flag.set(0);
158+
{
159+
let mut array1 = ArrayVec::<_, 3>::new();
160+
array1.push(Bump(flag));
161+
array1.push(Bump(flag));
162+
array1.push(Bump(flag));
163+
let array2 = array1.take();
164+
assert_eq!(flag.get(), 0);
165+
drop(array1);
166+
assert_eq!(flag.get(), 0);
167+
drop(array2);
168+
assert_eq!(flag.get(), 3);
169+
}
170+
156171
// test cloning into_iter
157172
flag.set(0);
158173
{
@@ -461,6 +476,15 @@ fn test_into_inner_3() {
461476
assert_eq!(v.into_inner().unwrap(), [1, 2, 3, 4]);
462477
}
463478

479+
#[test]
480+
fn test_take() {
481+
let mut v1 = ArrayVec::<i32, 4>::new();
482+
v1.extend(1..=4);
483+
let v2 = v1.take();
484+
assert!(v1.into_inner().is_err());
485+
assert_eq!(v2.into_inner().unwrap(), [1, 2, 3, 4]);
486+
}
487+
464488
#[cfg(feature="std")]
465489
#[test]
466490
fn test_write() {

0 commit comments

Comments
 (0)