Skip to content

Commit 316f864

Browse files
authored
hybrid-array: add slice_as_chunks(_mut) methods, minor refactoring (#974)
1 parent b755f1a commit 316f864

File tree

3 files changed

+261
-219
lines changed

3 files changed

+261
-219
lines changed

hybrid-array/src/impls.rs

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
use super::{Array, ArrayOps, ArraySize, IntoArray};
2+
3+
macro_rules! impl_array_size {
4+
($($len:expr => $ty:ident),+) => {
5+
$(
6+
impl<T> ArrayOps<T, $len> for Array<T, typenum::$ty> {
7+
const SIZE: usize = $len;
8+
type Size = typenum::$ty;
9+
10+
#[inline]
11+
fn as_core_array(&self) -> &[T; $len] {
12+
&self.0
13+
}
14+
15+
#[inline]
16+
fn as_mut_core_array(&mut self) -> &mut [T; $len] {
17+
&mut self.0
18+
}
19+
20+
#[inline]
21+
fn from_core_array(arr: [T; $len]) -> Self {
22+
Self(arr)
23+
}
24+
25+
#[inline]
26+
fn ref_from_core_array(array_ref: &[T; $len]) -> &Self {
27+
// SAFETY: `Self` is a `repr(transparent)` newtype for `[T; $len]`
28+
unsafe { &*(array_ref.as_ptr() as *const Self) }
29+
}
30+
31+
#[inline]
32+
fn ref_from_mut_core_array(array_ref: &mut [T; $len]) -> &mut Self {
33+
// SAFETY: `Self` is a `repr(transparent)` newtype for `[T; $len]`
34+
unsafe { &mut *(array_ref.as_mut_ptr() as *mut Self) }
35+
}
36+
37+
#[inline]
38+
fn map_to_core_array<F, U>(self, f: F) -> [U; $len]
39+
where
40+
F: FnMut(T) -> U
41+
{
42+
self.0.map(f)
43+
}
44+
}
45+
46+
unsafe impl ArraySize for typenum::$ty {
47+
type ArrayType<T> = [T; $len];
48+
}
49+
50+
impl<T> From<Array<T, typenum::$ty>> for [T; $len] {
51+
fn from(arr: Array<T, typenum::$ty>) -> [T; $len] {
52+
arr.0
53+
}
54+
}
55+
56+
impl<T> IntoArray<T> for [T; $len] {
57+
type Size = typenum::$ty;
58+
59+
fn into_hybrid_array(self) -> Array<T, Self::Size> {
60+
Array::from_core_array(self)
61+
}
62+
}
63+
)+
64+
};
65+
}
66+
67+
impl_array_size! {
68+
0 => U0,
69+
1 => U1,
70+
2 => U2,
71+
3 => U3,
72+
4 => U4,
73+
5 => U5,
74+
6 => U6,
75+
7 => U7,
76+
8 => U8,
77+
9 => U9,
78+
10 => U10,
79+
11 => U11,
80+
12 => U12,
81+
13 => U13,
82+
14 => U14,
83+
15 => U15,
84+
16 => U16,
85+
17 => U17,
86+
18 => U18,
87+
19 => U19,
88+
20 => U20,
89+
21 => U21,
90+
22 => U22,
91+
23 => U23,
92+
24 => U24,
93+
25 => U25,
94+
26 => U26,
95+
27 => U27,
96+
28 => U28,
97+
29 => U29,
98+
30 => U30,
99+
31 => U31,
100+
32 => U32,
101+
33 => U33,
102+
34 => U34,
103+
35 => U35,
104+
36 => U36,
105+
37 => U37,
106+
38 => U38,
107+
39 => U39,
108+
40 => U40,
109+
41 => U41,
110+
42 => U42,
111+
43 => U43,
112+
44 => U44,
113+
45 => U45,
114+
46 => U46,
115+
47 => U47,
116+
48 => U48,
117+
49 => U49,
118+
50 => U50,
119+
51 => U51,
120+
52 => U52,
121+
53 => U53,
122+
54 => U54,
123+
55 => U55,
124+
56 => U56,
125+
57 => U57,
126+
58 => U58,
127+
59 => U59,
128+
60 => U60,
129+
61 => U61,
130+
62 => U62,
131+
63 => U63,
132+
64 => U64,
133+
96 => U96,
134+
128 => U128,
135+
192 => U192,
136+
256 => U256,
137+
384 => U384,
138+
448 => U448,
139+
512 => U512,
140+
768 => U768,
141+
896 => U896,
142+
1024 => U1024,
143+
2048 => U2048,
144+
4096 => U4096,
145+
8192 => U8192
146+
}

0 commit comments

Comments
 (0)