Skip to content

Commit 2952685

Browse files
committed
auto merge of #11622 : bjz/rust/simplify-primitive-trait, r=brson
As part of #10387, this removes the `Primitive::{bits, bytes, is_signed}` methods and removes the trait's operator trait constraints for the reasons outlined below: - The `Primitive::{bits, bytes}` associated functions were originally added to reflect the existing `BITS` and `BYTES`statics included in the numeric modules. These statics are only exist as a workaround for Rust's lack of CTFE, and should be deprecated in the future in favor of using the `std::mem::size_of` function (see #11621). - `Primitive::is_signed` seems to be of little utility and does not seem to be used anywhere in the Rust compiler or libraries. It is also rather ugly to call due to the `Option<Self>` workaround for #8888. - The operator trait constraints are already covered by the `Num` trait.
2 parents fb40bdb + f125b71 commit 2952685

File tree

5 files changed

+31
-84
lines changed

5 files changed

+31
-84
lines changed

Diff for: src/libstd/num/f32.rs

+8-17
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ delegate!(
9696
fn tanh(n: c_float) -> c_float = cmath::c_float::tanh
9797
)
9898

99+
// FIXME(#11621): These constants should be deprecated once CTFE is implemented
100+
// in favour of calling their respective functions in `Bounded` and `Float`.
101+
99102
pub static RADIX: uint = 2u;
100103

