From 5592a8f5db52a11b63547b661b3a635655b16980 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 30 Apr 2014 21:55:14 -0700 Subject: [PATCH] core: Inherit the cmp module This removes the TotalOrd and TotalEq implementation macros, they will be added later to the numeric modules (where the other comparison implementations live). --- src/{libstd => libcore}/cmp.rs | 56 ++-------------------------------- src/libcore/lib.rs | 1 + src/libstd/fmt/mod.rs | 11 +++++++ src/libstd/lib.rs | 4 +-- 4 files changed, 15 insertions(+), 57 deletions(-) rename src/{libstd => libcore}/cmp.rs (88%) diff --git a/src/libstd/cmp.rs b/src/libcore/cmp.rs similarity index 88% rename from src/libstd/cmp.rs rename to src/libcore/cmp.rs index a6d6649aca0..867871805d0 100644 --- a/src/libstd/cmp.rs +++ b/src/libcore/cmp.rs @@ -81,32 +81,8 @@ pub trait TotalEq: Eq { fn assert_receiver_is_total_eq(&self) {} } -/// A macro which defines an implementation of TotalEq for a given type. -macro_rules! totaleq_impl( - ($t:ty) => { - impl TotalEq for $t {} - } -) - -totaleq_impl!(bool) - -totaleq_impl!(u8) -totaleq_impl!(u16) -totaleq_impl!(u32) -totaleq_impl!(u64) - -totaleq_impl!(i8) -totaleq_impl!(i16) -totaleq_impl!(i32) -totaleq_impl!(i64) - -totaleq_impl!(int) -totaleq_impl!(uint) - -totaleq_impl!(char) - /// An ordering is, e.g, a result of a comparison between two values. -#[deriving(Clone, Eq, Show)] +#[deriving(Clone, Eq)] pub enum Ordering { /// An ordering where a compared value is less [than another]. Less = -1, @@ -140,6 +116,7 @@ pub trait TotalOrd: TotalEq + Ord { } impl TotalEq for Ordering {} + impl TotalOrd for Ordering { #[inline] fn cmp(&self, other: &Ordering) -> Ordering { @@ -152,35 +129,6 @@ impl Ord for Ordering { fn lt(&self, other: &Ordering) -> bool { (*self as int) < (*other as int) } } -/// A macro which defines an implementation of TotalOrd for a given type. -macro_rules! totalord_impl( - ($t:ty) => { - impl TotalOrd for $t { - #[inline] - fn cmp(&self, other: &$t) -> Ordering { - if *self < *other { Less } - else if *self > *other { Greater } - else { Equal } - } - } - } -) - -totalord_impl!(u8) -totalord_impl!(u16) -totalord_impl!(u32) -totalord_impl!(u64) - -totalord_impl!(i8) -totalord_impl!(i16) -totalord_impl!(i32) -totalord_impl!(i64) - -totalord_impl!(int) -totalord_impl!(uint) - -totalord_impl!(char) - /// Combine orderings, lexically. /// /// For example for a type `(int, int)`, two comparisons could be done. diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index c4a5254b1eb..e5f8011f91e 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -30,6 +30,7 @@ pub mod ptr; /* Core language traits */ +pub mod cmp; pub mod kinds; pub mod ops; pub mod ty; diff --git a/src/libstd/fmt/mod.rs b/src/libstd/fmt/mod.rs index 067ce608980..4a81a6b38a7 100644 --- a/src/libstd/fmt/mod.rs +++ b/src/libstd/fmt/mod.rs @@ -486,6 +486,7 @@ will look like `"\\{"`. use any; use cast; use char::Char; +use cmp; use container::Container; use io::MemWriter; use io; @@ -1315,5 +1316,15 @@ impl Show for iter::MinMaxResult { } } +impl Show for cmp::Ordering { + fn fmt(&self, f: &mut Formatter) -> Result { + match *self { + cmp::Less => write!(f.buf, "Less"), + cmp::Greater => write!(f.buf, "Greater"), + cmp::Equal => write!(f.buf, "Equal"), + } + } +} + // If you expected tests to be here, look instead at the run-pass/ifmt.rs test, // it's a lot easier than creating all of the rt::Piece structures here. diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index 63d86e43427..041959f2ee7 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -135,6 +135,7 @@ extern crate core; #[cfg(not(test))] pub use kinds = core::kinds; #[cfg(not(test))] pub use ops = core::ops; +#[cfg(not(test))] pub use cmp = core::cmp; #[cfg(not(test))] pub use ty = core::ty; pub use core::any; @@ -207,13 +208,10 @@ mod reference; pub mod rc; pub mod gc; - /* Core language traits */ -#[cfg(not(test))] pub mod cmp; #[cfg(not(test))] pub mod owned; - /* Common traits */ pub mod from_str;