Skip to content

Commit e865415

Browse files
committed
auto merge of #14464 : Sawyer47/rust/issue-12925, r=alexcrichton
This is an attempt of fixing #12925. This PR moves almost all trait implementations for primitive types ((), bool, char, i*, u*, f*) near trait definitions. Only Float trait implementations weren't moved because they heavily rely on constants defined in f32.rs and f64.rs. Some trait implementations had cfg(not(test)) attribute. I suspect it's because of issue 2912. Still, someone who knows the problem better should probably check this code. Closes #12925
2 parents 98c2b4b + dd0d495 commit e865415

20 files changed

+638
-1402
lines changed

src/libcore/bool.rs

-143
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,10 @@
1010

1111
//! Operations on boolean values (`bool` type)
1212
//!
13-
//! A quick summary:
14-
//!
15-
//! Implementations of the following traits:
16-
//!
17-
//! * `Not`
18-
//! * `BitAnd`
19-
//! * `BitOr`
20-
//! * `BitXor`
21-
//! * `Ord`
22-
//! * `TotalOrd`
23-
//! * `Eq`
24-
//! * `TotalEq`
25-
//! * `Default`
26-
//!
2713
//! A `to_bit` conversion function.
2814
2915
use num::{Int, one, zero};
3016

31-
#[cfg(not(test))] use cmp::{Eq, Ord, TotalOrd, Ordering, TotalEq};
32-
#[cfg(not(test))] use ops::{Not, BitAnd, BitOr, BitXor};
33-
#[cfg(not(test))] use default::Default;
34-
3517
/////////////////////////////////////////////////////////////////////////////
3618
// Freestanding functions
3719
/////////////////////////////////////////////////////////////////////////////
@@ -51,131 +33,6 @@ pub fn to_bit<N: Int>(p: bool) -> N {
5133
if p { one() } else { zero() }
5234
}
5335

