//! assert!(SketchyNum {num: 25} != SketchyNum {num: 57});
//! ```
+#![stable]
+
use option::{Option, Some};
/// Trait for values that can be compared for equality and inequality.
/// Eventually, this will be implemented by default for types that implement
/// `Eq`.
#[lang="eq"]
+#[unstable = "Definition may change slightly after trait reform"]
pub trait PartialEq {
/// This method tests for `self` and `other` values to be equal, and is used by `==`.
fn eq(&self, other: &Self) -> bool;
/// - reflexive: `a == a`;
/// - symmetric: `a == b` implies `b == a`; and
/// - transitive: `a == b` and `b == c` implies `a == c`.
+#[unstable = "Definition may change slightly after trait reform"]
pub trait Eq: PartialEq {
// FIXME #13101: this method is used solely by #[deriving] to
// assert that every component of a type implements #[deriving]
/// An ordering is, e.g, a result of a comparison between two values.
#[deriving(Clone, PartialEq, Show)]
+#[stable]
pub enum Ordering {
/// An ordering where a compared value is less [than another].
Less = -1i,
/// true; and
/// - transitive, `a < b` and `b < c` implies `a < c`. The same must hold for
/// both `==` and `>`.
+#[unstable = "Definition may change slightly after trait reform"]
pub trait Ord: Eq + PartialOrd {
/// This method returns an ordering between `self` and `other` values.
///
fn cmp(&self, other: &Self) -> Ordering;
}
+#[unstable = "Trait is unstable."]
impl Eq for Ordering {}
+#[unstable = "Trait is unstable."]
impl Ord for Ordering {
#[inline]
fn cmp(&self, other: &Ordering) -> Ordering {
}
}
+#[unstable = "Trait is unstable."]
impl PartialOrd for Ordering {
#[inline]
fn partial_cmp(&self, other: &Ordering) -> Option<Ordering> {
/// If the first ordering is different, the first ordering is all that must be returned.
/// If the first ordering is equal, then second ordering is returned.
#[inline]
+#[deprecated = "Just call .cmp() on an Ordering"]
pub fn lexical_ordering(o1: Ordering, o2: Ordering) -> Ordering {
match o1 {
Equal => o2,
/// `NaN < 0 == false` and `NaN >= 0 == false` (cf. IEEE 754-2008 section
/// 5.11).
#[lang="ord"]
+#[unstable = "Definition may change slightly after trait reform"]
pub trait PartialOrd: PartialEq {
/// This method returns an ordering between `self` and `other` values
/// if one exists.
/// of different types. The most common use case for this relation is
/// container types; e.g. it is often desirable to be able to use `&str`
/// values to look up entries in a container with `String` keys.
+#[experimental = "Better solutions may be discovered."]
pub trait Equiv<T> {
/// Implement this function to decide equivalent values.
fn equiv(&self, other: &T) -> bool;
/// Compare and return the minimum of two values.
#[inline]
+#[stable]
pub fn min<T: Ord>(v1: T, v2: T) -> T {
if v1 < v2 { v1 } else { v2 }
}
/// Compare and return the maximum of two values.
#[inline]
+#[stable]
pub fn max<T: Ord>(v1: T, v2: T) -> T {
if v1 > v2 { v1 } else { v2 }
}
macro_rules! eq_impl(
($($t:ty)*) => ($(
+ #[unstable = "Trait is unstable."]
impl PartialEq for $t {
#[inline]
fn eq(&self, other: &$t) -> bool { (*self) == (*other) }
)*)
)
+ #[unstable = "Trait is unstable."]
impl PartialEq for () {
#[inline]
fn eq(&self, _other: &()) -> bool { true }
macro_rules! totaleq_impl(
($($t:ty)*) => ($(
+ #[unstable = "Trait is unstable."]
impl Eq for $t {}
)*)
)
macro_rules! ord_impl(
($($t:ty)*) => ($(
+ #[unstable = "Trait is unstable."]
impl PartialOrd for $t {
#[inline]
fn partial_cmp(&self, other: &$t) -> Option<Ordering> {
)*)
)
+ #[unstable = "Trait is unstable."]
impl PartialOrd for () {
#[inline]
fn partial_cmp(&self, _: &()) -> Option<Ordering> {
}
}
+ #[unstable = "Trait is unstable."]
impl PartialOrd for bool {
#[inline]
fn partial_cmp(&self, other: &bool) -> Option<Ordering> {
macro_rules! totalord_impl(
($($t:ty)*) => ($(
+ #[unstable = "Trait is unstable."]
impl Ord for $t {
#[inline]
fn cmp(&self, other: &$t) -> Ordering {
)*)
)
+ #[unstable = "Trait is unstable."]
impl Ord for () {
#[inline]
fn cmp(&self, _other: &()) -> Ordering { Equal }
}
+ #[unstable = "Trait is unstable."]
impl Ord for bool {
#[inline]
fn cmp(&self, other: &bool) -> Ordering {
totalord_impl!(char uint u8 u16 u32 u64 int i8 i16 i32 i64)
// & pointers
+ #[unstable = "Trait is unstable."]
impl<'a, T: PartialEq> PartialEq for &'a T {
#[inline]
fn eq(&self, other: & &'a T) -> bool { *(*self) == *(*other) }
#[inline]
fn ne(&self, other: & &'a T) -> bool { *(*self) != *(*other) }
}
+ #[unstable = "Trait is unstable."]
impl<'a, T: PartialOrd> PartialOrd for &'a T {
#[inline]
fn partial_cmp(&self, other: &&'a T) -> Option<Ordering> {
#[inline]
fn gt(&self, other: & &'a T) -> bool { *(*self) > *(*other) }
}
+ #[unstable = "Trait is unstable."]
impl<'a, T: Ord> Ord for &'a T {
#[inline]
fn cmp(&self, other: & &'a T) -> Ordering { (**self).cmp(*other) }
}
+ #[unstable = "Trait is unstable."]
impl<'a, T: Eq> Eq for &'a T {}
// &mut pointers
+ #[unstable = "Trait is unstable."]
impl<'a, T: PartialEq> PartialEq for &'a mut T {
#[inline]
fn eq(&self, other: &&'a mut T) -> bool { **self == *(*other) }
#[inline]
fn ne(&self, other: &&'a mut T) -> bool { **self != *(*other) }
}
+ #[unstable = "Trait is unstable."]
impl<'a, T: PartialOrd> PartialOrd for &'a mut T {
#[inline]
fn partial_cmp(&self, other: &&'a mut T) -> Option<Ordering> {
#[inline]
fn gt(&self, other: &&'a mut T) -> bool { **self > **other }
}
+ #[unstable = "Trait is unstable."]
impl<'a, T: Ord> Ord for &'a mut T {
#[inline]
fn cmp(&self, other: &&'a mut T) -> Ordering { (**self).cmp(*other) }
}
+ #[unstable = "Trait is unstable."]
impl<'a, T: Eq> Eq for &'a mut T {}
}