101104
pub static MANTISSA_DIGITS: uint = 53u;
@@ -122,6 +125,10 @@ pub static NEG_INFINITY: f32 = -1.0_f32/0.0_f32;
122125
pub mod consts {
123126
// FIXME (requires Issue #1433 to fix): replace with mathematical
124127
// staticants from cmath.
128+
129+
// FIXME(#11621): These constants should be deprecated once CTFE is
130+
// implemented in favour of calling their respective functions in `Real`.
131+
125132
/// Archimedes' constant
126133
pub static PI: f32 = 3.14159265358979323846264338327950288_f32;
127134

@@ -554,16 +561,7 @@ impl Bounded for f32 {
554561
fn max_value() -> f32 { 3.40282347e+38 }
555562
}
556563

557-
impl Primitive for f32 {
558-
#[inline]
559-
fn bits(_: Option<f32>) -> uint { 32 }
560-
561-
#[inline]
562-
fn bytes(_: Option<f32>) -> uint { Primitive::bits(Some(0f32)) / 8 }
563-
564-
#[inline]
565-
fn is_signed(_: Option<f32>) -> bool { true }
566-
}
564+
impl Primitive for f32 {}
567565

568566
impl Float for f32 {
569567
#[inline]
@@ -1173,13 +1171,6 @@ mod tests {
11731171
assert!(!NAN.is_negative());
11741172
}
11751173

1176-
#[test]
1177-
fn test_primitive() {
1178-
let none: Option<f32> = None;
1179-
assert_eq!(Primitive::bits(none), mem::size_of::<f32>() * 8);
1180-
assert_eq!(Primitive::bytes(none), mem::size_of::<f32>());
1181-
}
1182-
11831174
#[test]
11841175
fn test_is_normal() {
11851176
let nan: f32 = Float::nan();

Diff for: src/libstd/num/f64.rs

+8-17
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ delegate!(
101101

102102
// FIXME (#1433): obtain these in a different way
103103

104+
// FIXME(#11621): These constants should be deprecated once CTFE is implemented
105+
// in favour of calling their respective functions in `Bounded` and `Float`.
106+
104107
pub static RADIX: uint = 2u;
105108

106109
pub static MANTISSA_DIGITS: uint = 53u;
@@ -129,6 +132,10 @@ pub static NEG_INFINITY: f64 = -1.0_f64/0.0_f64;
129132
pub mod consts {
130133
// FIXME (requires Issue #1433 to fix): replace with mathematical
131134
// constants from cmath.
135+
136+
// FIXME(#11621): These constants should be deprecated once CTFE is
137+
// implemented in favour of calling their respective functions in `Real`.
138+
132139
/// Archimedes' constant
133140
pub static PI: f64 = 3.14159265358979323846264338327950288_f64;
134141

@@ -556,16 +563,7 @@ impl Bounded for f64 {
556563
fn max_value() -> f64 { 1.7976931348623157e+308 }
557564
}
558565

559-
impl Primitive for f64 {
560-
#[inline]
561-
fn bits(_: Option<f64>) -> uint { 64 }
562-
563-
#[inline]
564-
fn bytes(_: Option<f64>) -> uint { Primitive::bits(Some(0f64)) / 8 }
565-
566-
#[inline]
567-
fn is_signed(_: Option<f64>) -> bool { true }
568-
}
566+
impl Primitive for f64 {}
569567

570568
impl Float for f64 {
571569
#[inline]
@@ -1178,13 +1176,6 @@ mod tests {
11781176
assert!(!NAN.is_negative());
11791177
}
11801178

1181-
#[test]
1182-
fn test_primitive() {
1183-
let none: Option<f64> = None;
1184-
assert_eq!(Primitive::bits(none), mem::size_of::<f64>() * 8);
1185-
assert_eq!(Primitive::bytes(none), mem::size_of::<f64>());
1186-
}
1187-
11881179
#[test]
11891180
fn test_is_normal() {
11901181
let nan: f64 = Float::nan();

Diff for: src/libstd/num/int_macros.rs

+9-17
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,19 @@
1313

1414
macro_rules! int_module (($T:ty, $bits:expr) => (
1515

16+
// FIXME(#11621): Should be deprecated once CTFE is implemented in favour of
17+
// calling the `mem::size_of` function.
1618
pub static bits : uint = $bits;
19+
// FIXME(#11621): Should be deprecated once CTFE is implemented in favour of
20+
// calling the `mem::size_of` function.
1721
pub static bytes : uint = ($bits / 8);
1822

23+
// FIXME(#11621): Should be deprecated once CTFE is implemented in favour of
24+
// calling the `Bounded::min_value` function.
1925
pub static min_value: $T = (-1 as $T) << (bits - 1);
2026
// FIXME(#9837): Compute min_value like this so the high bits that shouldn't exist are 0.
27+
// FIXME(#11621): Should be deprecated once CTFE is implemented in favour of
28+
// calling the `Bounded::max_value` function.
2129
pub static max_value: $T = !min_value;
2230

2331
impl CheckedDiv for $T {
@@ -361,16 +369,7 @@ impl Bounded for $T {
361369

362370
impl Int for $T {}
363371

364-
impl Primitive for $T {
365-
#[inline]
366-
fn bits(_: Option<$T>) -> uint { bits }
367-
368-
#[inline]
369-
fn bytes(_: Option<$T>) -> uint { bits / 8 }
370-
371-
#[inline]
372-
fn is_signed(_: Option<$T>) -> bool { true }
373-
}
372+
impl Primitive for $T {}
374373

375374
// String conversion functions and impl str -> num
376375

@@ -639,13 +638,6 @@ mod tests {
639638
assert_eq!((0b010101 as $T).population_count(), 3);
640639
}
641640

642-
#[test]
643-
fn test_primitive() {
644-
let none: Option<$T> = None;
645-
assert_eq!(Primitive::bits(none), mem::size_of::<$T>() * 8);
646-
assert_eq!(Primitive::bytes(none), mem::size_of::<$T>());
647-
}
648-
649641
#[test]
650642
fn test_from_str() {
651643
assert_eq!(from_str::<$T>("0"), Some(0 as $T));

Diff for: src/libstd/num/mod.rs

+5-16
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
use clone::{Clone, DeepClone};
1919
use cmp::{Eq, Ord};
20+
use mem::size_of;
2021
use ops::{Add, Sub, Mul, Div, Rem, Neg};
2122
use ops::{Not, BitAnd, BitOr, BitXor, Shl, Shr};
2223
use option::{Option, Some, None};
@@ -425,19 +426,7 @@ pub trait Primitive: Clone
425426
+ Num
426427
+ NumCast
427428
+ Orderable
428-
+ Bounded
429-
+ Neg<Self>
430-
+ Add<Self,Self>
431-
+ Sub<Self,Self>
432-
+ Mul<Self,Self>
433-
+ Div<Self,Self>
434-
+ Rem<Self,Self> {
435-
// FIXME (#5527): These should be associated constants
436-
// FIXME (#8888): Removing `unused_self` requires #8888 to be fixed.
437-
fn bits(unused_self: Option<Self>) -> uint;
438-
fn bytes(unused_self: Option<Self>) -> uint;
439-
fn is_signed(unused_self: Option<Self>) -> bool;
440-
}
429+
+ Bounded {}
441430

442431
/// A collection of traits relevant to primitive signed and unsigned integers
443432
pub trait Int: Integer
@@ -580,7 +569,7 @@ pub trait ToPrimitive {
580569
macro_rules! impl_to_primitive_int_to_int(
581570
($SrcT:ty, $DstT:ty) => (
582571
{
583-
if Primitive::bits(None::<$SrcT>) <= Primitive::bits(None::<$DstT>) {
572+
if size_of::<$SrcT>() <= size_of::<$DstT>() {
584573
Some(*self as $DstT)
585574
} else {
586575
let n = *self as i64;
@@ -665,7 +654,7 @@ macro_rules! impl_to_primitive_uint_to_int(
665654
macro_rules! impl_to_primitive_uint_to_uint(
666655
($SrcT:ty, $DstT:ty) => (
667656
{
668-
if Primitive::bits(None::<$SrcT>) <= Primitive::bits(None::<$DstT>) {
657+
if size_of::<$SrcT>() <= size_of::<$DstT>() {
669658
Some(*self as $DstT)
670659
} else {
671660
let zero: $SrcT = Zero::zero();
@@ -721,7 +710,7 @@ impl_to_primitive_uint!(u64)
721710

722711
macro_rules! impl_to_primitive_float_to_float(
723712
($SrcT:ty, $DstT:ty) => (
724-
if Primitive::bits(None::<$SrcT>) <= Primitive::bits(None::<$DstT>) {
713+
if size_of::<$SrcT>() <= size_of::<$DstT>() {
725714
Some(*self as $DstT)
726715
} else {
727716
let n = *self as f64;

Diff for: src/libstd/num/uint_macros.rs

+1-17
Original file line numberDiff line numberDiff line change
@@ -285,16 +285,7 @@ impl ToStrRadix for $T {
285285
}
286286
}
287287

288-
impl Primitive for $T {
289-
#[inline]
290-
fn bits(_: Option<$T>) -> uint { bits }
291-
292-
#[inline]
293-
fn bytes(_: Option<$T>) -> uint { bits / 8 }
294-
295-
#[inline]
296-
fn is_signed(_: Option<$T>) -> bool { false }
297-
}
288+
impl Primitive for $T {}
298289

299290
impl Bitwise for $T {
300291
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
@@ -415,13 +406,6 @@ mod tests {
415406
assert_eq!((0b010101 as $T).population_count(), 3);
416407
}
417408

418-
#[test]
419-
fn test_primitive() {
420-
let none: Option<$T> = None;
421-
assert_eq!(Primitive::bits(none), mem::size_of::<$T>() * 8);
422-
assert_eq!(Primitive::bytes(none), mem::size_of::<$T>());
423-
}
424-
425409
#[test]
426410
pub fn test_to_str() {
427411
assert_eq!((0 as $T).to_str_radix(10u), ~"0");

0 commit comments

Comments
 (0)