mozilla/rust — pull request #6071 by bjz (2013-04-26 06:40:40+00:00)
Title: Add traits for primitive numeric types

As part of the numeric trait reform (see issue #4819), I have added the following traits to `core::num` and implemented them for Rust's primitive numeric types:

~~~rust
pub trait Bitwise: Not
+ BitAnd
+ BitOr
+ BitXor
+ Shl
+ Shr {}

pub trait BitCount {
fn population_count(&self) -> Self;
fn leading_zeros(&self) -> Self;
fn trailing_zeros(&self) -> Self;
}

pub trait Bounded {
fn min_value() -> Self;
fn max_value() -> Self;
}

pub trait Primitive: Num
+ NumCast
+ Neg
+ Add
+ Sub
+ Mul
+ Quot
+ Rem {
fn bits() -> uint;
fn bytes() -> uint;
}

pub trait PrimitiveInt: Integer
+ Primitive
+ Bounded
+ Bitwise
+ BitCount {}

pub trait Uint: PrimitiveInt
+ Unsigned {}

pub trait Int: PrimitiveInt
+ Signed {}

pub trait Float: Real
+ Signed
+ Primitive {
fn NaN() -> Self;
fn infinity() -> Self;
fn neg_infinity() -> Self;
fn neg_zero() -> Self;

fn is_NaN(&self) -> bool;
fn is_infinite(&self) -> bool;
fn is_finite(&self) -> bool;
}
~~~
Note: I'm not sure my implementation for `BitCount::trailing_zeros` and `BitCount::leading_zeros` is correct for uints. I also need some assistance creating appropriate unit tests for them.

More work needs to be done in implementing specialized floating-point and integer functions for primitive functions, but I'm beginning to reach the limits of my knowledge. Please leave your suggestions/critiques/ideas on #4819 if you have them – I'd very much appreciate hearing them.