From 5e616dbd2189844044e039436b7ed16b44f6595a Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Fri, 20 Feb 2015 20:32:55 +0300 Subject: [PATCH] Tweaks to equality comparisons for slices/arrays/vectors --- src/libcollections/vec.rs | 81 +++++++++++---------------------------- src/libcore/array.rs | 51 ++++-------------------- src/libcore/cmp_macros.rs | 50 ++++++++++++++++++++++++ src/libcore/lib.rs | 3 ++ src/libstd/ffi/c_str.rs | 2 +- 5 files changed, 85 insertions(+), 102 deletions(-) create mode 100644 src/libcore/cmp_macros.rs diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index 1cc2a5235ab..945c4f0ccd8 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -1501,69 +1501,34 @@ impl Extend for Vec { } } -impl PartialEq> for Vec where A: PartialEq { - #[inline] - fn eq(&self, other: &Vec) -> bool { PartialEq::eq(&**self, &**other) } - #[inline] - fn ne(&self, other: &Vec) -> bool { PartialEq::ne(&**self, &**other) } -} +__impl_slice_eq1! { Vec, Vec } +__impl_slice_eq2! { Vec, &'b [B] } +__impl_slice_eq2! { Vec, &'b mut [B] } +__impl_slice_eq2! { CowVec<'a, A>, &'b [B], Clone } +__impl_slice_eq2! { CowVec<'a, A>, &'b mut [B], Clone } +__impl_slice_eq2! { CowVec<'a, A>, Vec, Clone } -macro_rules! impl_eq { - ($lhs:ty, $rhs:ty) => { - impl<'b, A, B> PartialEq<$rhs> for $lhs where A: PartialEq { - #[inline] - fn eq(&self, other: &$rhs) -> bool { PartialEq::eq(&**self, &**other) } - #[inline] - fn ne(&self, other: &$rhs) -> bool { PartialEq::ne(&**self, &**other) } - } - - impl<'b, A, B> PartialEq<$lhs> for $rhs where B: PartialEq { - #[inline] - fn eq(&self, other: &$lhs) -> bool { PartialEq::eq(&**self, &**other) } - #[inline] - fn ne(&self, other: &$lhs) -> bool { PartialEq::ne(&**self, &**other) } - } +macro_rules! array_impls { + ($($N: expr)+) => { + $( + // NOTE: some less important impls are omitted to reduce code bloat + __impl_slice_eq2! { Vec, [B; $N] } + __impl_slice_eq2! { Vec, &'b [B; $N] } + // __impl_slice_eq2! { Vec, &'b mut [B; $N] } + // __impl_slice_eq2! { CowVec<'a, A>, [B; $N], Clone } + // __impl_slice_eq2! { CowVec<'a, A>, &'b [B; $N], Clone } + // __impl_slice_eq2! { CowVec<'a, A>, &'b mut [B; $N], Clone } + )+ } } -impl_eq! { Vec, &'b [B] } -impl_eq! { Vec, &'b mut [B] } - -impl<'a, A, B> PartialEq> for Cow<'a, [A]> where A: PartialEq + Clone { - #[inline] - fn eq(&self, other: &Vec) -> bool { PartialEq::eq(&**self, &**other) } - #[inline] - fn ne(&self, other: &Vec) -> bool { PartialEq::ne(&**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 } -impl<'a, A, B> PartialEq> for Vec where A: Clone, B: PartialEq { - #[inline] - fn eq(&self, other: &Cow<'a, [A]>) -> bool { PartialEq::eq(&**self, &**other) } - #[inline] - fn ne(&self, other: &Cow<'a, [A]>) -> bool { PartialEq::ne(&**self, &**other) } -} - -macro_rules! impl_eq_for_cowvec { - ($rhs:ty) => { - impl<'a, 'b, A, B> PartialEq<$rhs> for Cow<'a, [A]> where A: PartialEq + Clone { - #[inline] - fn eq(&self, other: &$rhs) -> bool { PartialEq::eq(&**self, &**other) } - #[inline] - fn ne(&self, other: &$rhs) -> bool { PartialEq::ne(&**self, &**other) } - } - - impl<'a, 'b, A, B> PartialEq> for $rhs where A: Clone, B: PartialEq { - #[inline] - fn eq(&self, other: &Cow<'a, [A]>) -> bool { PartialEq::eq(&**self, &**other) } - #[inline] - fn ne(&self, other: &Cow<'a, [A]>) -> bool { PartialEq::ne(&**self, &**other) } - } - } -} - -impl_eq_for_cowvec! { &'b [B] } -impl_eq_for_cowvec! { &'b mut [B] } - #[stable(feature = "rust1", since = "1.0.0")] impl PartialOrd for Vec { #[inline] @@ -2480,7 +2445,7 @@ mod tests { fn test_into_boxed_slice() { let xs = vec![1, 2, 3]; let ys = xs.into_boxed_slice(); - assert_eq!(ys, [1, 2, 3]); + assert_eq!(&*ys, [1, 2, 3]); } #[test] diff --git a/src/libcore/array.rs b/src/libcore/array.rs index afb5d95c9f8..a24c181b909 100644 --- a/src/libcore/array.rs +++ b/src/libcore/array.rs @@ -19,8 +19,7 @@ use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering}; use fmt; use hash::{Hash, self}; use iter::IntoIterator; -use marker::Copy; -use ops::Deref; +use marker::{Copy, Sized}; use option::Option; use slice::{Iter, IterMut, SliceExt}; @@ -76,47 +75,13 @@ macro_rules! array_impls { } } - #[stable(feature = "rust1", since = "1.0.0")] - impl PartialEq<[B; $N]> for [A; $N] where A: PartialEq { - #[inline] - fn eq(&self, other: &[B; $N]) -> bool { - &self[..] == &other[..] - } - #[inline] - fn ne(&self, other: &[B; $N]) -> bool { - &self[..] != &other[..] - } - } - - #[stable(feature = "rust1", since = "1.0.0")] - impl<'a, A, B, Rhs> PartialEq for [A; $N] where - A: PartialEq, - Rhs: Deref, - { - #[inline(always)] - fn eq(&self, other: &Rhs) -> bool { - PartialEq::eq(&self[..], &**other) - } - #[inline(always)] - fn ne(&self, other: &Rhs) -> bool { - PartialEq::ne(&self[..], &**other) - } - } - - #[stable(feature = "rust1", since = "1.0.0")] - impl<'a, A, B, Lhs> PartialEq<[B; $N]> for Lhs where - A: PartialEq, - Lhs: Deref - { - #[inline(always)] - fn eq(&self, other: &[B; $N]) -> bool { - PartialEq::eq(&**self, &other[..]) - } - #[inline(always)] - fn ne(&self, other: &[B; $N]) -> bool { - PartialEq::ne(&**self, &other[..]) - } - } + // 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] } #[stable(feature = "rust1", since = "1.0.0")] impl Eq for [T; $N] { } diff --git a/src/libcore/cmp_macros.rs b/src/libcore/cmp_macros.rs new file mode 100644 index 00000000000..18357bac9e6 --- /dev/null +++ b/src/libcore/cmp_macros.rs @@ -0,0 +1,50 @@ +// Copyright 2015 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. + +// Utility macros for implementing PartialEq on slice-like types + +#![doc(hidden)] + +#[macro_export] +macro_rules! __impl_slice_eq1 { + ($Lhs: ty, $Rhs: ty) => { + #[stable(feature = "rust1", since = "1.0.0")] + impl<'a, 'b, A, B> PartialEq<$Rhs> for $Lhs where A: PartialEq { + #[inline] + fn eq(&self, other: &$Rhs) -> bool { &self[..] == &other[..] } + #[inline] + fn ne(&self, other: &$Rhs) -> bool { &self[..] != &other[..] } + } + } +} + +#[macro_export] +macro_rules! __impl_slice_eq2 { + ($Lhs: ty, $Rhs: ty) => { + __impl_slice_eq2! { $Lhs, $Rhs, Sized } + }; + ($Lhs: ty, $Rhs: ty, $Bound: ident) => { + #[stable(feature = "rust1", since = "1.0.0")] + impl<'a, 'b, A: $Bound, B> PartialEq<$Rhs> for $Lhs where A: PartialEq { + #[inline] + fn eq(&self, other: &$Rhs) -> bool { &self[..] == &other[..] } + #[inline] + fn ne(&self, other: &$Rhs) -> bool { &self[..] != &other[..] } + } + + #[stable(feature = "rust1", since = "1.0.0")] + impl<'a, 'b, A: $Bound, B> PartialEq<$Lhs> for $Rhs where B: PartialEq { + #[inline] + fn eq(&self, other: &$Lhs) -> bool { &self[..] == &other[..] } + #[inline] + fn ne(&self, other: &$Lhs) -> bool { &self[..] != &other[..] } + } + } +} diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index 3c58480ff0c..7f52f071080 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -72,6 +72,9 @@ #[macro_use] mod macros; +#[macro_use] +mod cmp_macros; + #[path = "num/float_macros.rs"] #[macro_use] mod float_macros; diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index 8976813d3f9..a8ea4d6d658 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -371,7 +371,7 @@ impl CStr { impl PartialEq for CStr { fn eq(&self, other: &CStr) -> bool { - self.to_bytes().eq(&other.to_bytes()) + self.to_bytes().eq(other.to_bytes()) } } impl Eq for CStr {}