1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use alloc::{boxed::Box, vec::Vec};
use crate::{ArrayLength, GenericArray, LengthError};
impl<T, N: ArrayLength> TryFrom<Vec<T>> for GenericArray<T, N> {
type Error = crate::LengthError;
fn try_from(v: Vec<T>) -> Result<Self, Self::Error> {
if v.len() != N::USIZE {
return Err(crate::LengthError);
}
unsafe {
let mut destination = crate::ArrayBuilder::new();
let (dst_iter, position) = destination.iter_position();
dst_iter.zip(v).for_each(|(dst, src)| {
dst.write(src);
*position += 1;
});
Ok(destination.into_inner())
}
}
}
impl<T, N: ArrayLength> GenericArray<T, N> {
#[inline]
pub fn into_boxed_slice(self: Box<GenericArray<T, N>>) -> Box<[T]> {
unsafe {
Box::from_raw(core::ptr::slice_from_raw_parts_mut(
Box::into_raw(self) as *mut T,
N::USIZE,
))
}
}
#[inline]
pub fn into_vec(self: Box<GenericArray<T, N>>) -> Vec<T> {
Vec::from(self.into_boxed_slice())
}
#[inline]
pub fn try_from_boxed_slice(slice: Box<[T]>) -> Result<Box<GenericArray<T, N>>, LengthError> {
if slice.len() != N::USIZE {
return Err(LengthError);
}
Ok(unsafe { Box::from_raw(Box::into_raw(slice) as *mut _) })
}
#[inline]
pub fn try_from_vec(vec: Vec<T>) -> Result<Box<GenericArray<T, N>>, LengthError> {
Self::try_from_boxed_slice(vec.into_boxed_slice())
}
#[inline]
pub fn default_boxed() -> Box<GenericArray<T, N>>
where
T: Default,
{
Box::<GenericArray<T, N>>::generate(|_| T::default())
}
}
impl<T, N: ArrayLength> TryFrom<Box<[T]>> for GenericArray<T, N> {
type Error = crate::LengthError;
#[inline]
fn try_from(value: Box<[T]>) -> Result<Self, Self::Error> {
Vec::from(value).try_into()
}
}
impl<T, N: ArrayLength> From<GenericArray<T, N>> for Box<[T]> {
fn from(value: GenericArray<T, N>) -> Self {
Box::new(value).into_boxed_slice()
}
}
impl<T, N: ArrayLength> From<GenericArray<T, N>> for Vec<T> {
#[inline]
fn from(value: GenericArray<T, N>) -> Self {
Box::<[T]>::from(value).into()
}
}
impl<T, N: ArrayLength> IntoIterator for Box<GenericArray<T, N>> {
type IntoIter = alloc::vec::IntoIter<T>;
type Item = T;
fn into_iter(self) -> Self::IntoIter {
GenericArray::into_vec(self).into_iter()
}
}
impl<T, N: ArrayLength> FromIterator<T> for Box<GenericArray<T, N>> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
let mut iter = iter.into_iter();
let mut v = Vec::with_capacity(N::USIZE);
v.extend((&mut iter).take(N::USIZE));
if v.len() != N::USIZE {
crate::from_iter_length_fail(v.len(), N::USIZE);
}
if iter.next().is_some() {
crate::from_iter_length_fail(N::USIZE + 1, N::USIZE);
}
GenericArray::try_from_vec(v).unwrap()
}
}
use crate::functional::{FunctionalSequence, MappedGenericSequence};
use crate::GenericSequence;
unsafe impl<T, N: ArrayLength> GenericSequence<T> for Box<GenericArray<T, N>> {
type Length = N;
type Sequence = Box<GenericArray<T, N>>;
fn generate<F>(mut f: F) -> Self::Sequence
where
F: FnMut(usize) -> T,
{
let mut v = Vec::with_capacity(N::USIZE);
for i in 0..N::USIZE {
v.push(f(i));
}
GenericArray::try_from_vec(v).unwrap()
}
}
unsafe impl<T, U, N: ArrayLength> MappedGenericSequence<T, U> for Box<GenericArray<T, N>> {
type Mapped = Box<GenericArray<U, N>>;
}
unsafe impl<T, N: ArrayLength> FunctionalSequence<T> for Box<GenericArray<T, N>> where
Self: GenericSequence<T, Item = T, Length = N>
{
}