Skip to content

Commit

Permalink
Simplify std::num::Primitive trait definition
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
brendanzab authored and tweber12 committed Jan 18, 2014
1 parent abf2600 commit b7614b7
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 84 deletions.
18 changes: 1 addition & 17 deletions src/libstd/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,16 +554,7 @@ impl Bounded for f32 {
fn max_value() -> f32 { 3.40282347e+38 }
}

impl Primitive for f32 {
#[inline]
fn bits(_: Option<f32>) -> uint { 32 }

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

#[inline]
fn is_signed(_: Option<f32>) -> bool { true }
}
impl Primitive for f32 {}

impl Float for f32 {
#[inline]
Expand Down Expand Up @@ -1173,13 +1164,6 @@ mod tests {
assert!(!NAN.is_negative());
}

#[test]
fn test_primitive() {
let none: Option<f32> = None;
assert_eq!(Primitive::bits(none), mem::size_of::<f32>() * 8);
assert_eq!(Primitive::bytes(none), mem::size_of::<f32>());
}

#[test]
fn test_is_normal() {
let nan: f32 = Float::nan();
Expand Down
18 changes: 1 addition & 17 deletions src/libstd/num/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,16 +556,7 @@ impl Bounded for f64 {
fn max_value() -> f64 { 1.7976931348623157e+308 }
}

impl Primitive for f64 {
#[inline]
fn bits(_: Option<f64>) -> uint { 64 }

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

#[inline]
fn is_signed(_: Option<f64>) -> bool { true }
}
impl Primitive for f64 {}

impl Float for f64 {
#[inline]
Expand Down Expand Up @@ -1178,13 +1169,6 @@ mod tests {
assert!(!NAN.is_negative());
}

#[test]
fn test_primitive() {
let none: Option<f64> = None;
assert_eq!(Primitive::bits(none), mem::size_of::<f64>() * 8);
assert_eq!(Primitive::bytes(none), mem::size_of::<f64>());
}

#[test]
fn test_is_normal() {
let nan: f64 = Float::nan();
Expand Down
18 changes: 1 addition & 17 deletions src/libstd/num/int_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,16 +361,7 @@ impl Bounded for $T {

impl Int for $T {}

impl Primitive for $T {
#[inline]
fn bits(_: Option<$T>) -> uint { bits }

#[inline]
fn bytes(_: Option<$T>) -> uint { bits / 8 }

#[inline]
fn is_signed(_: Option<$T>) -> bool { true }
}
impl Primitive for $T {}

// String conversion functions and impl str -> num

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

#[test]
fn test_primitive() {
let none: Option<$T> = None;
assert_eq!(Primitive::bits(none), mem::size_of::<$T>() * 8);
assert_eq!(Primitive::bytes(none), mem::size_of::<$T>());
}

#[test]
fn test_from_str() {
assert_eq!(from_str::<$T>("0"), Some(0 as $T));
Expand Down
21 changes: 5 additions & 16 deletions src/libstd/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

use clone::{Clone, DeepClone};
use cmp::{Eq, Ord};
use mem::size_of;
use ops::{Add, Sub, Mul, Div, Rem, Neg};
use ops::{Not, BitAnd, BitOr, BitXor, Shl, Shr};
use option::{Option, Some, None};
Expand Down Expand Up @@ -425,19 +426,7 @@ pub trait Primitive: Clone
+ Num
+ NumCast
+ Orderable
+ Bounded
+ Neg<Self>
+ Add<Self,Self>
+ Sub<Self,Self>
+ Mul<Self,Self>
+ Div<Self,Self>
+ Rem<Self,Self> {
// FIXME (#5527): These should be associated constants
// FIXME (#8888): Removing `unused_self` requires #8888 to be fixed.
fn bits(unused_self: Option<Self>) -> uint;
fn bytes(unused_self: Option<Self>) -> uint;
fn is_signed(unused_self: Option<Self>) -> bool;
}
+ Bounded {}

/// A collection of traits relevant to primitive signed and unsigned integers
pub trait Int: Integer
Expand Down Expand Up @@ -580,7 +569,7 @@ pub trait ToPrimitive {
macro_rules! impl_to_primitive_int_to_int(
($SrcT:ty, $DstT:ty) => (
{
if Primitive::bits(None::<$SrcT>) <= Primitive::bits(None::<$DstT>) {
if size_of::<$SrcT>() <= size_of::<$DstT>() {
Some(*self as $DstT)
} else {
let n = *self as i64;
Expand Down Expand Up @@ -665,7 +654,7 @@ macro_rules! impl_to_primitive_uint_to_int(
macro_rules! impl_to_primitive_uint_to_uint(
($SrcT:ty, $DstT:ty) => (
{
if Primitive::bits(None::<$SrcT>) <= Primitive::bits(None::<$DstT>) {
if size_of::<$SrcT>() <= size_of::<$DstT>() {
Some(*self as $DstT)
} else {
let zero: $SrcT = Zero::zero();
Expand Down Expand Up @@ -721,7 +710,7 @@ impl_to_primitive_uint!(u64)

macro_rules! impl_to_primitive_float_to_float(
($SrcT:ty, $DstT:ty) => (
if Primitive::bits(None::<$SrcT>) <= Primitive::bits(None::<$DstT>) {
if size_of::<$SrcT>() <= size_of::<$DstT>() {
Some(*self as $DstT)
} else {
let n = *self as f64;
Expand Down
18 changes: 1 addition & 17 deletions src/libstd/num/uint_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,16 +285,7 @@ impl ToStrRadix for $T {
}
}

impl Primitive for $T {
#[inline]
fn bits(_: Option<$T>) -> uint { bits }

#[inline]
fn bytes(_: Option<$T>) -> uint { bits / 8 }

#[inline]
fn is_signed(_: Option<$T>) -> bool { false }
}
impl Primitive for $T {}

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

#[test]
fn test_primitive() {
let none: Option<$T> = None;
assert_eq!(Primitive::bits(none), mem::size_of::<$T>() * 8);
assert_eq!(Primitive::bytes(none), mem::size_of::<$T>());
}

#[test]
pub fn test_to_str() {
assert_eq!((0 as $T).to_str_radix(10u), ~"0");
Expand Down

0 comments on commit b7614b7

Please sign in to comment.