]> git.proxmox.com Git - rustc.git/blob - src/libcore/array.rs
Imported Upstream version 1.2.0+dfsg1
[rustc.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 #![doc(primitive = "array")]
16 #![unstable(feature = "fixed_size_array",
17 reason = "traits and impls are better expressed through generic \
18 integer constants")]
19
20 use clone::Clone;
21 use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
22 use convert::{AsRef, AsMut};
23 use fmt;
24 use hash::{Hash, self};
25 use iter::IntoIterator;
26 use marker::{Copy, Sized};
27 use option::Option;
28 use slice::{Iter, IterMut, SliceExt};
29
30 /// Utility trait implemented only on arrays of fixed size
31 ///
32 /// This trait can be used to implement other traits on fixed-size arrays
33 /// without causing much metadata bloat.
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 impl<T> FixedSizeArray<T> for [T; $N] {
46 #[inline]
47 fn as_slice(&self) -> &[T] {
48 &self[..]
49 }
50 #[inline]
51 fn as_mut_slice(&mut self) -> &mut [T] {
52 &mut self[..]
53 }
54 }
55
56 impl<T> AsRef<[T]> for [T; $N] {
57 #[inline]
58 fn as_ref(&self) -> &[T] {
59 &self[..]
60 }
61 }
62
63 impl<T> AsMut<[T]> for [T; $N] {
64 #[inline]
65 fn as_mut(&mut self) -> &mut [T] {
66 &mut self[..]
67 }
68 }
69
70 #[stable(feature = "rust1", since = "1.0.0")]
71 impl<T:Copy> Clone for [T; $N] {
72 fn clone(&self) -> [T; $N] {
73 *self
74 }
75 }
76
77 #[stable(feature = "rust1", since = "1.0.0")]
78 impl<T: Hash> Hash for [T; $N] {
79 fn hash<H: hash::Hasher>(&self, state: &mut H) {
80 Hash::hash(&self[..], state)
81 }
82 }
83
84 #[stable(feature = "rust1", since = "1.0.0")]
85 impl<T: fmt::Debug> fmt::Debug for [T; $N] {
86 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
87 fmt::Debug::fmt(&&self[..], f)
88 }
89 }
90
91 #[stable(feature = "rust1", since = "1.0.0")]
92 impl<'a, T> IntoIterator for &'a [T; $N] {
93 type Item = &'a T;
94 type IntoIter = Iter<'a, T>;
95
96 fn into_iter(self) -> Iter<'a, T> {
97 self.iter()
98 }
99 }
100
101 #[stable(feature = "rust1", since = "1.0.0")]
102 impl<'a, T> IntoIterator for &'a mut [T; $N] {
103 type Item = &'a mut T;
104 type IntoIter = IterMut<'a, T>;
105
106 fn into_iter(self) -> IterMut<'a, T> {
107 self.iter_mut()
108 }
109 }
110
111 // NOTE: some less important impls are omitted to reduce code bloat
112 __impl_slice_eq1! { [A; $N], [B; $N] }
113 __impl_slice_eq2! { [A; $N], [B] }
114 __impl_slice_eq2! { [A; $N], &'b [B] }
115 __impl_slice_eq2! { [A; $N], &'b mut [B] }
116 // __impl_slice_eq2! { [A; $N], &'b [B; $N] }
117 // __impl_slice_eq2! { [A; $N], &'b mut [B; $N] }
118
119 #[stable(feature = "rust1", since = "1.0.0")]
120 impl<T:Eq> Eq for [T; $N] { }
121
122 #[stable(feature = "rust1", since = "1.0.0")]
123 impl<T:PartialOrd> PartialOrd for [T; $N] {
124 #[inline]
125 fn partial_cmp(&self, other: &[T; $N]) -> Option<Ordering> {
126 PartialOrd::partial_cmp(&&self[..], &&other[..])
127 }
128 #[inline]
129 fn lt(&self, other: &[T; $N]) -> bool {
130 PartialOrd::lt(&&self[..], &&other[..])
131 }
132 #[inline]
133 fn le(&self, other: &[T; $N]) -> bool {
134 PartialOrd::le(&&self[..], &&other[..])
135 }
136 #[inline]
137 fn ge(&self, other: &[T; $N]) -> bool {
138 PartialOrd::ge(&&self[..], &&other[..])
139 }
140 #[inline]
141 fn gt(&self, other: &[T; $N]) -> bool {
142 PartialOrd::gt(&&self[..], &&other[..])
143 }
144 }
145
146 #[stable(feature = "rust1", since = "1.0.0")]
147 impl<T:Ord> Ord for [T; $N] {
148 #[inline]
149 fn cmp(&self, other: &[T; $N]) -> Ordering {
150 Ord::cmp(&&self[..], &&other[..])
151 }
152 }
153 )+
154 }
155 }
156
157 array_impls! {
158 0 1 2 3 4 5 6 7 8 9
159 10 11 12 13 14 15 16 17 18 19
160 20 21 22 23 24 25 26 27 28 29
161 30 31 32
162 }