allow items that implement Clone
This commit is contained in:
+9
-4
@@ -715,19 +715,24 @@ impl<A: Array> From<A> for ArrayVec<A> {
|
|||||||
/// use arrayvec::ArrayVec;
|
/// use arrayvec::ArrayVec;
|
||||||
/// use std::convert::TryInto as _;
|
/// use std::convert::TryInto as _;
|
||||||
///
|
///
|
||||||
/// let mut array: ArrayVec<[_; 4]> = (&[1, 2, 3] as &[_]).try_into().unwrap();
|
/// let array: ArrayVec<[_; 4]> = (&[1, 2, 3] as &[_]).try_into().unwrap();
|
||||||
/// assert_eq!(array.len(), 3);
|
/// assert_eq!(array.len(), 3);
|
||||||
/// assert_eq!(array.capacity(), 4);
|
/// assert_eq!(array.capacity(), 4);
|
||||||
/// ```
|
/// ```
|
||||||
impl<A: Array> std::convert::TryFrom<&[A::Item]> for ArrayVec<A>
|
impl<A: Array> std::convert::TryFrom<&[A::Item]> for ArrayVec<A>
|
||||||
where
|
where
|
||||||
A::Item: Copy,
|
A::Item: Clone,
|
||||||
{
|
{
|
||||||
type Error = CapacityError;
|
type Error = CapacityError;
|
||||||
|
|
||||||
fn try_from(slice: &[A::Item]) -> Result<Self, Self::Error> {
|
fn try_from(slice: &[A::Item]) -> Result<Self, Self::Error> {
|
||||||
let mut array = Self::new();
|
if A::CAPACITY < slice.len() {
|
||||||
array.try_extend_from_slice(slice).map(|()| array)
|
Err(CapacityError::new(()))
|
||||||
|
} else {
|
||||||
|
let mut array = Self::new();
|
||||||
|
array.extend(slice.iter().cloned());
|
||||||
|
Ok(array)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -65,6 +65,15 @@ fn test_extend_from_slice_error() {
|
|||||||
assert_matches!(res, Err(_));
|
assert_matches!(res, Err(_));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_try_from_slice_error() {
|
||||||
|
use arrayvec::ArrayVec;
|
||||||
|
use std::convert::TryInto as _;
|
||||||
|
|
||||||
|
let res: Result<ArrayVec<[_; 2]>, _> = (&[1, 2, 3] as &[_]).try_into();
|
||||||
|
assert_matches!(res, Err(_));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_u16_index() {
|
fn test_u16_index() {
|
||||||
const N: usize = 4096;
|
const N: usize = 4096;
|
||||||
|
|||||||
Reference in New Issue
Block a user