Skip to content

Commit 472dfe7

Browse files
committed
Simplify std::num::Primitive trait definition
This removes the `Primitive::{bits, bytes, is_signed}` methods and removes the 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 probably be deprecated in the future in favor of using the `std::mem::size_of` function (see rust-lang#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 rust-lang#8888. - The operator trait constraints are already covered by the `Num` trait.
1 parent 80a3f45 commit 472dfe7

File tree

5 files changed

+9
-84
lines changed

5 files changed

+9
-84
lines changed

src/libstd/num/f32.rs

+1-17
Original file line numberDiff line numberDiff line change
@@ -554,16 +554,7 @@ impl Bounded for f32 {
554554
fn max_value() -> f32 { 3.40282347e+38 }
555555
}
556556

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-
}
557+
impl Primitive for f32 {}
567558

568559
impl Float for f32 {
569560
#[inline]
@@ -1173,13 +1164,6 @@ mod tests {
11731164
assert!(!NAN.is_negative());
11741165
}
11751166

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-
11831167
#[test]
11841168
fn test_is_normal() {
11851169
let nan: f32 = Float::nan();

src/libstd/num/f64.rs

+1-17
Original file line numberDiff line numberDiff line change
@@ -556,16 +556,7 @@ impl Bounded for f64 {
556556
fn max_value() -> f64 { 1.7976931348623157e+308 }
557557
}
558558

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-
}
559+
impl Primitive for f64 {}
569560

570561
impl Float for f64 {
571562
#[inline]
@@ -1178,13 +1169,6 @@ mod tests {
11781169
assert!(!NAN.is_negative());
11791170
}
11801171

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-
11881172
#[test]
11891173
fn test_is_normal() {
11901174
let nan: f64 = Float::nan();

src/libstd/num/int_macros.rs

+1-17
Original file line numberDiff line numberDiff line change
@@ -361,16 +361,7 @@ impl Bounded for $T {
361361

362362
impl Int for $T {}
363363

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-
}
364+
impl Primitive for $T {}
374365

375366
// String conversion functions and impl str -> num
376367

@@ -639,13 +630,6 @@ mod tests {
639630
assert_eq!((0b010101 as $T).population_count(), 3);
640631
}
641632

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-
649633
#[test]
650634
fn test_from_str() {
651635
assert_eq!(from_str::<$T>("0"), Some(0 as $T));

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};
@@ -381,19 +382,7 @@ pub trait Primitive: Clone
381382
+ Num
382383
+ NumCast
383384
+ Orderable
384-
+ Bounded
385-
+ Neg<Self>
386-
+ Add<Self,Self>
387-
+ Sub<Self,Self>
388-
+ Mul<Self,Self>
389-
+ Div<Self,Self>
390-
+ Rem<Self,Self> {
391-
// FIXME (#5527): These should be associated constants
392-
// FIXME (#8888): Removing `unused_self` requires #8888 to be fixed.
393-
fn bits(unused_self: Option<Self>) -> uint;
394-
fn bytes(unused_self: Option<Self>) -> uint;
395-
fn is_signed(unused_self: Option<Self>) -> bool;
396-
}
385+
+ Bounded {}
397386

398387
/// A collection of traits relevant to primitive signed and unsigned integers
399388
pub trait Int: Integer
@@ -536,7 +525,7 @@ pub trait ToPrimitive {
536525
macro_rules! impl_to_primitive_int_to_int(
537526
($SrcT:ty, $DstT:ty) => (
538527
{
539-
if Primitive::bits(None::<$SrcT>) <= Primitive::bits(None::<$DstT>) {
528+
if size_of::<$SrcT>() <= size_of::<$DstT>() {
540529
Some(*self as $DstT)
541530
} else {
542531
let n = *self as i64;
@@ -621,7 +610,7 @@ macro_rules! impl_to_primitive_uint_to_int(
621610
macro_rules! impl_to_primitive_uint_to_uint(
622611
($SrcT:ty, $DstT:ty) => (
623612
{
624-
if Primitive::bits(None::<$SrcT>) <= Primitive::bits(None::<$DstT>) {
613+
if size_of::<$SrcT>() <= size_of::<$DstT>() {
625614
Some(*self as $DstT)
626615
} else {
627616
let zero: $SrcT = Zero::zero();
@@ -677,7 +666,7 @@ impl_to_primitive_uint!(u64)
677666

678667
macro_rules! impl_to_primitive_float_to_float(
679668
($SrcT:ty, $DstT:ty) => (
680-
if Primitive::bits(None::<$SrcT>) <= Primitive::bits(None::<$DstT>) {
669+
if size_of::<$SrcT>() <= size_of::<$DstT>() {
681670
Some(*self as $DstT)
682671
} else {
683672
let n = *self as f64;

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)