Skip to content
Closed
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
26 changes: 26 additions & 0 deletions src/arrayvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,32 @@ impl<T, const CAP: usize> std::convert::TryFrom<&[T]> for ArrayVec<T, CAP>
}


/// Try to create an `ArrayVec` from a vector. This will return an error if the slice was too big to
/// fit.
///
/// ```
/// use arrayvec::ArrayVec;
/// use std::convert::TryInto as _;
///
/// let array: ArrayVec<_, 4> = vec![1, 2, 3].try_into().unwrap();
/// assert_eq!(array.len(), 3);
/// assert_eq!(array.capacity(), 4);
/// ```
impl<T, const CAP: usize> std::convert::TryFrom<crate::Vec<T>> for ArrayVec<T, CAP> {
type Error = CapacityError;

fn try_from(vec: crate::Vec<T>) -> Result<Self, Self::Error> {
if Self::CAPACITY < vec.len() {
Err(CapacityError::new(()))
} else {
let mut array = Self::new();
// SAFETY: Vector length has already been checked
unsafe { array.extend_from_iter::<_, false>(vec); }
Ok(array)
}
}
}

/// Iterate the `ArrayVec` with references to each element.
///
/// ```
Expand Down
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@
#![doc(html_root_url="https://docs.rs/arrayvec/0.7/")]
#![cfg_attr(not(feature="std"), no_std)]

#[cfg(not(feature="std"))]
extern crate alloc;

#[cfg(feature="serde")]
extern crate serde;

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

#[cfg(feature="std")]
pub use std::vec::Vec as Vec;
#[cfg(not(feature="std"))]
pub use alloc::vec::Vec as Vec;

Comment on lines +21 to +34
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible to avoid all cfg by simply declaring extern crate alloc; regardless of no_std.

pub(crate) type LenUint = u32;

macro_rules! assert_capacity_limit {
Expand Down
11 changes: 8 additions & 3 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ use arrayvec::ArrayString;
use std::mem;
use arrayvec::CapacityError;

use std::collections::HashMap;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This import is only used inside test_string



#[test]
fn test_simple() {
use std::ops::Add;
Expand Down Expand Up @@ -523,6 +520,7 @@ fn array_clone_from() {
#[cfg(feature="std")]
#[test]
fn test_string() {
use std::collections::HashMap;
use std::error::Error;

let text = "hello world";
Expand Down Expand Up @@ -790,4 +788,11 @@ fn test_arraystring_zero_filled_has_some_sanity_checks() {
let string = ArrayString::<4>::zero_filled();
assert_eq!(string.as_str(), "\0\0\0\0");
assert_eq!(string.len(), 4);
}

#[test]
fn try_from_vec_returns_correct_values() {
use core::convert::TryFrom;
assert!(ArrayVec::<_, 1>::try_from(vec![1]).is_ok());
assert!(ArrayVec::<_, 1>::try_from(vec![1, 2]).is_err());
}