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 #![unstable(feature = "core")] // not yet reviewed
17 #![doc(primitive = "array")]
20 use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
21 use convert::{AsRef, AsMut};
23 use hash::{Hash, self};
24 use iter::IntoIterator;
25 use marker::{Copy, Sized};
27 use slice::{Iter, IterMut, SliceExt};
29 /// Utility trait implemented only on arrays of fixed size
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];
41 // macro for implementing n-ary tuple functions and operations
42 macro_rules! array_impls {
45 #[unstable(feature = "core")]
46 impl<T> FixedSizeArray<T> for [T; $N] {
48 fn as_slice(&self) -> &[T] {
52 fn as_mut_slice(&mut self) -> &mut [T] {
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] {
61 fn as_ref(&self) -> &[T] {
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] {
70 fn as_mut(&mut self) -> &mut [T] {
75 #[stable(feature = "rust1", since = "1.0.0")]
76 impl<T:Copy> Clone for [T; $N] {
77 fn clone(&self) -> [T; $N] {
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)
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)
96 #[stable(feature = "rust1", since = "1.0.0")]
97 impl<'a, T> IntoIterator for &'a [T; $N] {
99 type IntoIter = Iter<'a, T>;
101 fn into_iter(self) -> Iter<'a, T> {
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>;
111 fn into_iter(self) -> IterMut<'a, T> {
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] }
124 #[stable(feature = "rust1", since = "1.0.0")]
125 impl<T:Eq> Eq for [T; $N] { }
127 #[stable(feature = "rust1", since = "1.0.0")]
128 impl<T:PartialOrd> PartialOrd for [T; $N] {
130 fn partial_cmp(&self, other: &[T; $N]) -> Option<Ordering> {
131 PartialOrd::partial_cmp(&&self[..], &&other[..])
134 fn lt(&self, other: &[T; $N]) -> bool {
135 PartialOrd::lt(&&self[..], &&other[..])
138 fn le(&self, other: &[T; $N]) -> bool {
139 PartialOrd::le(&&self[..], &&other[..])
142 fn ge(&self, other: &[T; $N]) -> bool {
143 PartialOrd::ge(&&self[..], &&other[..])
146 fn gt(&self, other: &[T; $N]) -> bool {
147 PartialOrd::gt(&&self[..], &&other[..])
151 #[stable(feature = "rust1", since = "1.0.0")]
152 impl<T:Ord> Ord for [T; $N] {
154 fn cmp(&self, other: &[T; $N]) -> Ordering {
155 Ord::cmp(&&self[..], &&other[..])
164 10 11 12 13 14 15 16 17 18 19
165 20 21 22 23 24 25 26 27 28 29