diff --git a/src/lib.rs b/src/lib.rs index 249157d..76ada8a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -996,14 +996,14 @@ where #[cfg(feature = "alloc")] impl TryFrom> for Array where - Self: Clone, U: ArraySize, { type Error = TryFromSliceError; #[inline] fn try_from(b: alloc::boxed::Box<[T]>) -> Result { - Self::try_from(&*b) + check_slice_length::(b.as_ref())?; + Ok(Array::from_iter(b)) } } @@ -1024,14 +1024,14 @@ where #[cfg(feature = "alloc")] impl TryFrom> for Array where - Self: Clone, U: ArraySize, { type Error = TryFromSliceError; #[inline] fn try_from(v: alloc::vec::Vec) -> Result { - Self::try_from(v.as_slice()) + check_slice_length::(v.as_ref())?; + Ok(Array::from_iter(v)) } } diff --git a/tests/mod.rs b/tests/mod.rs index 17bb7cd..0698fe5 100644 --- a/tests/mod.rs +++ b/tests/mod.rs @@ -455,6 +455,23 @@ mod allocating { use super::*; use typenum::U4; + #[test] + fn array_try_from_boxed_slice() { + type A = Array; + assert!(A::try_from(vec![1].into_boxed_slice()).is_err()); + assert_eq!( + &A::try_from(vec![1, 2].into_boxed_slice()).unwrap(), + &[1, 2] + ); + } + + #[test] + fn array_try_from_vec() { + type A = Array; + assert!(A::try_from(vec![1]).is_err()); + assert_eq!(&A::try_from(vec![1, 2]).unwrap(), &[1, 2]); + } + #[test] fn boxed_slice_from_array() { let array: Array = Array([1, 2, 3, 4]);