Skip to content

Commit

Permalink
Move trait impls for primitives near trait definition
Browse files Browse the repository at this point in the history
  • Loading branch information
Sawyer47 committed May 28, 2014
1 parent a183829 commit dd0d495
Show file tree
Hide file tree
Showing 20 changed files with 638 additions and 1,402 deletions.
143 changes: 0 additions & 143 deletions src/libcore/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,10 @@

//! Operations on boolean values (`bool` type)
//!
//! A quick summary:
//!
//! Implementations of the following traits:
//!
//! * `Not`
//! * `BitAnd`
//! * `BitOr`
//! * `BitXor`
//! * `Ord`
//! * `TotalOrd`
//! * `Eq`
//! * `TotalEq`
//! * `Default`
//!
//! A `to_bit` conversion function.
use num::{Int, one, zero};

#[cfg(not(test))] use cmp::{Eq, Ord, TotalOrd, Ordering, TotalEq};
#[cfg(not(test))] use ops::{Not, BitAnd, BitOr, BitXor};
#[cfg(not(test))] use default::Default;

/////////////////////////////////////////////////////////////////////////////
// Freestanding functions
/////////////////////////////////////////////////////////////////////////////
Expand All @@ -51,131 +33,6 @@ pub fn to_bit<N: Int>(p: bool) -> N {
if p { one() } else { zero() }
}

/////////////////////////////////////////////////////////////////////////////
// Trait impls on `bool`
/////////////////////////////////////////////////////////////////////////////

#[cfg(not(test))]
impl Not<bool> for bool {
/// The logical complement of a boolean value.
///
/// # Examples
///
/// ```rust
/// assert_eq!(!true, false);
/// assert_eq!(!false, true);
/// ```
#[inline]
fn not(&self) -> bool { !*self }
}

#[cfg(not(test))]
impl BitAnd<bool, bool> for bool {
/// Conjunction of two boolean values.
///
/// # Examples
///
/// ```rust
/// assert_eq!(false.bitand(&false), false);
/// assert_eq!(true.bitand(&false), false);
/// assert_eq!(false.bitand(&true), false);
/// assert_eq!(true.bitand(&true), true);
///
/// assert_eq!(false & false, false);
/// assert_eq!(true & false, false);
/// assert_eq!(false & true, false);
/// assert_eq!(true & true, true);
/// ```
#[inline]
fn bitand(&self, b: &bool) -> bool { *self & *b }
}

#[cfg(not(test))]
impl BitOr<bool, bool> for bool {
/// Disjunction of two boolean values.
///
/// # Examples
///
/// ```rust
/// assert_eq!(false.bitor(&false), false);
/// assert_eq!(true.bitor(&false), true);
/// assert_eq!(false.bitor(&true), true);
/// assert_eq!(true.bitor(&true), true);
///
/// assert_eq!(false | false, false);
/// assert_eq!(true | false, true);
/// assert_eq!(false | true, true);
/// assert_eq!(true | true, true);
/// ```
#[inline]
fn bitor(&self, b: &bool) -> bool { *self | *b }
}

#[cfg(not(test))]
impl BitXor<bool, bool> for bool {
/// An 'exclusive or' of two boolean values.
///
/// 'exclusive or' is identical to `or(and(a, not(b)), and(not(a), b))`.
///
/// # Examples
///
/// ```rust
/// assert_eq!(false.bitxor(&false), false);
/// assert_eq!(true.bitxor(&false), true);
/// assert_eq!(false.bitxor(&true), true);
/// assert_eq!(true.bitxor(&true), false);
///
/// assert_eq!(false ^ false, false);
/// assert_eq!(true ^ false, true);
/// assert_eq!(false ^ true, true);
/// assert_eq!(true ^ true, false);
/// ```
#[inline]
fn bitxor(&self, b: &bool) -> bool { *self ^ *b }
}

#[cfg(not(test))]
impl Ord for bool {
#[inline]
fn lt(&self, other: &bool) -> bool {
to_bit::<u8>(*self) < to_bit(*other)
}
}

#[cfg(not(test))]
impl TotalOrd for bool {
#[inline]
fn cmp(&self, other: &bool) -> Ordering {
to_bit::<u8>(*self).cmp(&to_bit(*other))
}
}

/// Equality between two boolean values.
///
/// Two booleans are equal if they have the same value.
///
/// # Examples
///
/// ```rust
/// assert_eq!(false.eq(&true), false);
/// assert_eq!(false == false, true);
/// assert_eq!(false != true, true);
/// assert_eq!(false.ne(&false), false);
/// ```
#[cfg(not(test))]
impl Eq for bool {
#[inline]
fn eq(&self, other: &bool) -> bool { (*self) == (*other) }
}

#[cfg(not(test))]
impl TotalEq for bool {}

#[cfg(not(test))]
impl Default for bool {
fn default() -> bool { false }
}

