2014-10-31 04:41:25 -05:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-11-14 05:37:59 -06:00
|
|
|
//! Implementations of things like `Eq` for fixed-length arrays
|
|
|
|
//! up to a certain length. Eventually we should able to generalize
|
|
|
|
//! to all lengths.
|
2014-10-31 04:41:25 -05:00
|
|
|
|
2015-01-22 20:22:03 -06:00
|
|
|
#![unstable(feature = "core")] // not yet reviewed
|
2014-10-31 04:41:25 -05:00
|
|
|
|
2014-11-14 05:37:59 -06:00
|
|
|
use clone::Clone;
|
|
|
|
use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
|
|
|
|
use fmt;
|
2015-02-17 22:48:07 -06:00
|
|
|
use hash::{Hash, self};
|
2015-01-07 21:01:05 -06:00
|
|
|
use iter::IntoIterator;
|
2015-02-20 11:32:55 -06:00
|
|
|
use marker::{Copy, Sized};
|
2014-11-14 05:37:59 -06:00
|
|
|
use option::Option;
|
2015-01-07 21:01:05 -06:00
|
|
|
use slice::{Iter, IterMut, SliceExt};
|
2014-10-31 04:41:25 -05:00
|
|
|
|
|
|
|
// macro for implementing n-ary tuple functions and operations
|
|
|
|
macro_rules! array_impls {
|
|
|
|
($($N:expr)+) => {
|
|
|
|
$(
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 22:40:24 -06:00
|
|
|
impl<T:Copy> Clone for [T; $N] {
|
|
|
|
fn clone(&self) -> [T; $N] {
|
2014-11-14 05:37:59 -06:00
|
|
|
*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-17 22:48:07 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl<T: Hash> Hash for [T; $N] {
|
|
|
|
fn hash<H: hash::Hasher>(&self, state: &mut H) {
|
2015-02-18 17:58:07 -06:00
|
|
|
Hash::hash(&self[..], state)
|
2015-02-17 22:48:07 -06:00
|
|
|
}
|
|
|
|
}
|
2015-01-19 14:16:48 -06:00
|
|
|
|
2015-01-24 11:15:42 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-20 17:45:07 -06:00
|
|
|
impl<T: fmt::Debug> fmt::Debug for [T; $N] {
|
2014-11-14 05:37:59 -06:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2015-02-18 13:48:57 -06:00
|
|
|
fmt::Debug::fmt(&&self[..], f)
|
2014-11-14 05:37:59 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-17 12:06:24 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-13 16:55:10 -06:00
|
|
|
impl<'a, T> IntoIterator for &'a [T; $N] {
|
|
|
|
type Item = &'a T;
|
|
|
|
type IntoIter = Iter<'a, T>;
|
|
|
|
|
|
|
|
fn into_iter(self) -> Iter<'a, T> {
|
|
|
|
self.iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-17 12:06:24 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-07 21:01:05 -06:00
|
|
|
impl<'a, T> IntoIterator for &'a mut [T; $N] {
|
2015-02-13 16:55:10 -06:00
|
|
|
type Item = &'a mut T;
|
2015-02-06 16:47:55 -06:00
|
|
|
type IntoIter = IterMut<'a, T>;
|
2015-01-07 21:01:05 -06:00
|
|
|
|
|
|
|
fn into_iter(self) -> IterMut<'a, T> {
|
|
|
|
self.iter_mut()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 11:32:55 -06:00
|
|
|
// NOTE: some less important impls are omitted to reduce code bloat
|
|
|
|
__impl_slice_eq1! { [A; $N], [B; $N] }
|
|
|
|
__impl_slice_eq2! { [A; $N], [B] }
|
|
|
|
__impl_slice_eq2! { [A; $N], &'b [B] }
|
|
|
|
__impl_slice_eq2! { [A; $N], &'b mut [B] }
|
|
|
|
// __impl_slice_eq2! { [A; $N], &'b [B; $N] }
|
|
|
|
// __impl_slice_eq2! { [A; $N], &'b mut [B; $N] }
|
2014-11-20 23:14:05 -06:00
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 22:40:24 -06:00
|
|
|
impl<T:Eq> Eq for [T; $N] { }
|
2014-10-31 04:41:25 -05:00
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 22:40:24 -06:00
|
|
|
impl<T:PartialOrd> PartialOrd for [T; $N] {
|
2014-10-31 04:41:25 -05:00
|
|
|
#[inline]
|
2014-12-31 22:40:24 -06:00
|
|
|
fn partial_cmp(&self, other: &[T; $N]) -> Option<Ordering> {
|
2015-02-18 13:48:57 -06:00
|
|
|
PartialOrd::partial_cmp(&&self[..], &&other[..])
|
2014-10-31 04:41:25 -05:00
|
|
|
}
|
|
|
|
#[inline]
|
2014-12-31 22:40:24 -06:00
|
|
|
fn lt(&self, other: &[T; $N]) -> bool {
|
2015-02-18 13:48:57 -06:00
|
|
|
PartialOrd::lt(&&self[..], &&other[..])
|
2014-10-31 04:41:25 -05:00
|
|
|
}
|
|
|
|
#[inline]
|
2014-12-31 22:40:24 -06:00
|
|
|
fn le(&self, other: &[T; $N]) -> bool {
|
2015-02-18 13:48:57 -06:00
|
|
|
PartialOrd::le(&&self[..], &&other[..])
|
2014-10-31 04:41:25 -05:00
|
|
|
}
|
|
|
|
#[inline]
|
2014-12-31 22:40:24 -06:00
|
|
|
fn ge(&self, other: &[T; $N]) -> bool {
|
2015-02-18 13:48:57 -06:00
|
|
|
PartialOrd::ge(&&self[..], &&other[..])
|
2014-10-31 04:41:25 -05:00
|
|
|
}
|
|
|
|
#[inline]
|
2014-12-31 22:40:24 -06:00
|
|
|
fn gt(&self, other: &[T; $N]) -> bool {
|
2015-02-18 13:48:57 -06:00
|
|
|
PartialOrd::gt(&&self[..], &&other[..])
|
2014-10-31 04:41:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 22:40:24 -06:00
|
|
|
impl<T:Ord> Ord for [T; $N] {
|
2014-10-31 04:41:25 -05:00
|
|
|
#[inline]
|
2014-12-31 22:40:24 -06:00
|
|
|
fn cmp(&self, other: &[T; $N]) -> Ordering {
|
2015-02-18 13:48:57 -06:00
|
|
|
Ord::cmp(&&self[..], &&other[..])
|
2014-10-31 04:41:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)+
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
array_impls! {
|
|
|
|
0 1 2 3 4 5 6 7 8 9
|
|
|
|
10 11 12 13 14 15 16 17 18 19
|
|
|
|
20 21 22 23 24 25 26 27 28 29
|
|
|
|
30 31 32
|
|
|
|
}
|