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

Add From<[T; LEN]> for ArrayVec<T, CAP> #238

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
52 changes: 23 additions & 29 deletions src/arrayvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
/// ```
/// use arrayvec::ArrayVec;
///
/// let mut array = ArrayVec::from([1, 2, 3]);
/// let mut array = ArrayVec::<_, 3>::from([1, 2, 3]);
/// array.pop();
/// assert_eq!(array.len(), 2);
/// ```
Expand All @@ -115,7 +115,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
/// ```
/// use arrayvec::ArrayVec;
///
/// let mut array = ArrayVec::from([1]);
/// let mut array = ArrayVec::<_, 1>::from([1]);
/// array.pop();
/// assert_eq!(array.is_empty(), true);
/// ```
Expand All @@ -127,7 +127,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
/// ```
/// use arrayvec::ArrayVec;
///
/// let array = ArrayVec::from([1, 2, 3]);
/// let array = ArrayVec::<_, 3>::from([1, 2, 3]);
/// assert_eq!(array.capacity(), 3);
/// ```
#[inline(always)]
Expand All @@ -150,7 +150,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
/// ```
/// use arrayvec::ArrayVec;
///
/// let mut array = ArrayVec::from([1, 2, 3]);
/// let mut array = ArrayVec::<_, 3>::from([1, 2, 3]);
/// array.pop();
/// assert_eq!(array.remaining_capacity(), 1);
/// ```
Expand Down Expand Up @@ -236,7 +236,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
/// ```
/// use arrayvec::ArrayVec;
///
/// let mut array = ArrayVec::from([1, 2, 3, 4, 5]);
/// let mut array = ArrayVec::<_, 5>::from([1, 2, 3, 4, 5]);
/// array.truncate(3);
/// assert_eq!(&array[..], &[1, 2, 3]);
/// array.truncate(4);
Expand Down Expand Up @@ -356,7 +356,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
/// ```
/// use arrayvec::ArrayVec;
///
/// let mut array = ArrayVec::from([1, 2, 3]);
/// let mut array = ArrayVec::<_, 3>::from([1, 2, 3]);
///
/// assert_eq!(array.swap_remove(0), 1);
/// assert_eq!(&array[..], &[3, 2]);
Expand All @@ -381,7 +381,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
/// ```
/// use arrayvec::ArrayVec;
///
/// let mut array = ArrayVec::from([1, 2, 3]);
/// let mut array = ArrayVec::<_, 3>::from([1, 2, 3]);
///
/// assert_eq!(array.swap_pop(0), Some(1));
/// assert_eq!(&array[..], &[3, 2]);
Expand All @@ -406,7 +406,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
/// ```
/// use arrayvec::ArrayVec;
///
/// let mut array = ArrayVec::from([1, 2, 3]);
/// let mut array = ArrayVec::<_, 3>::from([1, 2, 3]);
///
/// let removed_elt = array.remove(0);
/// assert_eq!(removed_elt, 1);
Expand All @@ -427,7 +427,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
/// ```
/// use arrayvec::ArrayVec;
///
/// let mut array = ArrayVec::from([1, 2, 3]);
/// let mut array = ArrayVec::<_, 3>::from([1, 2, 3]);
///
/// assert!(array.pop_at(0).is_some());
/// assert_eq!(&array[..], &[2, 3]);
Expand All @@ -452,7 +452,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
/// ```
/// use arrayvec::ArrayVec;
///
/// let mut array = ArrayVec::from([1, 2, 3, 4]);
/// let mut array = ArrayVec::<_, 4>::from([1, 2, 3, 4]);
/// array.retain(|x| *x & 1 != 0 );
/// assert_eq!(&array[..], &[1, 3]);
/// ```
Expand Down Expand Up @@ -592,7 +592,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
/// ```
/// use arrayvec::ArrayVec;
///
/// let mut v1 = ArrayVec::from([1, 2, 3]);
/// let mut v1 = ArrayVec::<_, 3>::from([1, 2, 3]);
/// let v2: ArrayVec<_, 3> = v1.drain(0..2).collect();
/// assert_eq!(&v1[..], &[3]);
/// assert_eq!(&v2[..], &[1, 2]);
Expand Down Expand Up @@ -673,7 +673,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
/// ```
/// use arrayvec::ArrayVec;
///
/// let mut v = ArrayVec::from([0, 1, 2, 3]);
/// let mut v = ArrayVec::<_, 4>::from([0, 1, 2, 3]);
/// assert_eq!([0, 1, 2, 3], v.take().into_inner().unwrap());
/// assert!(v.is_empty());
/// ```
Expand Down Expand Up @@ -737,25 +737,19 @@ impl<T, const CAP: usize> DerefMut for ArrayVec<T, CAP> {
}
}

