Skip to content

Commit 14ec6db

Browse files
committed
Use std::util::ForType in numeric traits
1 parent 9405adf commit 14ec6db

File tree

6 files changed

+59
-54
lines changed

6 files changed

+59
-54
lines changed

src/libstd/num/f32.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ use libc::c_int;
1616
use num::{Zero, One, strconv};
1717
use num::{FPCategory, FPNaN, FPInfinite , FPZero, FPSubnormal, FPNormal};
1818
use num;
19-
use prelude::*;
2019
use to_str;
20+
use util::ForType;
21+
use prelude::*;
2122

2223
pub use cmath::c_float_targ_consts::*;
2324

@@ -584,13 +585,13 @@ impl Bounded for f32 {
584585

585586
impl Primitive for f32 {
586587
#[inline]
587-
fn bits(_: Option<f32>) -> uint { 32 }
588+
fn bits(_: ForType<f32>) -> uint { 32 }
588589

589590
#[inline]
590-
fn bytes(_: Option<f32>) -> uint { Primitive::bits(Some(0f32)) / 8 }
591+
fn bytes(_: ForType<f32>) -> uint { Primitive::bits(ForType::<f32>) / 8 }
591592

592593
#[inline]
593-
fn is_signed(_: Option<f32>) -> bool { true }
594+
fn is_signed(_: ForType<f32>) -> bool { true }
594595
}
595596

596597
impl Float for f32 {
@@ -647,25 +648,25 @@ impl Float for f32 {
647648
}
648649

649650
#[inline]
650-
fn mantissa_digits(_: Option<f32>) -> uint { 24 }
651+
fn mantissa_digits(_: ForType<f32>) -> uint { 24 }
651652

652653
#[inline]
653-
fn digits(_: Option<f32>) -> uint { 6 }
654+
fn digits(_: ForType<f32>) -> uint { 6 }
654655

655656
#[inline]
656657
fn epsilon() -> f32 { 1.19209290e-07 }
657658

658659
#[inline]
659-
fn min_exp(_: Option<f32>) -> int { -125 }
660+
fn min_exp(_: ForType<f32>) -> int { -125 }
660661

661662
#[inline]
662-
fn max_exp(_: Option<f32>) -> int { 128 }
663+
fn max_exp(_: ForType<f32>) -> int { 128 }
663664

664665
#[inline]
665-
fn min_10_exp(_: Option<f32>) -> int { -37 }
666+
fn min_10_exp(_: ForType<f32>) -> int { -37 }
666667

667668
#[inline]
668-
fn max_10_exp(_: Option<f32>) -> int { 38 }
669+
fn max_10_exp(_: ForType<f32>) -> int { 38 }
669670

670671
/// Constructs a floating point number by multiplying `x` by 2 raised to the power of `exp`
671672
#[inline]
@@ -927,6 +928,7 @@ mod tests {
927928
use num::*;
928929
use num;
929930
use mem;
931+
use util::ForType;
930932

931933
#[test]
932934
fn test_num() {
@@ -1195,9 +1197,8 @@ mod tests {
11951197

11961198
#[test]
11971199
fn test_primitive() {
1198-
let none: Option<f32> = None;
1199-
assert_eq!(Primitive::bits(none), mem::size_of::<f32>() * 8);
1200-
assert_eq!(Primitive::bytes(none), mem::size_of::<f32>());
1200+
assert_eq!(Primitive::bits(ForType::<f32>), mem::size_of::<f32>() * 8);
1201+
assert_eq!(Primitive::bytes(ForType::<f32>), mem::size_of::<f32>());
12011202
}
12021203

12031204
#[test]

src/libstd/num/f64.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ use libc::c_int;
1717
use num::{Zero, One, strconv};
1818
use num::{FPCategory, FPNaN, FPInfinite , FPZero, FPSubnormal, FPNormal};
1919
use num;
20-
use prelude::*;
2120
use to_str;
21+
use util::ForType;
22+
use prelude::*;
2223

2324
pub use cmath::c_double_targ_consts::*;
2425
pub use cmp::{min, max};
@@ -632,13 +633,13 @@ impl Bounded for f64 {
632633

633634
impl Primitive for f64 {
634635
#[inline]
635-
fn bits(_: Option<f64>) -> uint { 64 }
636+
fn bits(_: ForType<f64>) -> uint { 64 }
636637

637638
#[inline]
638-
fn bytes(_: Option<f64>) -> uint { Primitive::bits(Some(0f64)) / 8 }
639+
fn bytes(_: ForType<f64>) -> uint { Primitive::bits(ForType::<f64>) / 8 }
639640

640641
#[inline]
641-
fn is_signed(_: Option<f64>) -> bool { true }
642+
fn is_signed(_: ForType<f64>) -> bool { true }
642643
}
643644

644645
impl Float for f64 {
@@ -695,25 +696,25 @@ impl Float for f64 {
695696
}
696697

697698
#[inline]
698-
fn mantissa_digits(_: Option<f64>) -> uint { 53 }
699+
fn mantissa_digits(_: ForType<f64>) -> uint { 53 }
699700

700701
#[inline]
701-
fn digits(_: Option<f64>) -> uint { 15 }
702+
fn digits(_: ForType<f64>) -> uint { 15 }
702703

703704
#[inline]
704705
fn epsilon() -> f64 { 2.2204460492503131e-16 }
705706

706707
#[inline]
707-
fn min_exp(_: Option<f64>) -> int { -1021 }
708+
fn min_exp(_: ForType<f64>) -> int { -1021 }
708709

709710
#[inline]
710-
fn max_exp(_: Option<f64>) -> int { 1024 }
711+
fn max_exp(_: ForType<f64>) -> int { 1024 }
711712

712713
#[inline]
713-
fn min_10_exp(_: Option<f64>) -> int { -307 }
714+
fn min_10_exp(_: ForType<f64>) -> int { -307 }
714715

715716
#[inline]
716-
fn max_10_exp(_: Option<f64>) -> int { 308 }
717+
fn max_10_exp(_: ForType<f64>) -> int { 308 }
717718

718719
/// Constructs a floating point number by multiplying `x` by 2 raised to the power of `exp`
719720
#[inline]
@@ -975,6 +976,7 @@ mod tests {
975976
use num::*;
976977
use num;
977978
use mem;
979+
use util::ForType;
978980

979981
#[test]
980982
fn test_num() {
@@ -1246,9 +1248,8 @@ mod tests {
12461248

12471249
#[test]
12481250
fn test_primitive() {
1249-
let none: Option<f64> = None;
1250-
assert_eq!(Primitive::bits(none), mem::size_of::<f64>() * 8);
1251-
assert_eq!(Primitive::bytes(none), mem::size_of::<f64>());
1251+
assert_eq!(Primitive::bits(ForType::<f64>), mem::size_of::<f64>() * 8);
1252+
assert_eq!(Primitive::bytes(ForType::<f64>), mem::size_of::<f64>());
12521253
}
12531254

12541255
#[test]

src/libstd/num/int_macros.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ macro_rules! int_module (($T:ty, $bits:expr) => (mod generated {
2020
use default::Default;
2121
use num::{ToStrRadix, FromStrRadix};
2222
use num::{CheckedDiv, Zero, One, strconv};
23-
use prelude::*;
2423
use str;
24+
use util::ForType;
25+
use prelude::*;
2526

2627
pub use cmp::{min, max};
2728

@@ -377,13 +378,13 @@ impl Int for $T {}
377378

378379
impl Primitive for $T {
379380
#[inline]
380-
fn bits(_: Option<$T>) -> uint { bits }
381+
fn bits(_: ForType<$T>) -> uint { bits }
381382

382383
#[inline]
383-
fn bytes(_: Option<$T>) -> uint { bits / 8 }
384+
fn bytes(_: ForType<$T>) -> uint { bits / 8 }
384385

385386
#[inline]
386-
fn is_signed(_: Option<$T>) -> bool { true }
387+
fn is_signed(_: ForType<$T>) -> bool { true }
387388
}
388389

389390
// String conversion functions and impl str -> num
@@ -458,6 +459,7 @@ mod tests {
458459
use i32;
459460
use num;
460461
use mem;
462+
use util::ForType;
461463

462464
#[test]
463465
fn test_num() {
@@ -653,9 +655,8 @@ mod tests {
653655

654656
#[test]
655657
fn test_primitive() {
656-
let none: Option<$T> = None;
657-
assert_eq!(Primitive::bits(none), mem::size_of::<$T>() * 8);
658-
assert_eq!(Primitive::bytes(none), mem::size_of::<$T>());
658+
assert_eq!(Primitive::bits(ForType::<$T>), mem::size_of::<$T>() * 8);
659+
assert_eq!(Primitive::bytes(ForType::<$T>), mem::size_of::<$T>());
659660
}
660661

661662
#[test]

src/libstd/num/mod.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use cmp::{Eq, ApproxEq, Ord};
2020
use ops::{Add, Sub, Mul, Div, Rem, Neg};
2121
use ops::{Not, BitAnd, BitOr, BitXor, Shl, Shr};
2222
use option::{Option, Some, None};
23+
use util::ForType;
2324

2425
pub mod strconv;
2526

@@ -344,9 +345,9 @@ pub trait Primitive: Clone
344345
+ Rem<Self,Self> {
345346
// FIXME (#5527): These should be associated constants
346347
// FIXME (#8888): Removing `unused_self` requires #8888 to be fixed.
347-
fn bits(unused_self: Option<Self>) -> uint;
348-
fn bytes(unused_self: Option<Self>) -> uint;
349-
fn is_signed(unused_self: Option<Self>) -> bool;
348+
fn bits(_: ForType<Self>) -> uint;
349+
fn bytes(_: ForType<Self>) -> uint;
350+
fn is_signed(_: ForType<Self>) -> bool;
350351
}
351352

352353
/// A collection of traits relevant to primitive signed and unsigned integers
@@ -388,13 +389,13 @@ pub trait Float: Real
388389
fn classify(&self) -> FPCategory;
389390

390391
// FIXME (#8888): Removing `unused_self` requires #8888 to be fixed.
391-
fn mantissa_digits(unused_self: Option<Self>) -> uint;
392-
fn digits(unused_self: Option<Self>) -> uint;
392+
fn mantissa_digits(_: ForType<Self>) -> uint;
393+
fn digits(_: ForType<Self>) -> uint;
393394
fn epsilon() -> Self;
394-
fn min_exp(unused_self: Option<Self>) -> int;
395-
fn max_exp(unused_self: Option<Self>) -> int;
396-
fn min_10_exp(unused_self: Option<Self>) -> int;
397-
fn max_10_exp(unused_self: Option<Self>) -> int;
395+
fn min_exp(_: ForType<Self>) -> int;
396+
fn max_exp(_: ForType<Self>) -> int;
397+
fn min_10_exp(_: ForType<Self>) -> int;
398+
fn max_10_exp(_: ForType<Self>) -> int;
398399

399400
fn ldexp(x: Self, exp: int) -> Self;
400401
fn frexp(&self) -> (Self, int);
@@ -490,7 +491,7 @@ pub trait ToPrimitive {
490491
macro_rules! impl_to_primitive_int_to_int(
491492
($SrcT:ty, $DstT:ty) => (
492493
{
493-
if Primitive::bits(None::<$SrcT>) <= Primitive::bits(None::<$DstT>) {
494+
if Primitive::bits(ForType::<$SrcT>) <= Primitive::bits(ForType::<$DstT>) {
494495
Some(*self as $DstT)
495496
} else {
496497
let n = *self as i64;
@@ -575,7 +576,7 @@ macro_rules! impl_to_primitive_uint_to_int(
575576
macro_rules! impl_to_primitive_uint_to_uint(
576577
($SrcT:ty, $DstT:ty) => (
577578
{
578-
if Primitive::bits(None::<$SrcT>) <= Primitive::bits(None::<$DstT>) {
579+
if Primitive::bits(ForType::<$SrcT>) <= Primitive::bits(ForType::<$DstT>) {
579580
Some(*self as $DstT)
580581
} else {
581582
let zero: $SrcT = Zero::zero();
@@ -631,7 +632,7 @@ impl_to_primitive_uint!(u64)
631632

632633
macro_rules! impl_to_primitive_float_to_float(
633634
($SrcT:ty, $DstT:ty) => (
634-
if Primitive::bits(None::<$SrcT>) <= Primitive::bits(None::<$DstT>) {
635+
if Primitive::bits(ForType::<$SrcT>) <= Primitive::bits(ForType::<$DstT>) {
635636
Some(*self as $DstT)
636637
} else {
637638
let n = *self as f64;

src/libstd/num/uint_macros.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ use default::Default;
2121
use num::BitCount;
2222
use num::{ToStrRadix, FromStrRadix};
2323
use num::{CheckedDiv, Zero, One, strconv};
24-
use prelude::*;
2524
use str;
25+
use util::ForType;
26+
use prelude::*;
2627

2728
pub use cmp::{min, max};
2829

@@ -302,13 +303,13 @@ impl ToStrRadix for $T {
302303

303304
impl Primitive for $T {
304305
#[inline]
305-
fn bits(_: Option<$T>) -> uint { bits }
306+
fn bits(_: ForType<$T>) -> uint { bits }
306307

307308
#[inline]
308-
fn bytes(_: Option<$T>) -> uint { bits / 8 }
309+
fn bytes(_: ForType<$T>) -> uint { bits / 8 }
309310

310311
#[inline]
311-
fn is_signed(_: Option<$T>) -> bool { false }
312+
fn is_signed(_: ForType<$T>) -> bool { false }
312313
}
313314

314315
impl BitCount for $T {
@@ -339,6 +340,7 @@ mod tests {
339340
use num;
340341
use mem;
341342
use u16;
343+
use util::ForType;
342344

343345
#[test]
344346
fn test_num() {
@@ -430,9 +432,8 @@ mod tests {
430432

431433
#[test]
432434
fn test_primitive() {
433-
let none: Option<$T> = None;
434-
assert_eq!(Primitive::bits(none), mem::size_of::<$T>() * 8);
435-
assert_eq!(Primitive::bytes(none), mem::size_of::<$T>());
435+
assert_eq!(Primitive::bits(ForType::<$T>), mem::size_of::<$T>() * 8);
436+
assert_eq!(Primitive::bytes(ForType::<$T>), mem::size_of::<$T>());
436437
}
437438

438439
#[test]

src/libstd/util.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl Void {
9191
/// }
9292
///
9393
/// impl A for int {
94-
/// fn a(_: ForType<Self>) -> uint { 6 }
94+
/// fn a(_: ForType<int>) -> uint { 6 }
9595
/// }
9696
///
9797
/// assert_eq!(A::a(ForType::<int>), 6);
@@ -176,7 +176,7 @@ mod tests {
176176
}
177177

178178
impl A for int {
179-
fn a(_: ForType<Self>) -> uint { 6 }
179+
fn a(_: ForType<int>) -> uint { 6 }
180180
}
181181

182182
assert_eq!(A::a(ForType::<int>), 6);

0 commit comments

Comments
 (0)