Skip to content

Commit 8dd8f8b

Browse files
authored
Add alloc feature with Box/Vec conversions (#136)
Adds `TryFrom` impls which convert from `Box`/`Vec`. The main impetus for this is #114 where it's noted such conversions work for `[T; N]` but don't work for the `Array` type, where we are trying to make a type which works as much like core arrays as possible. Closes #114
1 parent 0450c2b commit 8dd8f8b

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ zeroize = { version = "1.8", optional = true, default-features = false }
2929
bincode = { version = "2", features = ["serde"] }
3030

3131
[features]
32+
alloc = []
3233
extra-sizes = []
3334

3435
[package.metadata.docs.rs]

src/lib.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@
117117
//! If you have any questions, please
118118
//! [start a discussion](https://github.com/RustCrypto/hybrid-array/discussions).
119119
120+
#[cfg(feature = "alloc")]
121+
extern crate alloc;
122+
120123
pub mod sizes;
121124

122125
mod from_fn;
@@ -829,6 +832,62 @@ where
829832
}
830833
}
831834

835+
#[cfg(feature = "alloc")]
836+
impl<T, U> TryFrom<alloc::boxed::Box<[T]>> for Array<T, U>
837+
where
838+
Self: Clone,
839+
U: ArraySize,
840+
{
841+
type Error = TryFromSliceError;
842+
843+
#[inline]
844+
fn try_from(b: alloc::boxed::Box<[T]>) -> Result<Self, TryFromSliceError> {
845+
Self::try_from(&*b)
846+
}
847+
}
848+
849+
#[cfg(feature = "alloc")]
850+
impl<'a, T, U> TryFrom<&'a alloc::boxed::Box<[T]>> for Array<T, U>
851+
where
852+
Self: Clone,
853+
U: ArraySize,
854+
{
855+
type Error = TryFromSliceError;
856+
857+
#[inline]
858+
fn try_from(b: &'a alloc::boxed::Box<[T]>) -> Result<Self, TryFromSliceError> {
859+
Self::try_from(&**b)
860+
}
861+
}
862+
863+
#[cfg(feature = "alloc")]
864+
impl<T, U> TryFrom<alloc::vec::Vec<T>> for Array<T, U>
865+
where
866+
Self: Clone,
867+
U: ArraySize,
868+
{
869+
type Error = TryFromSliceError;
870+
871+
#[inline]
872+
fn try_from(v: alloc::vec::Vec<T>) -> Result<Self, TryFromSliceError> {
873+
Self::try_from(v.as_slice())
874+
}
875+
}
876+
877+
#[cfg(feature = "alloc")]
878+
impl<'a, T, U> TryFrom<&'a alloc::vec::Vec<T>> for Array<T, U>
879+
where
880+
Self: Clone,
881+
U: ArraySize,
882+
{
883+
type Error = TryFromSliceError;
884+
885+
#[inline]
886+
fn try_from(v: &'a alloc::vec::Vec<T>) -> Result<Self, TryFromSliceError> {
887+
Self::try_from(v.as_slice())
888+
}
889+
}
890+
832891
impl<'a, T, U> TryFrom<&'a [T]> for &'a Array<T, U>
833892
where
834893
U: ArraySize,

0 commit comments

Comments
 (0)