impl<T, const LEN: usize, const CAP: usize> From<[T; LEN]> for ArrayVec<T, CAP> {
fn from(array: [T; LEN]) -> Self {
assert!(LEN <= CAP);

/// Create an `ArrayVec` from an array.
///
/// ```
/// use arrayvec::ArrayVec;
///
/// let mut array = ArrayVec::from([1, 2, 3]);
/// assert_eq!(array.len(), 3);
/// assert_eq!(array.capacity(), 3);
/// ```
impl<T, const CAP: usize> From<[T; CAP]> for ArrayVec<T, CAP> {
fn from(array: [T; CAP]) -> Self {
let array = ManuallyDrop::new(array);
let mut vec = <ArrayVec<T, CAP>>::new();

unsafe {
(&*array as *const [T; CAP] as *const [MaybeUninit<T>; CAP])
.copy_to_nonoverlapping(&mut vec.xs as *mut [MaybeUninit<T>; CAP], 1);
vec.set_len(CAP);
(&*array as *const [T; LEN] as *const MaybeUninit<T>)
.copy_to_nonoverlapping(&mut vec.xs as *mut MaybeUninit<T>, LEN);
vec.set_len(LEN);
}

vec
}
}
Expand Down Expand Up @@ -794,7 +788,7 @@ impl<T, const CAP: usize> std::convert::TryFrom<&[T]> for ArrayVec<T, CAP>
/// ```
/// use arrayvec::ArrayVec;
///
/// let array = ArrayVec::from([1, 2, 3]);
/// let array = ArrayVec::<_, 3>::from([1, 2, 3]);
///
/// for elt in &array {
/// // ...
Expand All @@ -811,7 +805,7 @@ impl<'a, T: 'a, const CAP: usize> IntoIterator for &'a ArrayVec<T, CAP> {
/// ```
/// use arrayvec::ArrayVec;
///
/// let mut array = ArrayVec::from([1, 2, 3]);
/// let mut array = ArrayVec::<_, 3>::from([1, 2, 3]);
///
/// for elt in &mut array {
/// // ...
Expand All @@ -830,7 +824,7 @@ impl<'a, T: 'a, const CAP: usize> IntoIterator for &'a mut ArrayVec<T, CAP> {
/// ```
/// use arrayvec::ArrayVec;
///
/// for elt in ArrayVec::from([1, 2, 3]) {
/// for elt in ArrayVec::<_, 3>::from([1, 2, 3]) {
/// // ...
/// }
/// ```
Expand Down
23 changes: 12 additions & 11 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::collections::HashMap;
fn test_simple() {
use std::ops::Add;

let mut vec: ArrayVec<Vec<i32>, 3> = ArrayVec::new();
let mut vec: ArrayVec<Vec<_>, 3> = ArrayVec::new();

vec.push(vec![1, 2, 3, 4]);
vec.push(vec![10]);
Expand Down Expand Up @@ -87,7 +87,7 @@ fn test_u16_index() {

#[test]
fn test_iter() {
let mut iter = ArrayVec::from([1, 2, 3]).into_iter();
let mut iter = ArrayVec::<_, 3>::from([1, 2, 3]).into_iter();
assert_eq!(iter.size_hint(), (3, Some(3)));
assert_eq!(iter.next_back(), Some(3));
assert_eq!(iter.next(), Some(1));
Expand Down Expand Up @@ -333,7 +333,7 @@ fn test_still_works_with_option_arrayvec() {

#[test]
fn test_drain() {
let mut v = ArrayVec::from([0; 8]);
let mut v = ArrayVec::<_, 8>::from([0; 8]);
v.pop();
v.drain(0..7);
assert_eq!(&v[..], &[]);
Expand All @@ -350,7 +350,7 @@ fn test_drain() {

#[test]
fn test_drain_range_inclusive() {
let mut v = ArrayVec::from([0; 8]);
let mut v = ArrayVec::<_, 8>::from([0; 8]);
v.drain(0..=7);
assert_eq!(&v[..], &[]);

Expand All @@ -367,13 +367,13 @@ fn test_drain_range_inclusive() {
#[test]
#[should_panic]
fn test_drain_range_inclusive_oob() {
let mut v = ArrayVec::from([0; 0]);
let mut v = ArrayVec::<_, 0>::from([0; 0]);
v.drain(0..=0);
}

#[test]
fn test_retain() {
let mut v = ArrayVec::from([0; 8]);
let mut v = ArrayVec::<_, 8>::from([0; 8]);
for (i, elt) in v.iter_mut().enumerate() {
*elt = i;
}
Expand All @@ -391,7 +391,7 @@ fn test_retain() {
#[test]
#[should_panic]
fn test_drain_oob() {
let mut v = ArrayVec::from([0; 8]);
let mut v = ArrayVec::<_, 8>::from([0; 8]);
v.pop();
v.drain(0..8);
}
Expand Down Expand Up @@ -429,7 +429,7 @@ fn test_drop_panic_into_iter() {

#[test]
fn test_insert() {
let mut v = ArrayVec::from([]);
let mut v = ArrayVec::<_, 0>::from([]);
assert_matches!(v.try_push(1), Err(_));

let mut v = ArrayVec::<_, 3>::new();
Expand All @@ -445,15 +445,15 @@ fn test_insert() {
assert_eq!(&v[..], &[0, 1, 2]);
assert_matches!(ret2, Err(_));

let mut v = ArrayVec::from([2]);
let mut v = ArrayVec::<_, 1>::from([2]);
assert_matches!(v.try_insert(0, 1), Err(CapacityError { .. }));
assert_matches!(v.try_insert(1, 1), Err(CapacityError { .. }));
//assert_matches!(v.try_insert(2, 1), Err(CapacityError { .. }));
}

#[test]
fn test_into_inner_1() {
let mut v = ArrayVec::from([1, 2]);
let mut v = ArrayVec::<_, 2>::from([1, 2]);
v.pop();
let u = v.clone();
assert_eq!(v.into_inner(), Err(u));
Expand Down Expand Up @@ -681,7 +681,8 @@ fn test_pop_at() {

#[test]
fn test_sizes() {
let v = ArrayVec::from([0u8; 1 << 16]);
const LEN: usize = 1 << 16;
let v = ArrayVec::<_, LEN>::from([0u8; LEN]);
assert_eq!(vec![0u8; v.len()], &v[..]);
}

Expand Down