impl<A: Array> TryFrom<&[A::Item]> for ArrayVec<A>

For issue #106
This commit is contained in:
Paul Kernfeld
2020-05-12 12:04:09 -04:00
parent 04a3559eea
commit 488efd0b3e
+24
View File
@@ -708,6 +708,30 @@ impl<A: Array> From<A> for ArrayVec<A> {
} }
/// Try to create an `ArrayVec` from a slice. This will return an error if the slice was too big to
/// fit.
///
/// ```
/// use arrayvec::ArrayVec;
/// use std::convert::TryInto as _;
///
/// let mut array: ArrayVec<[_; 4]> = (&[1, 2, 3] as &[_]).try_into().unwrap();
/// assert_eq!(array.len(), 3);
/// assert_eq!(array.capacity(), 4);
/// ```
impl<A: Array> std::convert::TryFrom<&[A::Item]> for ArrayVec<A>
where
A::Item: Copy,
{
type Error = CapacityError;
fn try_from(slice: &[A::Item]) -> Result<Self, Self::Error> {
let mut array = Self::new();
array.try_extend_from_slice(slice).map(|()| array)
}
}
/// Iterate the `ArrayVec` with references to each element. /// Iterate the `ArrayVec` with references to each element.
/// ///
/// ``` /// ```