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