Skip to content

Commit f3fee40

Browse files
committed
Implement TryFrom<Vec<T>> for ArrayVec<T, N>
1 parent e209a50 commit f3fee40

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

src/arrayvec.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,34 @@ impl<T, const CAP: usize> std::convert::TryFrom<&[T]> for ArrayVec<T, CAP>
772772
}
773773

774774

775+
/// Try to create an `ArrayVec` from a vector. This will return an error if the slice was too big to
776+
/// fit.
777+
///
778+
/// ```
779+
/// use arrayvec::ArrayVec;
780+
/// use std::convert::TryInto as _;
781+
///
782+
/// let array: ArrayVec<_, 4> = vec![1, 2, 3].try_into().unwrap();
783+
/// assert_eq!(array.len(), 3);
784+
/// assert_eq!(array.capacity(), 4);
785+
/// ```
786+
impl<T, const CAP: usize> std::convert::TryFrom<crate::Vec<T>> for ArrayVec<T, CAP>
787+
where T: Clone,
788+
{
789+
type Error = CapacityError;
790+
791+
fn try_from(vec: crate::Vec<T>) -> Result<Self, Self::Error> {
792+
if Self::CAPACITY < vec.len() {
793+
Err(CapacityError::new(()))
794+
} else {
795+
let mut array = Self::new();
796+
// SAFETY: Vector length has already been checked
797+
unsafe { array.extend_from_iter::<_, false>(vec); }
798+
Ok(array)
799+
}
800+
}
801+
}
802+
775803
/// Iterate the `ArrayVec` with references to each element.
776804
///
777805
/// ```

src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,20 @@
1818
#![doc(html_root_url="https://docs.rs/arrayvec/0.7/")]
1919
#![cfg_attr(not(feature="std"), no_std)]
2020

21+
#[cfg(not(feature="std"))]
22+
extern crate alloc;
23+
2124
#[cfg(feature="serde")]
2225
extern crate serde;
2326

2427
#[cfg(not(feature="std"))]
2528
extern crate core as std;
2629

30+
#[cfg(feature="std")]
31+
pub use std::vec::Vec as Vec;
32+
#[cfg(not(feature="std"))]
33+
pub use alloc::vec::Vec as Vec;
34+
2735
pub(crate) type LenUint = u32;
2836

2937
macro_rules! assert_capacity_limit {

tests/tests.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ use arrayvec::ArrayString;
66
use std::mem;
77
use arrayvec::CapacityError;
88

9-
use std::collections::HashMap;
10-
11-
129
#[test]
1310
fn test_simple() {
1411
use std::ops::Add;
@@ -523,6 +520,7 @@ fn array_clone_from() {
523520
#[cfg(feature="std")]
524521
#[test]
525522
fn test_string() {
523+
use std::collections::HashMap;
526524
use std::error::Error;
527525

528526
let text = "hello world";
@@ -790,4 +788,11 @@ fn test_arraystring_zero_filled_has_some_sanity_checks() {
790788
let string = ArrayString::<4>::zero_filled();
791789
assert_eq!(string.as_str(), "\0\0\0\0");
792790
assert_eq!(string.len(), 4);
791+
}
792+
793+
#[test]
794+
fn try_from_vec_returns_correct_values() {
795+
use core::convert::TryFrom;
796+
assert!(ArrayVec::<_, 1>::try_from(vec![1]).is_ok());
797+
assert!(ArrayVec::<_, 1>::try_from(vec![1, 2]).is_err());
793798
}

0 commit comments

Comments
 (0)