1 use super::{ArrayLength, GenericArray}; 2 use core::borrow::{Borrow, BorrowMut}; 3 use core::cmp::Ordering; 4 use core::fmt::{self, Debug}; 5 use core::hash::{Hash, Hasher}; 6 use functional::*; 7 use sequence::*; 8 9 impl<T: Default, N> Default for GenericArray<T, N> 10 where 11 N: ArrayLength<T>, 12 { 13 #[inline] default() -> Self14 fn default() -> Self { 15 Self::generate(|_| T::default()) 16 } 17 } 18 19 impl<T: Clone, N> Clone for GenericArray<T, N> 20 where 21 N: ArrayLength<T>, 22 { clone(&self) -> GenericArray<T, N>23 fn clone(&self) -> GenericArray<T, N> { 24 self.map(Clone::clone) 25 } 26 } 27 28 impl<T: Copy, N> Copy for GenericArray<T, N> 29 where 30 N: ArrayLength<T>, 31 N::ArrayType: Copy, 32 { 33 } 34 35 impl<T: PartialEq, N> PartialEq for GenericArray<T, N> 36 where 37 N: ArrayLength<T>, 38 { eq(&self, other: &Self) -> bool39 fn eq(&self, other: &Self) -> bool { 40 **self == **other 41 } 42 } 43 impl<T: Eq, N> Eq for GenericArray<T, N> 44 where 45 N: ArrayLength<T>, 46 { 47 } 48 49 impl<T: PartialOrd, N> PartialOrd for GenericArray<T, N> 50 where 51 N: ArrayLength<T>, 52 { partial_cmp(&self, other: &GenericArray<T, N>) -> Option<Ordering>53 fn partial_cmp(&self, other: &GenericArray<T, N>) -> Option<Ordering> { 54 PartialOrd::partial_cmp(self.as_slice(), other.as_slice()) 55 } 56 } 57 58 impl<T: Ord, N> Ord for GenericArray<T, N> 59 where 60 N: ArrayLength<T>, 61 { cmp(&self, other: &GenericArray<T, N>) -> Ordering62 fn cmp(&self, other: &GenericArray<T, N>) -> Ordering { 63 Ord::cmp(self.as_slice(), other.as_slice()) 64 } 65 } 66 67 impl<T: Debug, N> Debug for GenericArray<T, N> 68 where 69 N: ArrayLength<T>, 70 { fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result71 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { 72 self[..].fmt(fmt) 73 } 74 } 75 76 impl<T, N> Borrow<[T]> for GenericArray<T, N> 77 where 78 N: ArrayLength<T>, 79 { 80 #[inline(always)] borrow(&self) -> &[T]81 fn borrow(&self) -> &[T] { 82 &self[..] 83 } 84 } 85 86 impl<T, N> BorrowMut<[T]> for GenericArray<T, N> 87 where 88 N: ArrayLength<T>, 89 { 90 #[inline(always)] borrow_mut(&mut self) -> &mut [T]91 fn borrow_mut(&mut self) -> &mut [T] { 92 &mut self[..] 93 } 94 } 95 96 impl<T, N> AsRef<[T]> for GenericArray<T, N> 97 where 98 N: ArrayLength<T>, 99 { 100 #[inline(always)] as_ref(&self) -> &[T]101 fn as_ref(&self) -> &[T] { 102 &self[..] 103 } 104 } 105 106 impl<T, N> AsMut<[T]> for GenericArray<T, N> 107 where 108 N: ArrayLength<T>, 109 { 110 #[inline(always)] as_mut(&mut self) -> &mut [T]111 fn as_mut(&mut self) -> &mut [T] { 112 &mut self[..] 113 } 114 } 115 116 impl<T: Hash, N> Hash for GenericArray<T, N> 117 where 118 N: ArrayLength<T>, 119 { hash<H>(&self, state: &mut H) where H: Hasher,120 fn hash<H>(&self, state: &mut H) 121 where 122 H: Hasher, 123 { 124 Hash::hash(&self[..], state) 125 } 126 } 127 128 macro_rules! impl_from { 129 ($($n: expr => $ty: ty),*) => { 130 $( 131 impl<T> From<[T; $n]> for GenericArray<T, $ty> { 132 #[inline(always)] 133 fn from(arr: [T; $n]) -> Self { 134 unsafe { $crate::transmute(arr) } 135 } 136 } 137 138 impl<T> Into<[T; $n]> for GenericArray<T, $ty> { 139 #[inline(always)] 140 fn into(self) -> [T; $n] { 141 unsafe { $crate::transmute(self) } 142 } 143 } 144 )* 145 146 } 147 } 148 149 impl_from! { 150 1 => ::typenum::U1, 151 2 => ::typenum::U2, 152 3 => ::typenum::U3, 153 4 => ::typenum::U4, 154 5 => ::typenum::U5, 155 6 => ::typenum::U6, 156 7 => ::typenum::U7, 157 8 => ::typenum::U8, 158 9 => ::typenum::U9, 159 10 => ::typenum::U10, 160 11 => ::typenum::U11, 161 12 => ::typenum::U12, 162 13 => ::typenum::U13, 163 14 => ::typenum::U14, 164 15 => ::typenum::U15, 165 16 => ::typenum::U16, 166 17 => ::typenum::U17, 167 18 => ::typenum::U18, 168 19 => ::typenum::U19, 169 20 => ::typenum::U20, 170 21 => ::typenum::U21, 171 22 => ::typenum::U22, 172 23 => ::typenum::U23, 173 24 => ::typenum::U24, 174 25 => ::typenum::U25, 175 26 => ::typenum::U26, 176 27 => ::typenum::U27, 177 28 => ::typenum::U28, 178 29 => ::typenum::U29, 179 30 => ::typenum::U30, 180 31 => ::typenum::U31, 181 32 => ::typenum::U32 182 } 183