rust/src/libcore/array.rs

153 lines
5.0 KiB
Rust
Raw Normal View History

// 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.
//! Implementations of things like `Eq` for fixed-length arrays
//! up to a certain length. Eventually we should able to generalize
//! to all lengths.
#![unstable(feature = "core")] // not yet reviewed
use clone::Clone;
use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
use fmt;
use hash::{Hash, Hasher, self};
2015-01-07 21:01:05 -06:00
use iter::IntoIterator;
2015-01-06 16:33:42 -06:00
use marker::Copy;
2015-01-27 19:54:25 -06:00
use ops::Deref;
use option::Option;
2015-01-07 21:01:05 -06:00
use slice::{Iter, IterMut, SliceExt};
// 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] {
*self
}
}
impl<S: hash::Writer + Hasher, T: Hash<S>> Hash<S> for [T; $N] {
fn hash(&self, state: &mut S) {
Hash::hash(&self[], state)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
std: Rename Show/String to Debug/Display This commit is an implementation of [RFC 565][rfc] which is a stabilization of the `std::fmt` module and the implementations of various formatting traits. Specifically, the following changes were performed: [rfc]: https://github.com/rust-lang/rfcs/blob/master/text/0565-show-string-guidelines.md * The `Show` trait is now deprecated, it was renamed to `Debug` * The `String` trait is now deprecated, it was renamed to `Display` * Many `Debug` and `Display` implementations were audited in accordance with the RFC and audited implementations now have the `#[stable]` attribute * Integers and floats no longer print a suffix * Smart pointers no longer print details that they are a smart pointer * Paths with `Debug` are now quoted and escape characters * The `unwrap` methods on `Result` now require `Display` instead of `Debug` * The `Error` trait no longer has a `detail` method and now requires that `Display` must be implemented. With the loss of `String`, this has moved into libcore. * `impl<E: Error> FromError<E> for Box<Error>` now exists * `derive(Show)` has been renamed to `derive(Debug)`. This is not currently warned about due to warnings being emitted on stage1+ While backwards compatibility is attempted to be maintained with a blanket implementation of `Display` for the old `String` trait (and the same for `Show`/`Debug`) this is still a breaking change due to primitives no longer implementing `String` as well as modifications such as `unwrap` and the `Error` trait. Most code is fairly straightforward to update with a rename or tweaks of method calls. [breaking-change] Closes #21436
2015-01-20 17:45:07 -06:00
impl<T: fmt::Debug> fmt::Debug for [T; $N] {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
std: Rename Show/String to Debug/Display This commit is an implementation of [RFC 565][rfc] which is a stabilization of the `std::fmt` module and the implementations of various formatting traits. Specifically, the following changes were performed: [rfc]: https://github.com/rust-lang/rfcs/blob/master/text/0565-show-string-guidelines.md * The `Show` trait is now deprecated, it was renamed to `Debug` * The `String` trait is now deprecated, it was renamed to `Display` * Many `Debug` and `Display` implementations were audited in accordance with the RFC and audited implementations now have the `#[stable]` attribute * Integers and floats no longer print a suffix * Smart pointers no longer print details that they are a smart pointer * Paths with `Debug` are now quoted and escape characters * The `unwrap` methods on `Result` now require `Display` instead of `Debug` * The `Error` trait no longer has a `detail` method and now requires that `Display` must be implemented. With the loss of `String`, this has moved into libcore. * `impl<E: Error> FromError<E> for Box<Error>` now exists * `derive(Show)` has been renamed to `derive(Debug)`. This is not currently warned about due to warnings being emitted on stage1+ While backwards compatibility is attempted to be maintained with a blanket implementation of `Display` for the old `String` trait (and the same for `Show`/`Debug`) this is still a breaking change due to primitives no longer implementing `String` as well as modifications such as `unwrap` and the `Error` trait. Most code is fairly straightforward to update with a rename or tweaks of method calls. [breaking-change] Closes #21436
2015-01-20 17:45:07 -06:00
fmt::Debug::fmt(&&self[], f)
}
}
2015-01-07 21:01:05 -06:00
impl<'a, T> IntoIterator for &'a [T; $N] {
type Iter = Iter<'a, T>;
fn into_iter(self) -> Iter<'a, T> {
self.iter()
}
}
impl<'a, T> IntoIterator for &'a mut [T; $N] {
type Iter = IterMut<'a, T>;
fn into_iter(self) -> IterMut<'a, T> {
self.iter_mut()
}
}
2015-01-23 23:48:20 -06:00
#[stable(feature = "rust1", since = "1.0.0")]
2014-12-31 22:40:24 -06:00
impl<A, B> PartialEq<[B; $N]> for [A; $N] where A: PartialEq<B> {
#[inline]
2014-12-31 22:40:24 -06:00
fn eq(&self, other: &[B; $N]) -> bool {
2015-01-07 10:58:31 -06:00
&self[] == &other[]
}
#[inline]
2014-12-31 22:40:24 -06:00
fn ne(&self, other: &[B; $N]) -> bool {
2015-01-07 10:58:31 -06:00
&self[] != &other[]
}
}
2015-01-23 23:48:20 -06:00
#[stable(feature = "rust1", since = "1.0.0")]
2014-12-31 22:40:24 -06:00
impl<'a, A, B, Rhs> PartialEq<Rhs> for [A; $N] where
A: PartialEq<B>,
2015-01-01 13:53:20 -06:00
Rhs: Deref<Target=[B]>,
{
#[inline(always)]
2015-01-03 22:43:24 -06:00
fn eq(&self, other: &Rhs) -> bool {
2015-01-07 10:58:31 -06:00
PartialEq::eq(&self[], &**other)
2015-01-03 22:43:24 -06:00
}
#[inline(always)]
2015-01-03 22:43:24 -06:00
fn ne(&self, other: &Rhs) -> bool {
2015-01-07 10:58:31 -06:00
PartialEq::ne(&self[], &**other)
2015-01-03 22:43:24 -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<'a, A, B, Lhs> PartialEq<[B; $N]> for Lhs where
A: PartialEq<B>,
2015-01-01 13:53:20 -06:00
Lhs: Deref<Target=[A]>
{
#[inline(always)]
2015-01-03 22:43:24 -06:00
fn eq(&self, other: &[B; $N]) -> bool {
2015-01-07 10:58:31 -06:00
PartialEq::eq(&**self, &other[])
2015-01-03 22:43:24 -06:00
}
#[inline(always)]
2015-01-03 22:43:24 -06:00
fn ne(&self, other: &[B; $N]) -> bool {
2015-01-07 10:58:31 -06:00
PartialEq::ne(&**self, &other[])
2015-01-03 22:43:24 -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] { }
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] {
#[inline]
2014-12-31 22:40:24 -06:00
fn partial_cmp(&self, other: &[T; $N]) -> Option<Ordering> {
2015-01-07 10:58:31 -06:00
PartialOrd::partial_cmp(&&self[], &&other[])
}
#[inline]
2014-12-31 22:40:24 -06:00
fn lt(&self, other: &[T; $N]) -> bool {
2015-01-07 10:58:31 -06:00
PartialOrd::lt(&&self[], &&other[])
}
#[inline]
2014-12-31 22:40:24 -06:00
fn le(&self, other: &[T; $N]) -> bool {
2015-01-07 10:58:31 -06:00
PartialOrd::le(&&self[], &&other[])
}
#[inline]
2014-12-31 22:40:24 -06:00
fn ge(&self, other: &[T; $N]) -> bool {
2015-01-07 10:58:31 -06:00
PartialOrd::ge(&&self[], &&other[])
}
#[inline]
2014-12-31 22:40:24 -06:00
fn gt(&self, other: &[T; $N]) -> bool {
2015-01-07 10:58:31 -06:00
PartialOrd::gt(&&self[], &&other[])
}
}
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] {
#[inline]
2014-12-31 22:40:24 -06:00
fn cmp(&self, other: &[T; $N]) -> Ordering {
2015-01-07 10:58:31 -06:00
Ord::cmp(&&self[], &&other[])
}
}
)+
}
}
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
}