1 //! Implementations of things like `Eq` for fixed-length arrays
2 //! up to a certain length. Eventually we should able to generalize
5 //! *[See also the array primitive type](../../std/primitive.array.html).*
7 #![unstable(feature = "fixed_size_array",
8 reason = "traits and impls are better expressed through generic \
12 use borrow::{Borrow, BorrowMut};
16 use hash::{Hash, self};
18 use slice::{Iter, IterMut};
20 /// Utility trait implemented only on arrays of fixed size
22 /// This trait can be used to implement other traits on fixed-size arrays
23 /// without causing much metadata bloat.
25 /// The trait is marked unsafe in order to restrict implementors to fixed-size
26 /// arrays. User of this trait can assume that implementors have the exact
27 /// layout in memory of a fixed size array (for example, for unsafe
30 /// Note that the traits AsRef and AsMut provide similar methods for types that
31 /// may not be fixed-size arrays. Implementors should prefer those traits
33 pub unsafe trait FixedSizeArray<T> {
34 /// Converts the array to immutable slice
35 fn as_slice(&self) -> &[T];
36 /// Converts the array to mutable slice
37 fn as_mut_slice(&mut self) -> &mut [T];
40 unsafe impl<T, A: Unsize<[T]>> FixedSizeArray<T> for A {
42 fn as_slice(&self) -> &[T] {
46 fn as_mut_slice(&mut self) -> &mut [T] {
51 /// The error type returned when a conversion from a slice to an array fails.
52 #[stable(feature = "try_from", since = "1.34.0")]
53 #[derive(Debug, Copy, Clone)]
54 pub struct TryFromSliceError(());
56 impl fmt::Display for TryFromSliceError {
58 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59 fmt::Display::fmt(self.__description(), f)
63 impl TryFromSliceError {
64 #[unstable(feature = "array_error_internals",
65 reason = "available through Error trait and this method should not \
70 pub fn __description(&self) -> &str {
71 "could not convert slice to array"
75 macro_rules! __impl_slice_eq1 {
76 ($Lhs: ty, $Rhs: ty) => {
77 __impl_slice_eq1! { $Lhs, $Rhs, Sized }
79 ($Lhs: ty, $Rhs: ty, $Bound: ident) => {
80 #[stable(feature = "rust1", since = "1.0.0")]
81 impl<'a, 'b, A: $Bound, B> PartialEq<$Rhs> for $Lhs where A: PartialEq<B> {
83 fn eq(&self, other: &$Rhs) -> bool { self[..] == other[..] }
85 fn ne(&self, other: &$Rhs) -> bool { self[..] != other[..] }
90 macro_rules! __impl_slice_eq2 {
91 ($Lhs: ty, $Rhs: ty) => {
92 __impl_slice_eq2! { $Lhs, $Rhs, Sized }
94 ($Lhs: ty, $Rhs: ty, $Bound: ident) => {
95 __impl_slice_eq1!($Lhs, $Rhs, $Bound);
97 #[stable(feature = "rust1", since = "1.0.0")]
98 impl<'a, 'b, A: $Bound, B> PartialEq<$Lhs> for $Rhs where B: PartialEq<A> {
100 fn eq(&self, other: &$Lhs) -> bool { self[..] == other[..] }
102 fn ne(&self, other: &$Lhs) -> bool { self[..] != other[..] }
107 // macro for implementing n-element array functions and operations
108 macro_rules! array_impls {
111 #[stable(feature = "rust1", since = "1.0.0")]
112 impl<T> AsRef<[T]> for [T; $N] {
114 fn as_ref(&self) -> &[T] {
119 #[stable(feature = "rust1", since = "1.0.0")]
120 impl<T> AsMut<[T]> for [T; $N] {
122 fn as_mut(&mut self) -> &mut [T] {
127 #[stable(feature = "array_borrow", since = "1.4.0")]
128 impl<T> Borrow<[T]> for [T; $N] {
129 fn borrow(&self) -> &[T] {
134 #[stable(feature = "array_borrow", since = "1.4.0")]
135 impl<T> BorrowMut<[T]> for [T; $N] {
136 fn borrow_mut(&mut self) -> &mut [T] {
141 #[stable(feature = "try_from", since = "1.34.0")]
142 impl<T> TryFrom<&[T]> for [T; $N] where T: Copy {
143 type Error = TryFromSliceError;
145 fn try_from(slice: &[T]) -> Result<[T; $N], TryFromSliceError> {
146 <&Self>::try_from(slice).map(|r| *r)
150 #[stable(feature = "try_from", since = "1.34.0")]
151 impl<'a, T> TryFrom<&'a [T]> for &'a [T; $N] {
152 type Error = TryFromSliceError;
154 fn try_from(slice: &[T]) -> Result<&[T; $N], TryFromSliceError> {
155 if slice.len() == $N {
156 let ptr = slice.as_ptr() as *const [T; $N];
159 Err(TryFromSliceError(()))
164 #[stable(feature = "try_from", since = "1.34.0")]
165 impl<'a, T> TryFrom<&'a mut [T]> for &'a mut [T; $N] {
166 type Error = TryFromSliceError;
168 fn try_from(slice: &mut [T]) -> Result<&mut [T; $N], TryFromSliceError> {
169 if slice.len() == $N {
170 let ptr = slice.as_mut_ptr() as *mut [T; $N];
171 unsafe { Ok(&mut *ptr) }
173 Err(TryFromSliceError(()))
178 #[stable(feature = "rust1", since = "1.0.0")]
179 impl<T: Hash> Hash for [T; $N] {
180 fn hash<H: hash::Hasher>(&self, state: &mut H) {
181 Hash::hash(&self[..], state)
185 #[stable(feature = "rust1", since = "1.0.0")]
186 impl<T: fmt::Debug> fmt::Debug for [T; $N] {
187 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
188 fmt::Debug::fmt(&&self[..], f)
192 #[stable(feature = "rust1", since = "1.0.0")]
193 impl<'a, T> IntoIterator for &'a [T; $N] {
195 type IntoIter = Iter<'a, T>;
197 fn into_iter(self) -> Iter<'a, T> {
202 #[stable(feature = "rust1", since = "1.0.0")]
203 impl<'a, T> IntoIterator for &'a mut [T; $N] {
204 type Item = &'a mut T;
205 type IntoIter = IterMut<'a, T>;
207 fn into_iter(self) -> IterMut<'a, T> {
212 // NOTE: some less important impls are omitted to reduce code bloat
213 __impl_slice_eq1! { [A; $N], [B; $N] }
214 __impl_slice_eq2! { [A; $N], [B] }
215 __impl_slice_eq2! { [A; $N], &'b [B] }
216 __impl_slice_eq2! { [A; $N], &'b mut [B] }
217 // __impl_slice_eq2! { [A; $N], &'b [B; $N] }
218 // __impl_slice_eq2! { [A; $N], &'b mut [B; $N] }
220 #[stable(feature = "rust1", since = "1.0.0")]
221 impl<T:Eq> Eq for [T; $N] { }
223 #[stable(feature = "rust1", since = "1.0.0")]
224 impl<T:PartialOrd> PartialOrd for [T; $N] {
226 fn partial_cmp(&self, other: &[T; $N]) -> Option<Ordering> {
227 PartialOrd::partial_cmp(&&self[..], &&other[..])
230 fn lt(&self, other: &[T; $N]) -> bool {
231 PartialOrd::lt(&&self[..], &&other[..])
234 fn le(&self, other: &[T; $N]) -> bool {
235 PartialOrd::le(&&self[..], &&other[..])
238 fn ge(&self, other: &[T; $N]) -> bool {
239 PartialOrd::ge(&&self[..], &&other[..])
242 fn gt(&self, other: &[T; $N]) -> bool {
243 PartialOrd::gt(&&self[..], &&other[..])
247 #[stable(feature = "rust1", since = "1.0.0")]
248 impl<T:Ord> Ord for [T; $N] {
250 fn cmp(&self, other: &[T; $N]) -> Ordering {
251 Ord::cmp(&&self[..], &&other[..])
260 10 11 12 13 14 15 16 17 18 19
261 20 21 22 23 24 25 26 27 28 29
265 // The Default impls cannot be generated using the array_impls! macro because
266 // they require array literals.
268 macro_rules! array_impl_default {
269 {$n:expr, $t:ident $($ts:ident)*} => {
270 #[stable(since = "1.4.0", feature = "array_default")]
271 impl<T> Default for [T; $n] where T: Default {
272 fn default() -> [T; $n] {
273 [$t::default(), $($ts::default()),*]
276 array_impl_default!{($n - 1), $($ts)*}
279 #[stable(since = "1.4.0", feature = "array_default")]
280 impl<T> Default for [T; $n] {
281 fn default() -> [T; $n] { [] }
286 array_impl_default!{32, T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T}