From 3e2113c77677c6a816d484dbc6c3240e98869432 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Mon, 1 Sep 2025 12:28:16 -0600 Subject: [PATCH 1/2] Add `From` conversions for `Box`/`Vec` Gated on the `alloc` feature, added in #136 These are cooresponding changes which allow conversions the other way. The owned conversion moves array elements into a `Box<[T]>` or `Vec`. The borrowed conversion bounds on `T: Clone` ala the upstream impls in liballoc. --- .github/workflows/hybrid-array.yml | 2 ++ src/lib.rs | 46 ++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/.github/workflows/hybrid-array.yml b/.github/workflows/hybrid-array.yml index 59844cd..ab907d1 100644 --- a/.github/workflows/hybrid-array.yml +++ b/.github/workflows/hybrid-array.yml @@ -37,6 +37,7 @@ jobs: toolchain: ${{ matrix.rust }} targets: ${{ matrix.target }} - run: cargo build --no-default-features --target ${{ matrix.target }} + - run: cargo build --no-default-features --target ${{ matrix.target }} --features alloc - run: cargo build --no-default-features --target ${{ matrix.target }} --features bytemuck - run: cargo build --no-default-features --target ${{ matrix.target }} --features extra-sizes - run: cargo build --no-default-features --target ${{ matrix.target }} --features serde @@ -110,6 +111,7 @@ jobs: with: toolchain: ${{ matrix.toolchain }} - run: cargo test + - run: cargo test --features alloc - run: cargo test --features bytemuck - run: cargo test --features extra-sizes - run: cargo test --features serde diff --git a/src/lib.rs b/src/lib.rs index 7b14093..96a483c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -666,6 +666,52 @@ where } } +#[cfg(feature = "alloc")] +impl From> for alloc::boxed::Box<[T]> +where + U: ArraySize, +{ + #[inline] + fn from(array: Array) -> alloc::boxed::Box<[T]> { + alloc::vec::Vec::from(array).into_boxed_slice() + } +} + +#[cfg(feature = "alloc")] +impl From<&Array> for alloc::boxed::Box<[T]> +where + T: Clone, + U: ArraySize, +{ + #[inline] + fn from(array: &Array) -> alloc::boxed::Box<[T]> { + array.as_slice().into() + } +} + +#[cfg(feature = "alloc")] +impl From> for alloc::vec::Vec +where + U: ArraySize, +{ + #[inline] + fn from(array: Array) -> alloc::vec::Vec { + array.into_iter().collect() + } +} + +#[cfg(feature = "alloc")] +impl From<&Array> for alloc::vec::Vec +where + T: Clone, + U: ArraySize, +{ + #[inline] + fn from(array: &Array) -> alloc::vec::Vec { + array.as_slice().into() + } +} + impl Hash for Array where T: Hash, From b413befa174d69dfd8664ec2c20d3cee43115d21 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Mon, 1 Sep 2025 12:31:59 -0600 Subject: [PATCH 2/2] Collect works on Box<[T]> too --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 96a483c..d6bdd93 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -673,7 +673,7 @@ where { #[inline] fn from(array: Array) -> alloc::boxed::Box<[T]> { - alloc::vec::Vec::from(array).into_boxed_slice() + array.into_iter().collect() } }