54-
/////////////////////////////////////////////////////////////////////////////
55-
// Trait impls on `bool`
56-
/////////////////////////////////////////////////////////////////////////////
57-
58-
#[cfg(not(test))]
59-
impl Not<bool> for bool {
60-
/// The logical complement of a boolean value.
61-
///
62-
/// # Examples
63-
///
64-
/// ```rust
65-
/// assert_eq!(!true, false);
66-
/// assert_eq!(!false, true);
67-
/// ```
68-
#[inline]
69-
fn not(&self) -> bool { !*self }
70-
}
71-
72-
#[cfg(not(test))]
73-
impl BitAnd<bool, bool> for bool {
74-
/// Conjunction of two boolean values.
75-
///
76-
/// # Examples
77-
///
78-
/// ```rust
79-
/// assert_eq!(false.bitand(&false), false);
80-
/// assert_eq!(true.bitand(&false), false);
81-
/// assert_eq!(false.bitand(&true), false);
82-
/// assert_eq!(true.bitand(&true), true);
83-
///
84-
/// assert_eq!(false & false, false);
85-
/// assert_eq!(true & false, false);
86-
/// assert_eq!(false & true, false);
87-
/// assert_eq!(true & true, true);
88-
/// ```
89-
#[inline]
90-
fn bitand(&self, b: &bool) -> bool { *self & *b }
91-
}
92-
93-
#[cfg(not(test))]
94-
impl BitOr<bool, bool> for bool {
95-
/// Disjunction of two boolean values.
96-
///
97-
/// # Examples
98-
///
99-
/// ```rust
100-
/// assert_eq!(false.bitor(&false), false);
101-
/// assert_eq!(true.bitor(&false), true);
102-
/// assert_eq!(false.bitor(&true), true);
103-
/// assert_eq!(true.bitor(&true), true);
104-
///
105-
/// assert_eq!(false | false, false);
106-
/// assert_eq!(true | false, true);
107-
/// assert_eq!(false | true, true);
108-
/// assert_eq!(true | true, true);
109-
/// ```
110-
#[inline]
111-
fn bitor(&self, b: &bool) -> bool { *self | *b }
112-
}
113-
114-
#[cfg(not(test))]
115-
impl BitXor<bool, bool> for bool {
116-
/// An 'exclusive or' of two boolean values.
117-
///
118-
/// 'exclusive or' is identical to `or(and(a, not(b)), and(not(a), b))`.
119-
///
120-
/// # Examples
121-
///
122-
/// ```rust
123-
/// assert_eq!(false.bitxor(&false), false);
124-
/// assert_eq!(true.bitxor(&false), true);
125-
/// assert_eq!(false.bitxor(&true), true);
126-
/// assert_eq!(true.bitxor(&true), false);
127-
///
128-
/// assert_eq!(false ^ false, false);
129-
/// assert_eq!(true ^ false, true);
130-
/// assert_eq!(false ^ true, true);
131-
/// assert_eq!(true ^ true, false);
132-
/// ```
133-
#[inline]
134-
fn bitxor(&self, b: &bool) -> bool { *self ^ *b }
135-
}
136-
137-
#[cfg(not(test))]
138-
impl Ord for bool {
139-
#[inline]
140-
fn lt(&self, other: &bool) -> bool {
141-
to_bit::<u8>(*self) < to_bit(*other)
142-
}
143-
}
144-
145-
#[cfg(not(test))]
146-
impl TotalOrd for bool {
147-
#[inline]
148-
fn cmp(&self, other: &bool) -> Ordering {
149-
to_bit::<u8>(*self).cmp(&to_bit(*other))
150-
}
151-
}
152-
153-
/// Equality between two boolean values.
154-
///
155-
/// Two booleans are equal if they have the same value.
156-
///
157-
/// # Examples
158-
///
159-
/// ```rust
160-
/// assert_eq!(false.eq(&true), false);
161-
/// assert_eq!(false == false, true);
162-
/// assert_eq!(false != true, true);
163-
/// assert_eq!(false.ne(&false), false);
164-
/// ```
165-
#[cfg(not(test))]
166-
impl Eq for bool {
167-
#[inline]
168-
fn eq(&self, other: &bool) -> bool { (*self) == (*other) }
169-
}
170-
171-
#[cfg(not(test))]
172-
impl TotalEq for bool {}
173-
174-
#[cfg(not(test))]
175-
impl Default for bool {
176-
fn default() -> bool { false }
177-
}
178-
17936
#[cfg(test)]
18037
mod tests {
18138
use realstd::prelude::*;

src/libcore/char.rs

-30
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ pub use unicode::normalization::decompose_canonical;
3434
/// Returns the compatibility decomposition of a character.
3535
pub use unicode::normalization::decompose_compatible;
3636

37-
#[cfg(not(test))] use cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering};
38-
#[cfg(not(test))] use default::Default;
39-
4037
// UTF-8 ranges and tags for encoding characters
4138
static TAG_CONT: u8 = 0b1000_0000u8;
4239
static TAG_TWO_B: u8 = 0b1100_0000u8;
@@ -601,33 +598,6 @@ impl Char for char {
601598
}
602599
}
603600

604-
#[cfg(not(test))]
605-
impl Eq for char {
606-
#[inline]
607-
fn eq(&self, other: &char) -> bool { (*self) == (*other) }
608-
}
609-
610-
#[cfg(not(test))]
611-
impl TotalEq for char {}
612-
613-
#[cfg(not(test))]
614-
impl Ord for char {
615-
#[inline]
616-
fn lt(&self, other: &char) -> bool { *self < *other }
617-
}
618-
619-
#[cfg(not(test))]
620-
impl TotalOrd for char {
621-
fn cmp(&self, other: &char) -> Ordering {
622-
(*self as u32).cmp(&(*other as u32))
623-
}
624-
}
625-
626-
#[cfg(not(test))]
627-
impl Default for char {
628-
#[inline]
629-
fn default() -> char { '\x00' }
630-
}
631601

