Skip to content

Commit 3e2113c

Browse files
committed
Add From<Array> 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<T>`. The borrowed conversion bounds on `T: Clone` ala the upstream impls in liballoc.
1 parent f7c4828 commit 3e2113c

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

.github/workflows/hybrid-array.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ jobs:
3737
toolchain: ${{ matrix.rust }}
3838
targets: ${{ matrix.target }}
3939
- run: cargo build --no-default-features --target ${{ matrix.target }}
40+
- run: cargo build --no-default-features --target ${{ matrix.target }} --features alloc
4041
- run: cargo build --no-default-features --target ${{ matrix.target }} --features bytemuck
4142
- run: cargo build --no-default-features --target ${{ matrix.target }} --features extra-sizes
4243
- run: cargo build --no-default-features --target ${{ matrix.target }} --features serde
@@ -110,6 +111,7 @@ jobs:
110111
with:
111112
toolchain: ${{ matrix.toolchain }}
112113
- run: cargo test
114+
- run: cargo test --features alloc
113115
- run: cargo test --features bytemuck
114116
- run: cargo test --features extra-sizes
115117
- run: cargo test --features serde

src/lib.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,52 @@ where
666666
}
667667
}
668668

669+
#[cfg(feature = "alloc")]
670+
impl<T, U> From<Array<T, U>> for alloc::boxed::Box<[T]>
671+
where
672+
U: ArraySize,
673+
{
674+
#[inline]
675+
fn from(array: Array<T, U>) -> alloc::boxed::Box<[T]> {
676+
alloc::vec::Vec::from(array).into_boxed_slice()
677+
}
678+
}
679+
680+
#[cfg(feature = "alloc")]
681+
impl<T, U> From<&Array<T, U>> for alloc::boxed::Box<[T]>
682+
where
683+
T: Clone,
684+
U: ArraySize,
685+
{
686+
#[inline]
687+
fn from(array: &Array<T, U>) -> alloc::boxed::Box<[T]> {
688+
array.as_slice().into()
689+
}
690+
}
691+
692+
#[cfg(feature = "alloc")]
693+
impl<T, U> From<Array<T, U>> for alloc::vec::Vec<T>
694+
where
695+
U: ArraySize,
696+
{
697+
#[inline]
698+
fn from(array: Array<T, U>) -> alloc::vec::Vec<T> {
699+
array.into_iter().collect()
700+
}
701+
}
702+
703+
#[cfg(feature = "alloc")]
704+
impl<T, U> From<&Array<T, U>> for alloc::vec::Vec<T>
705+
where
706+
T: Clone,
707+
U: ArraySize,
708+
{
709+
#[inline]
710+
fn from(array: &Array<T, U>) -> alloc::vec::Vec<T> {
711+
array.as_slice().into()
712+
}
713+
}
714+
669715
impl<T, U> Hash for Array<T, U>
670716
where
671717
T: Hash,

0 commit comments

Comments
 (0)