| 1 |
use core::borrow::{Borrow, BorrowMut}; |
| 2 |
use core::cmp::Ordering; |
| 3 |
use core::fmt::{self, Debug}; |
| 4 |
use core::hash::{Hash, Hasher}; |
| 5 |
|
| 6 |
use super::{ArrayLength, GenericArray}; |
| 7 |
|
| 8 |
use crate::functional::*; |
| 9 |
use crate::sequence::*; |
| 10 |
|
| 11 |
impl<T: Default, N> Default for GenericArray<T, N> |
| 12 |
where |
| 13 |
N: ArrayLength<T>, |
| 14 |
{ |
| 15 |
#[inline(always)] |
| 16 |
fn default() -> Self { |
| 17 |
Self::generate(|_| T::default()) |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
impl<T: Clone, N> Clone for GenericArray<T, N> |
| 22 |
where |
| 23 |
N: ArrayLength<T>, |
| 24 |
{ |
| 25 |
fn clone(&self) -> GenericArray<T, N> { |
| 26 |
self.map(Clone::clone) |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
impl<T: Copy, N> Copy for GenericArray<T, N> |
| 31 |
where |
| 32 |
N: ArrayLength<T>, |
| 33 |
N::ArrayType: Copy, |
| 34 |
{ |
| 35 |
} |
| 36 |
|
| 37 |
impl<T: PartialEq, N> PartialEq for GenericArray<T, N> |
| 38 |
where |
| 39 |
N: ArrayLength<T>, |
| 40 |
{ |
| 41 |
fn eq(&self, other: &Self) -> bool { |
| 42 |
**self == **other |
| 43 |
} |
| 44 |
} |
| 45 |
impl<T: Eq, N> Eq for GenericArray<T, N> where N: ArrayLength<T> {} |
| 46 |
|
| 47 |
impl<T: PartialOrd, N> PartialOrd for GenericArray<T, N> |
| 48 |
where |
| 49 |
N: ArrayLength<T>, |
| 50 |
{ |
| 51 |
fn partial_cmp(&self, other: &GenericArray<T, N>) -> Option<Ordering> { |
| 52 |
PartialOrd::partial_cmp(self.as_slice(), other.as_slice()) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
impl<T: Ord, N> Ord for GenericArray<T, N> |
| 57 |
where |
| 58 |
N: ArrayLength<T>, |
| 59 |
{ |
| 60 |
fn cmp(&self, other: &GenericArray<T, N>) -> Ordering { |
| 61 |
Ord::cmp(self.as_slice(), other.as_slice()) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
impl<T: Debug, N> Debug for GenericArray<T, N> |
| 66 |
where |
| 67 |
N: ArrayLength<T>, |
| 68 |
{ |
| 69 |
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
| 70 |
self[..].fmt(fmt) |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
impl<T, N> Borrow<[T]> for GenericArray<T, N> |
| 75 |
where |
| 76 |
N: ArrayLength<T>, |
| 77 |
{ |
| 78 |
#[inline(always)] |
| 79 |
fn borrow(&self) -> &[T] { |
| 80 |
&self[..] |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
impl<T, N> BorrowMut<[T]> for GenericArray<T, N> |
| 85 |
where |
| 86 |
N: ArrayLength<T>, |
| 87 |
{ |
| 88 |
#[inline(always)] |
| 89 |
fn borrow_mut(&mut self) -> &mut [T] { |
| 90 |
&mut self[..] |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
impl<T, N> AsRef<[T]> for GenericArray<T, N> |
| 95 |
where |
| 96 |
N: ArrayLength<T>, |
| 97 |
{ |
| 98 |
#[inline(always)] |
| 99 |
fn as_ref(&self) -> &[T] { |
| 100 |
&self[..] |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
impl<T, N> AsMut<[T]> for GenericArray<T, N> |
| 105 |
where |
| 106 |
N: ArrayLength<T>, |
| 107 |
{ |
| 108 |
#[inline(always)] |
| 109 |
fn as_mut(&mut self) -> &mut [T] { |
| 110 |
&mut self[..] |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
impl<T: Hash, N> Hash for GenericArray<T, N> |
| 115 |
where |
| 116 |
N: ArrayLength<T>, |
| 117 |
{ |
| 118 |
fn hash<H>(&self, state: &mut H) |
| 119 |
where |
| 120 |
H: Hasher, |
| 121 |
{ |
| 122 |
Hash::hash(&self[..], state) |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
macro_rules! impl_from { |
| 127 |
($($n: expr => $ty: ty),*) => { |
| 128 |
$( |
| 129 |
impl<T> From<[T; $n]> for GenericArray<T, $ty> { |
| 130 |
#[inline(always)] |
| 131 |
fn from(arr: [T; $n]) -> Self { |
| 132 |
unsafe { $crate::transmute(arr) } |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
#[cfg(relaxed_coherence)] |
| 137 |
impl<T> From<GenericArray<T, $ty>> for [T; $n] { |
| 138 |
#[inline(always)] |
| 139 |
fn from(sel: GenericArray<T, $ty>) -> [T; $n] { |
| 140 |
unsafe { $crate::transmute(sel) } |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
impl<'a, T> From<&'a [T; $n]> for &'a GenericArray<T, $ty> { |
| 145 |
#[inline] |
| 146 |
fn from(slice: &[T; $n]) -> &GenericArray<T, $ty> { |
| 147 |
unsafe { &*(slice.as_ptr() as *const GenericArray<T, $ty>) } |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
impl<'a, T> From<&'a mut [T; $n]> for &'a mut GenericArray<T, $ty> { |
| 152 |
#[inline] |
| 153 |
fn from(slice: &mut [T; $n]) -> &mut GenericArray<T, $ty> { |
| 154 |
unsafe { &mut *(slice.as_mut_ptr() as *mut GenericArray<T, $ty>) } |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
#[cfg(not(relaxed_coherence))] |
| 159 |
impl<T> Into<[T; $n]> for GenericArray<T, $ty> { |
| 160 |
#[inline(always)] |
| 161 |
fn into(self) -> [T; $n] { |
| 162 |
unsafe { $crate::transmute(self) } |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
impl<T> AsRef<[T; $n]> for GenericArray<T, $ty> { |
| 167 |
#[inline] |
| 168 |
fn as_ref(&self) -> &[T; $n] { |
| 169 |
unsafe { $crate::transmute(self) } |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
impl<T> AsMut<[T; $n]> for GenericArray<T, $ty> { |
| 174 |
#[inline] |
| 175 |
fn as_mut(&mut self) -> &mut [T; $n] { |
| 176 |
unsafe { $crate::transmute(self) } |
| 177 |
} |
| 178 |
} |
| 179 |
)* |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
impl_from! { |
| 184 |
1 => ::typenum::U1, |
| 185 |
2 => ::typenum::U2, |
| 186 |
3 => ::typenum::U3, |
| 187 |
4 => ::typenum::U4, |
| 188 |
5 => ::typenum::U5, |
| 189 |
6 => ::typenum::U6, |
| 190 |
7 => ::typenum::U7, |
| 191 |
8 => ::typenum::U8, |
| 192 |
9 => ::typenum::U9, |
| 193 |
10 => ::typenum::U10, |
| 194 |
11 => ::typenum::U11, |
| 195 |
12 => ::typenum::U12, |
| 196 |
13 => ::typenum::U13, |
| 197 |
14 => ::typenum::U14, |
| 198 |
15 => ::typenum::U15, |
| 199 |
16 => ::typenum::U16, |
| 200 |
17 => ::typenum::U17, |
| 201 |
18 => ::typenum::U18, |
| 202 |
19 => ::typenum::U19, |
| 203 |
20 => ::typenum::U20, |
| 204 |
21 => ::typenum::U21, |
| 205 |
22 => ::typenum::U22, |
| 206 |
23 => ::typenum::U23, |
| 207 |
24 => ::typenum::U24, |
| 208 |
25 => ::typenum::U25, |
| 209 |
26 => ::typenum::U26, |
| 210 |
27 => ::typenum::U27, |
| 211 |
28 => ::typenum::U28, |
| 212 |
29 => ::typenum::U29, |
| 213 |
30 => ::typenum::U30, |
| 214 |
31 => ::typenum::U31, |
| 215 |
32 => ::typenum::U32 |
| 216 |
} |
| 217 |
|
| 218 |
#[cfg(feature = "more_lengths")] |
| 219 |
impl_from! { |
| 220 |
33 => ::typenum::U33, |
| 221 |
34 => ::typenum::U34, |
| 222 |
35 => ::typenum::U35, |
| 223 |
36 => ::typenum::U36, |
| 224 |
37 => ::typenum::U37, |
| 225 |
38 => ::typenum::U38, |
| 226 |
39 => ::typenum::U39, |
| 227 |
40 => ::typenum::U40, |
| 228 |
41 => ::typenum::U41, |
| 229 |
42 => ::typenum::U42, |
| 230 |
43 => ::typenum::U43, |
| 231 |
44 => ::typenum::U44, |
| 232 |
45 => ::typenum::U45, |
| 233 |
46 => ::typenum::U46, |
| 234 |
47 => ::typenum::U47, |
| 235 |
48 => ::typenum::U48, |
| 236 |
49 => ::typenum::U49, |
| 237 |
50 => ::typenum::U50, |
| 238 |
51 => ::typenum::U51, |
| 239 |
52 => ::typenum::U52, |
| 240 |
53 => ::typenum::U53, |
| 241 |
54 => ::typenum::U54, |
| 242 |
55 => ::typenum::U55, |
| 243 |
56 => ::typenum::U56, |
| 244 |
57 => ::typenum::U57, |
| 245 |
58 => ::typenum::U58, |
| 246 |
59 => ::typenum::U59, |
| 247 |
60 => ::typenum::U60, |
| 248 |
61 => ::typenum::U61, |
| 249 |
62 => ::typenum::U62, |
| 250 |
63 => ::typenum::U63, |
| 251 |
64 => ::typenum::U64, |
| 252 |
|
| 253 |
70 => ::typenum::U70, |
| 254 |
80 => ::typenum::U80, |
| 255 |
90 => ::typenum::U90, |
| 256 |
|
| 257 |
100 => ::typenum::U100, |
| 258 |
200 => ::typenum::U200, |
| 259 |
300 => ::typenum::U300, |
| 260 |
400 => ::typenum::U400, |
| 261 |
500 => ::typenum::U500, |
| 262 |
|
| 263 |
128 => ::typenum::U128, |
| 264 |
256 => ::typenum::U256, |
| 265 |
512 => ::typenum::U512, |
| 266 |
|
| 267 |
1000 => ::typenum::U1000, |
| 268 |
1024 => ::typenum::U1024 |
| 269 |
} |