Skip to content

Commit aa38867

Browse files
committed
auto merge of #6071 : bjz/rust/numeric-traits, r=graydon
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<Self> + BitAnd<Self,Self> + BitOr<Self,Self> + BitXor<Self,Self> + Shl<Self,Self> + Shr<Self,Self> {} 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 + Bounded + Neg<Self> + Add<Self,Self> + Sub<Self,Self> + Mul<Self,Self> + Quot<Self,Self> + Rem<Self,Self> { fn bits() -> uint; fn bytes() -> uint; } pub trait Int: Integer + Primitive + Bitwise + BitCount {} 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; fn mantissa_digits() -> uint; fn digits() -> uint; fn epsilon() -> Self; fn min_exp() -> int; fn max_exp() -> int; fn min_10_exp() -> int; fn max_10_exp() -> int; fn mul_add(&self, a: Self, b: Self) -> Self; fn next_after(&self, other: Self) -> Self; } ~~~ 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 primitive floating-point and integer methods, 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. I have also added an `Orderable` trait: ~~~rust pub trait Orderable: Ord { fn min(&self, other: &Self) -> Self; fn max(&self, other: &Self) -> Self; fn clamp(&self, mn: &Self, mx: &Self) -> Self; } ~~~ This is a temporary trait until we have default methods. We don't want to encumber all implementors of Ord by requiring them to implement these functions, but at the same time we want to be able to take advantage of the speed of the specific numeric functions (like the `fmin` and `fmax` intrinsics).
2 parents 5de74f3 + 9cdf402 commit aa38867

File tree

23 files changed

+1722
-848
lines changed

23 files changed

+1722
-848
lines changed

src/libcore/core.rc

+4-1
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,11 @@ pub use iter::{CopyableOrderedIter, CopyableNonstrictIter, Times};
104104
pub use iter::{ExtendedMutableIter};
105105

106106
pub use num::{Num, NumCast};
107-
pub use num::{Signed, Unsigned, Integer};
107+
pub use num::{Orderable, Signed, Unsigned, Integer};
108108
pub use num::{Round, Fractional, Real, RealExt};
109+
pub use num::{Bitwise, BitCount, Bounded};
110+
pub use num::{Primitive, Int, Float};
111+
109112
pub use ptr::Ptr;
110113
pub use to_str::ToStr;
111114
pub use clone::Clone;

src/libcore/num/cmath.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ pub mod c_double_utils {
4747
unsafe fn fmax(a: c_double, b: c_double) -> c_double;
4848
#[link_name="fmin"]
4949
unsafe fn fmin(a: c_double, b: c_double) -> c_double;
50-
unsafe fn nextafter(x: c_double, y: c_double) -> c_double;
50+
#[link_name="nextafter"]
51+
unsafe fn next_after(x: c_double, y: c_double) -> c_double;
5152
unsafe fn frexp(n: c_double, value: &mut c_int) -> c_double;
5253
unsafe fn hypot(x: c_double, y: c_double) -> c_double;
5354
unsafe fn ldexp(x: c_double, n: c_int) -> c_double;
@@ -131,7 +132,7 @@ pub mod c_float_utils {
131132
#[link_name="fminf"]
132133
unsafe fn fmin(a: c_float, b: c_float) -> c_float;
133134
#[link_name="nextafterf"]
134-
unsafe fn nextafter(x: c_float, y: c_float) -> c_float;
135+
unsafe fn next_after(x: c_float, y: c_float) -> c_float;
135136
#[link_name="hypotf"]
136137
unsafe fn hypot(x: c_float, y: c_float) -> c_float;
137138
#[link_name="ldexpf"]

0 commit comments

Comments
 (0)