]> git.lizzy.rs Git - rust.git/blob - src/libcore/array.rs
91301ee558ca566315559f663a6948d11519f2ba
[rust.git] / src / libcore / array.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Implementations of things like `Eq` for fixed-length arrays
12 //! up to a certain length. Eventually we should able to generalize
13 //! to all lengths.
14
15 #![unstable(feature = "core")] // not yet reviewed
16
17 #![doc(primitive = "array")]
18
19 use clone::Clone;
20 use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
21 use convert::{AsRef, AsMut};
22 use fmt;
23 use hash::{Hash, self};
24 use iter::IntoIterator;
25 use marker::{Copy, Sized};
26 use option::Option;
27 use slice::{Iter, IterMut, SliceExt};
28
29 /// Utility trait implemented only on arrays of fixed size
30 ///
31 /// This trait can be used to implement other traits on fixed-size arrays
32 /// without causing much metadata bloat.
33 #[unstable(feature = "core")]
34 pub trait FixedSizeArray<T> {
35     /// Converts the array to immutable slice
36     fn as_slice(&self) -> &[T];
37     /// Converts the array to mutable slice
38     fn as_mut_slice(&mut self) -> &mut [T];
39 }
40
41 // macro for implementing n-ary tuple functions and operations
42 macro_rules! array_impls {
43     ($($N:expr)+) => {
44         $(
45             #[unstable(feature = "core")]
46             impl<T> FixedSizeArray<T> for [T; $N] {
47                 #[inline]
48                 fn as_slice(&self) -> &[T] {
49                     &self[..]
50                 }
51                 #[inline]
52                 fn as_mut_slice(&mut self) -> &mut [T] {
53                     &mut self[..]
54                 }
55             }
56
57             #[unstable(feature = "array_as_ref",
58                        reason = "should ideally be implemented for all fixed-sized arrays")]
59             impl<T> AsRef<[T]> for [T; $N] {
60                 #[inline]
61                 fn as_ref(&self) -> &[T] {
62                     &self[..]
63                 }
64             }
65
66             #[unstable(feature = "array_as_ref",
67                        reason = "should ideally be implemented for all fixed-sized arrays")]
68             impl<T> AsMut<[T]> for [T; $N] {
69                 #[inline]
70                 fn as_mut(&mut self) -> &mut [T] {
71                     &mut self[..]
72                 }
73             }
74
75             #[stable(feature = "rust1", since = "1.0.0")]
76             impl<T:Copy> Clone for [T; $N] {
77                 fn clone(&self) -> [T; $N] {
78                     *self
79                 }
80             }
81
82             #[stable(feature = "rust1", since = "1.0.0")]
83             impl<T: Hash> Hash for [T; $N] {
84                 fn hash<H: hash::Hasher>(&self, state: &mut H) {
85                     Hash::hash(&self[..], state)
86                 }
87             }
88
89             #[stable(feature = "rust1", since = "1.0.0")]
90             impl<T: fmt::Debug> fmt::Debug for [T; $N] {
91                 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
92                     fmt::Debug::fmt(&&self[..], f)
93                 }
94             }
95
96             #[stable(feature = "rust1", since = "1.0.0")]
97             impl<'a, T> IntoIterator for &'a [T; $N] {
98                 type Item = &'a T;
99                 type IntoIter = Iter<'a, T>;
100
101                 fn into_iter(self) -> Iter<'a, T> {
102                     self.iter()
103                 }
104             }
105
106             #[stable(feature = "rust1", since = "1.0.0")]
107             impl<'a, T> IntoIterator for &'a mut [T; $N] {
108                 type Item = &'a mut T;
109                 type IntoIter = IterMut<'a, T>;
110
111                 fn into_iter(self) -> IterMut<'a, T> {
112                     self.iter_mut()
113                 }
114             }
115
116             // NOTE: some less important impls are omitted to reduce code bloat
117             __impl_slice_eq1! { [A; $N], [B; $N] }
118             __impl_slice_eq2! { [A; $N], [B] }
119             __impl_slice_eq2! { [A; $N], &'b [B] }
120             __impl_slice_eq2! { [A; $N], &'b mut [B] }
121             // __impl_slice_eq2! { [A; $N], &'b [B; $N] }
122             // __impl_slice_eq2! { [A; $N], &'b mut [B; $N] }
123
124             #[stable(feature = "rust1", since = "1.0.0")]
125             impl<T:Eq> Eq for [T; $N] { }
126
127             #[stable(feature = "rust1", since = "1.0.0")]
128             impl<T:PartialOrd> PartialOrd for [T; $N] {
129                 #[inline]
130                 fn partial_cmp(&self, other: &[T; $N]) -> Option<Ordering> {
131                     PartialOrd::partial_cmp(&&self[..], &&other[..])
132                 }
133                 #[inline]
134                 fn lt(&self, other: &[T; $N]) -> bool {
135                     PartialOrd::lt(&&self[..], &&other[..])
136                 }
137                 #[inline]
138                 fn le(&self, other: &[T; $N]) -> bool {
139                     PartialOrd::le(&&self[..], &&other[..])
140                 }
141                 #[inline]
142                 fn ge(&self, other: &[T; $N]) -> bool {
143                     PartialOrd::ge(&&self[..], &&other[..])
144                 }
145                 #[inline]
146                 fn gt(&self, other: &[T; $N]) -> bool {
147                     PartialOrd::gt(&&self[..], &&other[..])
148                 }
149             }
150
151             #[stable(feature = "rust1", since = "1.0.0")]
152             impl<T:Ord> Ord for [T; $N] {
153                 #[inline]
154                 fn cmp(&self, other: &[T; $N]) -> Ordering {
155                     Ord::cmp(&&self[..], &&other[..])
156                 }
157             }
158         )+
159     }
160 }
161
162 array_impls! {
163      0  1  2  3  4  5  6  7  8  9
164     10 11 12 13 14 15 16 17 18 19
165     20 21 22 23 24 25 26 27 28 29
166     30 31 32
167 }