+24
@@ -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.
|
||||
///
|
||||
/// ```
|
||||
|
||||
Reference in New Issue
Block a user