Skip to content

Commit cc6fe6d

Browse files
authored
Remove needless Clone bounds on Box/Vec conversions (#209)
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.
1 parent deb3e92 commit cc6fe6d

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -996,14 +996,14 @@ where
996996
#[cfg(feature = "alloc")]
997997
impl<T, U> TryFrom<alloc::boxed::Box<[T]>> for Array<T, U>
998998
where
999-
Self: Clone,
1000999
U: ArraySize,
10011000
{
10021001
type Error = TryFromSliceError;
10031002

10041003
#[inline]
10051004
fn try_from(b: alloc::boxed::Box<[T]>) -> Result<Self, TryFromSliceError> {
1006-
Self::try_from(&*b)
1005+
check_slice_length::<T, U>(b.as_ref())?;
1006+
Ok(Array::from_iter(b))
10071007
}
10081008
}
10091009

@@ -1024,14 +1024,14 @@ where
10241024
#[cfg(feature = "alloc")]
10251025
impl<T, U> TryFrom<alloc::vec::Vec<T>> for Array<T, U>
10261026
where
1027-
Self: Clone,
10281027
U: ArraySize,
10291028
{
10301029
type Error = TryFromSliceError;
10311030

10321031
#[inline]
10331032
fn try_from(v: alloc::vec::Vec<T>) -> Result<Self, TryFromSliceError> {
1034-
Self::try_from(v.as_slice())
1033+
check_slice_length::<T, U>(v.as_ref())?;
1034+
Ok(Array::from_iter(v))
10351035
}
10361036
}
10371037

tests/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,23 @@ mod allocating {
455455
use super::*;
456456
use typenum::U4;
457457

458+
#[test]
459+
fn array_try_from_boxed_slice() {
460+
type A = Array<u8, U2>;
461+
assert!(A::try_from(vec![1].into_boxed_slice()).is_err());
462+
assert_eq!(
463+
&A::try_from(vec![1, 2].into_boxed_slice()).unwrap(),
464+
&[1, 2]
465+
);
466+
}
467+
468+
#[test]
469+
fn array_try_from_vec() {
470+
type A = Array<u8, U2>;
471+
assert!(A::try_from(vec![1]).is_err());
472+
assert_eq!(&A::try_from(vec![1, 2]).unwrap(), &[1, 2]);
473+
}
474+
458475
#[test]
459476
fn boxed_slice_from_array() {
460477
let array: Array<u8, U4> = Array([1, 2, 3, 4]);

0 commit comments

Comments
 (0)