#[cfg(test)]
mod tests {
use realstd::prelude::*;
Expand Down
30 changes: 0 additions & 30 deletions src/libcore/char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ pub use unicode::normalization::decompose_canonical;
/// Returns the compatibility decomposition of a character.
pub use unicode::normalization::decompose_compatible;

#[cfg(not(test))] use cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering};
#[cfg(not(test))] use default::Default;

// UTF-8 ranges and tags for encoding characters
static TAG_CONT: u8 = 0b1000_0000u8;
static TAG_TWO_B: u8 = 0b1100_0000u8;
Expand Down Expand Up @@ -601,33 +598,6 @@ impl Char for char {
}
}

#[cfg(not(test))]
impl Eq for char {
#[inline]
fn eq(&self, other: &char) -> bool { (*self) == (*other) }
}

#[cfg(not(test))]
impl TotalEq for char {}

#[cfg(not(test))]
impl Ord for char {
#[inline]
fn lt(&self, other: &char) -> bool { *self < *other }
}

#[cfg(not(test))]
impl TotalOrd for char {
fn cmp(&self, other: &char) -> Ordering {
(*self as u32).cmp(&(*other as u32))
}
}

#[cfg(not(test))]
impl Default for char {
#[inline]
fn default() -> char { '\x00' }
}

#[cfg(test)]
mod test {
Expand Down
74 changes: 71 additions & 3 deletions src/libcore/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,27 +189,95 @@ pub fn max<T: TotalOrd>(v1: T, v2: T) -> T {
if v1 > v2 { v1 } else { v2 }
}

// Implementation of Eq/TotalEq for some primitive types
// Implementation of Eq, TotalEq, Ord and TotalOrd for primitive types
#[cfg(not(test))]
mod impls {
use cmp::{Ord, TotalOrd, Eq, TotalEq, Ordering, Equal};
use cmp::{Ord, TotalOrd, Eq, TotalEq, Ordering, Less, Greater, Equal};

macro_rules! eq_impl(
($($t:ty)*) => ($(
impl Eq for $t {
#[inline]
fn eq(&self, other: &$t) -> bool { (*self) == (*other) }
#[inline]
fn ne(&self, other: &$t) -> bool { (*self) != (*other) }
}
)*)
)

impl Eq for () {
#[inline]
fn eq(&self, _other: &()) -> bool { true }
#[inline]
fn ne(&self, _other: &()) -> bool { false }
}
impl TotalEq for () {}

eq_impl!(bool char uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)

macro_rules! totaleq_impl(
($($t:ty)*) => ($(
impl TotalEq for $t {}
)*)
)

totaleq_impl!(() bool char uint u8 u16 u32 u64 int i8 i16 i32 i64)

macro_rules! ord_impl(
($($t:ty)*) => ($(
impl Ord for $t {
#[inline]
fn lt(&self, other: &$t) -> bool { (*self) < (*other) }
#[inline]
fn le(&self, other: &$t) -> bool { (*self) <= (*other) }
#[inline]
fn ge(&self, other: &$t) -> bool { (*self) >= (*other) }
#[inline]
fn gt(&self, other: &$t) -> bool { (*self) > (*other) }
}
)*)
)

impl Ord for () {
#[inline]
fn lt(&self, _other: &()) -> bool { false }
}

impl Ord for bool {
#[inline]
fn lt(&self, other: &bool) -> bool {
(*self as u8) < (*other as u8)
}
}

ord_impl!(char uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)

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 }
}
}
)*)
)

impl TotalOrd for () {
#[inline]
fn cmp(&self, _other: &()) -> Ordering { Equal }
}

impl TotalOrd for bool {
#[inline]
fn cmp(&self, other: &bool) -> Ordering {
(*self as u8).cmp(&(*other as u8))
}
}

totalord_impl!(char uint u8 u16 u32 u64 int i8 i16 i32 i64)

// & pointers
impl<'a, T: Eq> Eq for &'a T {
#[inline]
Expand Down
31 changes: 27 additions & 4 deletions src/libcore/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,33 @@ pub trait Default {
fn default() -> Self;
}

impl Default for () {
#[inline]
fn default() -> () { () }
}
macro_rules! default_impl(
($t:ty, $v:expr) => {
impl Default for $t {
#[inline]
fn default() -> $t { $v }
}
}
)

default_impl!((), ())
default_impl!(bool, false)
default_impl!(char, '\x00')

default_impl!(uint, 0u)
default_impl!(u8, 0u8)
default_impl!(u16, 0u16)
default_impl!(u32, 0u32)
default_impl!(u64, 0u64)

default_impl!(int, 0i)
default_impl!(i8, 0i8)
default_impl!(i16, 0i16)
default_impl!(i32, 0i32)
default_impl!(i64, 0i64)

default_impl!(f32, 0.0f32)
default_impl!(f64, 0.0f64)

impl<T: Default + 'static> Default for @T {
fn default() -> @T { @Default::default() }
Expand Down
Loading

0 comments on commit dd0d495

Please sign in to comment.