632602
#[cfg(test)]
633603
mod test {

src/libcore/cmp.rs

+71-3
Original file line numberDiff line numberDiff line change
@@ -189,27 +189,95 @@ pub fn max<T: TotalOrd>(v1: T, v2: T) -> T {
189189
if v1 > v2 { v1 } else { v2 }
190190
}
191191

192-
// Implementation of Eq/TotalEq for some primitive types
192+
// Implementation of Eq, TotalEq, Ord and TotalOrd for primitive types
193193
#[cfg(not(test))]
194194
mod impls {
195-
use cmp::{Ord, TotalOrd, Eq, TotalEq, Ordering, Equal};
195+
use cmp::{Ord, TotalOrd, Eq, TotalEq, Ordering, Less, Greater, Equal};
196+
197+
macro_rules! eq_impl(
198+
($($t:ty)*) => ($(
199+
impl Eq for $t {
200+
#[inline]
201+
fn eq(&self, other: &$t) -> bool { (*self) == (*other) }
202+
#[inline]
203+
fn ne(&self, other: &$t) -> bool { (*self) != (*other) }
204+
}
205+
)*)
206+
)
196207

197208
impl Eq for () {
198209
#[inline]
199210
fn eq(&self, _other: &()) -> bool { true }
200211
#[inline]
201212
fn ne(&self, _other: &()) -> bool { false }
202213
}
203-
impl TotalEq for () {}
214+
215+
eq_impl!(bool char uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
216+
217+
macro_rules! totaleq_impl(
218+
($($t:ty)*) => ($(
219+
impl TotalEq for $t {}
220+
)*)
221+
)
222+
223+
totaleq_impl!(() bool char uint u8 u16 u32 u64 int i8 i16 i32 i64)
224+
225+
macro_rules! ord_impl(
226+
($($t:ty)*) => ($(
227+
impl Ord for $t {
228+
#[inline]
229+
fn lt(&self, other: &$t) -> bool { (*self) < (*other) }
230+
#[inline]
231+
fn le(&self, other: &$t) -> bool { (*self) <= (*other) }
232+
#[inline]
233+
fn ge(&self, other: &$t) -> bool { (*self) >= (*other) }
234+
#[inline]
235+
fn gt(&self, other: &$t) -> bool { (*self) > (*other) }
236+
}
237+
)*)
238+
)
239+
204240
impl Ord for () {
205241
#[inline]
206242
fn lt(&self, _other: &()) -> bool { false }
207243
}
244+
245+
impl Ord for bool {
246+
#[inline]
247+
fn lt(&self, other: &bool) -> bool {
248+
(*self as u8) < (*other as u8)
249+
}
250+
}
251+
252+
ord_impl!(char uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
253+
254+
macro_rules! totalord_impl(
255+
($($t:ty)*) => ($(
256+
impl TotalOrd for $t {
257+
#[inline]
258+
fn cmp(&self, other: &$t) -> Ordering {
259+
if *self < *other { Less }
260+
else if *self > *other { Greater }
261+
else { Equal }
262+
}
263+
}
264+
)*)
265+
)
266+
208267
impl TotalOrd for () {
209268
#[inline]
210269
fn cmp(&self, _other: &()) -> Ordering { Equal }
211270
}
212271

272+
impl TotalOrd for bool {
273+
#[inline]
274+
fn cmp(&self, other: &bool) -> Ordering {
275+
(*self as u8).cmp(&(*other as u8))
276+
}
277+
}
278+
279+
totalord_impl!(char uint u8 u16 u32 u64 int i8 i16 i32 i64)
280+
213281
// & pointers
214282
impl<'a, T: Eq> Eq for &'a T {
215283
#[inline]

src/libcore/default.rs

+27-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,33 @@ pub trait Default {
1616
fn default() -> Self;
1717
}
1818

19-
impl Default for () {
20-
#[inline]
21-
fn default() -> () { () }
22-
}
19+
macro_rules! default_impl(
20+
($t:ty, $v:expr) => {
21+
impl Default for $t {
22+
#[inline]
23+
fn default() -> $t { $v }
24+
}
25+
}
26+
)
27+
28+
default_impl!((), ())
29+
default_impl!(bool, false)
30+
default_impl!(char, '\x00')
31+
32+
default_impl!(uint, 0u)
33+
default_impl!(u8, 0u8)
34+
default_impl!(u16, 0u16)
35+
default_impl!(u32, 0u32)
36+
default_impl!(u64, 0u64)
37+
38+
default_impl!(int, 0i)
39+
default_impl!(i8, 0i8)
40+
default_impl!(i16, 0i16)
41+
default_impl!(i32, 0i32)
42+
default_impl!(i64, 0i64)
43+
44+
default_impl!(f32, 0.0f32)
45+
default_impl!(f64, 0.0f64)
2346

2447
impl<T: Default + 'static> Default for @T {
2548
fn default() -> @T { @Default::default() }

0 commit comments

Comments
 (0)