Skip to content

Commit

Permalink
Added append and try_append with tests to implement bluss#103
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Feb 11, 2020
1 parent 4f0faaf commit b97c7ba
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,33 @@ impl<A: Array> ArrayVec<A> {
mem::forget(self);
}

/// Takes another array and appends as many items as possible to the end
/// of this array and drops the rest. Use `try_append` for all or nothing.
pub fn append<B: Array<Item = A::Item>>(&mut self, mut other: ArrayVec<B>) {
unsafe {
let count = other.len().min(A::CAPACITY - self.len());
std::ptr::copy_nonoverlapping(other.as_ptr(), self.get_unchecked_ptr(self.len()), count);
if std::mem::needs_drop::<A::Item>() {
for i in count..other.len() {
std::ptr::drop_in_place(other.get_unchecked_ptr(i));
}
}
std::mem::forget(other);
self.set_len(self.len() + count);
}
}

/// Takes another array and appends all of the items if there is room
/// otherwise it returns the array as an Err
pub fn try_append<B: Array<Item = A::Item>>(&mut self, other: ArrayVec<B>) -> Result<(), ArrayVec<B>> {
if other.len() > A::CAPACITY - self.len() {
Err(other)
} else {
self.append(other);
Ok(())
}
}

/// Return a slice containing all elements of the vector.
pub fn as_slice(&self) -> &[A::Item] {
self
Expand Down
60 changes: 59 additions & 1 deletion tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::mem;
use arrayvec::CapacityError;

use std::collections::HashMap;

use std::sync::atomic::{AtomicUsize, Ordering};

#[test]
fn test_simple() {
Expand Down Expand Up @@ -678,3 +678,61 @@ fn test_extend_zst() {
assert_eq!(&array[..], &[Z; 5]);
assert_eq!(array.len(), 5);
}

#[test]
fn test_append() {
let mut empty = ArrayVec::<[u64; 64]>::new();
let full: ArrayVec<[u64; 32]> = (0..32).collect();
empty.append(full);
assert_eq!(empty.len(), 32);
empty.iter().enumerate().for_each(|(i, x)|assert_eq!(i as u64, *x));

let mut empty = ArrayVec::<[u64; 32]>::new();
let full: ArrayVec<[u64; 64]> = (0..64).collect();
empty.append(full);
assert_eq!(empty.len(), 32);
empty.iter().enumerate().for_each(|(i, x)|assert_eq!(i as u64, *x));

let mut half_full: ArrayVec<[u64; 32]> = (0..15).collect();
let to_append: ArrayVec<[u64; 32]> = half_full.clone();
half_full.append(to_append);
assert_eq!(half_full.len(), 30);
half_full.iter().enumerate().for_each(|(i, x)|assert_eq!(i as u64 % 15, *x));
}

#[test]
fn test_append_drop() {
static DROP_COUNT: AtomicUsize = AtomicUsize::new(0);
struct DropWrapper(u64);
impl Drop for DropWrapper {
fn drop(&mut self) {
println!("Dropping {}", self.0);
DROP_COUNT.fetch_add(1, Ordering::SeqCst);
}
}
let mut empty = ArrayVec::<[DropWrapper; 32]>::new();
let full: ArrayVec<[DropWrapper; 64]> = (0..32).map(DropWrapper).collect();
empty.append(full);
assert_eq!(empty.len(), 32);
empty.iter().enumerate().for_each(|(i, x)|assert_eq!(i as u64, x.0));
assert_eq!(DROP_COUNT.load(Ordering::SeqCst), 0);
let full: ArrayVec<[DropWrapper; 64]> = (0..17).map(DropWrapper).collect();
empty.append(full);
assert_eq!(empty.len(), 32);
empty.iter().enumerate().for_each(|(i, x)|assert_eq!(i as u64, x.0));
assert_eq!(DROP_COUNT.load(Ordering::SeqCst), 17);
}

#[test]
fn test_try_append() {
let mut empty = ArrayVec::<[usize; 32]>::new();
let full: ArrayVec<[usize; 16]> = (0..16).collect();
empty.try_append(full).expect("Should be able to add all 16 elements");
assert_eq!(empty.len(), 16);
let full: ArrayVec<[usize; 32]> = (0..32).collect();
let mut full = empty.try_append(full).expect_err("Should not be able to add 32 elements");
assert_eq!(empty.len(), 16);
full.drain(15..32);
empty.try_append(full).expect("Should be able to add 15 elements");
assert_eq!(empty.len(), 31);
}

0 comments on commit b97c7ba

Please sign in to comment.