From 3443a957c12be80234fd8c1226222afa46c71e09 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Mon, 6 Apr 2026 08:48:28 -0600 Subject: [PATCH] Remove needless `Clone` bounds on `Box`/`Vec` conversions This changes the `TryFrom` impls for `Array` which convert from an owned boxed slice or `Vec` into an `Array` to not require `Clone` bounds, because we can move the owned data into the array. Also adds tests. --- src/lib.rs | 8 ++++---- tests/mod.rs | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) 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]);