Skip to content

Commit 4673686

Browse files
committed
Removed has_* predicates from NumStrConv trait
Moved `is_*` predicates into standalone functions
1 parent a0846d4 commit 4673686

File tree

1 file changed

+34
-50
lines changed

1 file changed

+34
-50
lines changed

src/libcore/num/strconv.rs

Lines changed: 34 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -36,34 +36,47 @@ pub enum SignFormat {
3636
SignAll
3737
}
3838

39-
pub trait NumStrConv {
40-
static pure fn has_NaN() -> bool;
41-
static pure fn has_inf() -> bool;
42-
static pure fn has_neg_inf() -> bool;
43-
static pure fn has_neg_zero() -> bool;
39+
#[inline(always)]
40+
pure fn is_NaN<T:Eq>(num: &T) -> bool {
41+
*num != *num
42+
}
43+
44+
#[inline(always)]
45+
pure fn is_inf<T:Eq+NumStrConv>(num: &T) -> bool {
46+
match NumStrConv::inf() {
47+
None => false,
48+
Some(n) => *num == n
49+
}
50+
}
51+
52+
#[inline(always)]
53+
pure fn is_neg_inf<T:Eq+NumStrConv>(num: &T) -> bool {
54+
match NumStrConv::neg_inf() {
55+
None => false,
56+
Some(n) => *num == n
57+
}
58+
}
59+
60+
#[inline(always)]
61+
pure fn is_neg_zero<T:Eq+One+Zero+NumStrConv+Div<T,T>>(num: &T) -> bool {
62+
let _0: T = Zero::zero();
63+
let _1: T = One::one();
64+
65+
*num == _0 && is_neg_inf(&(_1 / *num))
66+
}
4467

68+
pub trait NumStrConv {
4569
static pure fn NaN() -> Option<Self>;
4670
static pure fn inf() -> Option<Self>;
4771
static pure fn neg_inf() -> Option<Self>;
4872
static pure fn neg_zero() -> Option<Self>;
4973

50-
pure fn is_NaN(&self) -> bool;
51-
pure fn is_inf(&self) -> bool;
52-
pure fn is_neg_inf(&self) -> bool;
53-
pure fn is_neg_zero(&self) -> bool;
54-
5574
pure fn round_to_zero(&self) -> Self;
5675
pure fn fractional_part(&self) -> Self;
57-
5876
}
5977

6078
macro_rules! impl_NumStrConv_Floating (($t:ty) => (
6179
impl NumStrConv for $t {
62-
#[inline(always)] static pure fn has_NaN() -> bool { true }
63-
#[inline(always)] static pure fn has_inf() -> bool { true }
64-
#[inline(always)] static pure fn has_neg_inf() -> bool { true }
65-
#[inline(always)] static pure fn has_neg_zero() -> bool { true }
66-
6780
#[inline(always)]
6881
static pure fn NaN() -> Option<$t> { Some( 0.0 / 0.0) }
6982
#[inline(always)]
@@ -73,27 +86,10 @@ macro_rules! impl_NumStrConv_Floating (($t:ty) => (
7386
#[inline(always)]
7487
static pure fn neg_zero() -> Option<$t> { Some(-0.0 ) }
7588

76-
#[inline(always)] pure fn is_NaN(&self) -> bool { *self != *self }
77-
78-
#[inline(always)]
79-
pure fn is_inf(&self) -> bool {
80-
*self == NumStrConv::inf().unwrap()
81-
}
82-
83-
#[inline(always)]
84-
pure fn is_neg_inf(&self) -> bool {
85-
*self == NumStrConv::neg_inf().unwrap()
86-
}
87-
88-
#[inline(always)]
89-
pure fn is_neg_zero(&self) -> bool {
90-
*self == 0.0 && (1.0 / *self).is_neg_inf()
91-
}
92-
9389
#[inline(always)]
9490
pure fn round_to_zero(&self) -> $t {
9591
( if *self < 0.0 { f64::ceil(*self as f64) }
96-
else { f64::floor(*self as f64) }
92+
else { f64::floor(*self as f64) }
9793
) as $t
9894
}
9995

@@ -106,21 +102,11 @@ macro_rules! impl_NumStrConv_Floating (($t:ty) => (
106102

107103
macro_rules! impl_NumStrConv_Integer (($t:ty) => (
108104
impl NumStrConv for $t {
109-
#[inline(always)] static pure fn has_NaN() -> bool { false }
110-
#[inline(always)] static pure fn has_inf() -> bool { false }
111-
#[inline(always)] static pure fn has_neg_inf() -> bool { false }
112-
#[inline(always)] static pure fn has_neg_zero() -> bool { false }
113-
114105
#[inline(always)] static pure fn NaN() -> Option<$t> { None }
115106
#[inline(always)] static pure fn inf() -> Option<$t> { None }
116107
#[inline(always)] static pure fn neg_inf() -> Option<$t> { None }
117108
#[inline(always)] static pure fn neg_zero() -> Option<$t> { None }
118109

119-
#[inline(always)] pure fn is_NaN(&self) -> bool { false }
120-
#[inline(always)] pure fn is_inf(&self) -> bool { false }
121-
#[inline(always)] pure fn is_neg_inf(&self) -> bool { false }
122-
#[inline(always)] pure fn is_neg_zero(&self) -> bool { false }
123-
124110
#[inline(always)] pure fn round_to_zero(&self) -> $t { *self }
125111
#[inline(always)] pure fn fractional_part(&self) -> $t { 0 }
126112
}
@@ -190,25 +176,23 @@ pub pure fn to_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+NumStrConv+Copy+
190176
let _0: T = Zero::zero();
191177
let _1: T = One::one();
192178

193-
if NumStrConv::has_NaN::<T>() && num.is_NaN() {
179+
if is_NaN(num) {
194180
return (str::to_bytes("NaN"), true);
195181
}
196-
if NumStrConv::has_inf::<T>() && num.is_inf(){
182+
else if is_inf(num){
197183
return match sign {
198184
SignAll => (str::to_bytes("+inf"), true),
199185
_ => (str::to_bytes("inf"), true)
200186
}
201187
}
202-
if NumStrConv::has_neg_inf::<T>() && num.is_neg_inf() {
188+
else if is_neg_inf(num) {
203189
return match sign {
204190
SignNone => (str::to_bytes("inf"), true),
205191
_ => (str::to_bytes("-inf"), true),
206192
}
207193
}
208194

209-
let neg = *num < _0 || (negative_zero
210-
&& NumStrConv::has_neg_zero::<T>()
211-
&& num.is_neg_zero());
195+
let neg = *num < _0 || (negative_zero && is_neg_zero(num));
212196
let mut buf: ~[u8] = ~[];
213197
let radix_gen: T = cast(radix as int);
214198

0 commit comments

Comments
 (0)