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.
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.
11 //! Implementations of things like `Eq` for fixed-length arrays
12 //! up to a certain length. Eventually we should able to generalize
15 #![doc(primitive = "array")]
16 #![unstable(feature = "fixed_size_array",
17 reason = "traits and impls are better expressed through generic \
21 use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
22 use convert::{AsRef, AsMut};
24 use hash::{Hash, self};
25 use iter::IntoIterator;
26 use marker::{Copy, Sized};
28 use slice::{Iter, IterMut, SliceExt};
30 /// Utility trait implemented only on arrays of fixed size
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];
41 // macro for implementing n-ary tuple functions and operations
42 macro_rules! array_impls {
45 impl<T> FixedSizeArray<T> for [T; $N] {
47 fn as_slice(&self) -> &[T] {
51 fn as_mut_slice(&mut self) -> &mut [T] {
56 impl<T> AsRef<[T]> for [T; $N] {
58 fn as_ref(&self) -> &[T] {
63 impl<T> AsMut<[T]> for [T; $N] {
65 fn as_mut(&mut self) -> &mut [T] {
70 #[stable(feature = "rust1", since = "1.0.0")]
71 impl<T:Copy> Clone for [T; $N] {
72 fn clone(&self) -> [T; $N] {
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)
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)
91 #[stable(feature = "rust1", since = "1.0.0")]
92 impl<'a, T> IntoIterator for &'a [T; $N] {
94 type IntoIter = Iter<'a, T>;
96 fn into_iter(self) -> Iter<'a, T> {
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>;
106 fn into_iter(self) -> IterMut<'a, T> {
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] }
119 #[stable(feature = "rust1", since = "1.0.0")]
120 impl<T:Eq> Eq for [T; $N] { }
122 #[stable(feature = "rust1", since = "1.0.0")]
123 impl<T:PartialOrd> PartialOrd for [T; $N] {
125 fn partial_cmp(&self, other: &[T; $N]) -> Option<Ordering> {
126 PartialOrd::partial_cmp(&&self[..], &&other[..])
129 fn lt(&self, other: &[T; $N]) -> bool {
130 PartialOrd::lt(&&self[..], &&other[..])
133 fn le(&self, other: &[T; $N]) -> bool {
134 PartialOrd::le(&&self[..], &&other[..])
137 fn ge(&self, other: &[T; $N]) -> bool {
138 PartialOrd::ge(&&self[..], &&other[..])
141 fn gt(&self, other: &[T; $N]) -> bool {
142 PartialOrd::gt(&&self[..], &&other[..])
146 #[stable(feature = "rust1", since = "1.0.0")]
147 impl<T:Ord> Ord for [T; $N] {
149 fn cmp(&self, other: &[T; $N]) -> Ordering {
150 Ord::cmp(&&self[..], &&other[..])
159 10 11 12 13 14 15 16 17 18 19
160 20 21 22 23 24 25 26 27 28 29