From 33ef78fa8bbe9b8d05ba0da607d4da5e31475a95 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Fri, 31 Oct 2014 05:41:25 -0400 Subject: Add impls of the comparison operators for fixed-length arrays of lengths 0...32 and repair various cases where slices and fixed-length arrays were being compared. --- src/libcore/array.rs | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 src/libcore/array.rs (limited to 'src/libcore/array.rs') diff --git a/src/libcore/array.rs b/src/libcore/array.rs new file mode 100644 index 00000000000..999574b4d7d --- /dev/null +++ b/src/libcore/array.rs @@ -0,0 +1,87 @@ +// 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 or the MIT license +// , 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. + */ + +#![doc(primitive = "tuple")] +#![stable] + +#[unstable = "this is just a documentation module and should not be part \ + of the public api"] +pub use unit; + +use cmp::*; +use option::{Option}; + +// macro for implementing n-ary tuple functions and operations +macro_rules! array_impls { + ($($N:expr)+) => { + $( + #[unstable = "waiting for PartialEq to stabilize"] + impl PartialEq for [T, ..$N] { + #[inline] + fn eq(&self, other: &[T, ..$N]) -> bool { + self[] == other[] + } + #[inline] + fn ne(&self, other: &[T, ..$N]) -> bool { + self[] != other[] + } + } + + #[unstable = "waiting for Eq to stabilize"] + impl Eq for [T, ..$N] { } + + #[unstable = "waiting for PartialOrd to stabilize"] + impl PartialOrd for [T, ..$N] { + #[inline] + fn partial_cmp(&self, other: &[T, ..$N]) -> Option { + PartialOrd::partial_cmp(&self[], &other[]) + } + #[inline] + fn lt(&self, other: &[T, ..$N]) -> bool { + PartialOrd::lt(&self[], &other[]) + } + #[inline] + fn le(&self, other: &[T, ..$N]) -> bool { + PartialOrd::le(&self[], &other[]) + } + #[inline] + fn ge(&self, other: &[T, ..$N]) -> bool { + PartialOrd::ge(&self[], &other[]) + } + #[inline] + fn gt(&self, other: &[T, ..$N]) -> bool { + PartialOrd::gt(&self[], &other[]) + } + } + + #[unstable = "waiting for Ord to stabilize"] + impl Ord for [T, ..$N] { + #[inline] + fn cmp(&self, other: &[T, ..$N]) -> Ordering { + 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 +} + -- cgit 1.4.1-3-g733a5