auto merge of #11548 : bjz/rust/bitwise, r=alexcrichton

One less trait in `std::num` and three less exported in the prelude.

cc. #10387
This commit is contained in:
bors 2014-01-15 20:36:48 -08:00
commit 6708558c34
21 changed files with 45 additions and 42 deletions

View File

@ -83,8 +83,7 @@ syn keyword rustTrait OrdIterator MutableDoubleEndedIterator ExactSize
syn keyword rustTrait Times
syn keyword rustTrait Algebraic Trigonometric Exponential Hyperbolic
syn keyword rustTrait Bitwise BitCount Bounded
syn keyword rustTrait Integer Fractional Real RealExt
syn keyword rustTrait Bitwise Bounded Integer Fractional Real RealExt
syn keyword rustTrait Num NumCast CheckedAdd CheckedSub CheckedMul
syn keyword rustTrait Orderable Signed Unsigned Round
syn keyword rustTrait Primitive Int Float ToStrRadix ToPrimitive FromPrimitive

View File

@ -13,6 +13,8 @@
//! This module defines a container which uses an efficient bit mask
//! representation to hold C-like enum variants.
use std::num::Bitwise;
#[deriving(Clone, Eq, IterBytes, ToStr, Encodable, Decodable)]
/// A specialized Set implementation to use enum types.
pub struct EnumSet<E> {

View File

@ -22,7 +22,7 @@ A `BigInt` is a combination of `BigUint` and `Sign`.
use std::cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering, Less, Equal, Greater};
use std::num;
use std::num::{Zero, One, ToStrRadix, FromStrRadix, Orderable};
use std::num::{ToPrimitive, FromPrimitive};
use std::num::{Bitwise, ToPrimitive, FromPrimitive};
use std::rand::Rng;
use std::str;
use std::uint;

View File

@ -523,6 +523,7 @@ static H256: [u32, ..8] = [
#[cfg(test)]
mod tests {
use super::{Digest, Sha256, FixedBuffer};
use std::num::Bounded;
use std::vec;
use std::rand::isaac::IsaacRng;
use std::rand::Rng;

View File

@ -445,6 +445,7 @@ impl Digest for Sha1 {
#[cfg(test)]
mod tests {
use std::num::Bounded;
use std::rand::{IsaacRng, Rng};
use std::vec;
use extra::hex::FromHex;

View File

@ -17,7 +17,7 @@ use cmath;
use default::Default;
use libc::{c_float, c_int};
use num::{FPCategory, FPNaN, FPInfinite , FPZero, FPSubnormal, FPNormal};
use num::{Zero, One, strconv};
use num::{Zero, One, Bounded, strconv};
use num;
use to_str;
use unstable::intrinsics;

View File

@ -18,7 +18,7 @@ use cmath;
use default::Default;
use libc::{c_double, c_int};
use num::{FPCategory, FPNaN, FPInfinite , FPZero, FPSubnormal, FPNormal};
use num::{Zero, One, strconv};
use num::{Zero, One, Bounded, strconv};
use num;
use to_str;
use unstable::intrinsics;

View File

@ -15,7 +15,7 @@
use prelude::*;
use default::Default;
use num::{BitCount, CheckedAdd, CheckedSub, CheckedMul};
use num::{Bitwise, Bounded, CheckedAdd, CheckedSub, CheckedMul};
use num::{CheckedDiv, Zero, One, strconv};
use num::{ToStrRadix, FromStrRadix};
use option::{Option, Some, None};
@ -24,7 +24,7 @@ use unstable::intrinsics;
int_module!(i16, 16)
impl BitCount for i16 {
impl Bitwise for i16 {
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
#[inline]
fn population_count(&self) -> i16 { unsafe { intrinsics::ctpop16(*self) } }

View File

@ -15,7 +15,7 @@
use prelude::*;
use default::Default;
use num::{BitCount, CheckedAdd, CheckedSub, CheckedMul};
use num::{Bitwise, Bounded, CheckedAdd, CheckedSub, CheckedMul};
use num::{CheckedDiv, Zero, One, strconv};
use num::{ToStrRadix, FromStrRadix};
use option::{Option, Some, None};
@ -24,7 +24,7 @@ use unstable::intrinsics;
int_module!(i32, 32)
impl BitCount for i32 {
impl Bitwise for i32 {
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
#[inline]
fn population_count(&self) -> i32 { unsafe { intrinsics::ctpop32(*self) } }

View File

@ -17,7 +17,7 @@ use prelude::*;
use default::Default;
#[cfg(target_word_size = "64")]
use num::CheckedMul;
use num::{BitCount, CheckedAdd, CheckedSub};
use num::{Bitwise, Bounded, CheckedAdd, CheckedSub};
use num::{CheckedDiv, Zero, One, strconv};
use num::{ToStrRadix, FromStrRadix};
use option::{Option, Some, None};
@ -26,7 +26,7 @@ use unstable::intrinsics;
int_module!(i64, 64)
impl BitCount for i64 {
impl Bitwise for i64 {
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
#[inline]
fn population_count(&self) -> i64 { unsafe { intrinsics::ctpop64(*self) } }

View File

@ -15,7 +15,7 @@
use prelude::*;
use default::Default;
use num::{BitCount, CheckedAdd, CheckedSub, CheckedMul};
use num::{Bitwise, Bounded, CheckedAdd, CheckedSub, CheckedMul};
use num::{CheckedDiv, Zero, One, strconv};
use num::{ToStrRadix, FromStrRadix};
use option::{Option, Some, None};
@ -24,7 +24,7 @@ use unstable::intrinsics;
int_module!(i8, 8)
impl BitCount for i8 {
impl Bitwise for i8 {
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
#[inline]
fn population_count(&self) -> i8 { unsafe { intrinsics::ctpop8(*self) } }

View File

@ -15,7 +15,7 @@
use prelude::*;
use default::Default;
use num::{BitCount, CheckedAdd, CheckedSub, CheckedMul};
use num::{Bitwise, Bounded, CheckedAdd, CheckedSub, CheckedMul};
use num::{CheckedDiv, Zero, One, strconv};
use num::{ToStrRadix, FromStrRadix};
use option::{Option, Some, None};
@ -26,7 +26,7 @@ use unstable::intrinsics;
#[cfg(target_word_size = "64")] int_module!(int, 64)
#[cfg(target_word_size = "32")]
impl BitCount for int {
impl Bitwise for int {
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
#[inline]
fn population_count(&self) -> int { (*self as i32).population_count() as int }
@ -41,7 +41,7 @@ impl BitCount for int {
}
#[cfg(target_word_size = "64")]
impl BitCount for int {
impl Bitwise for int {
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
#[inline]
fn population_count(&self) -> int { (*self as i64).population_count() as int }

View File

@ -315,8 +315,6 @@ impl Integer for $T {
fn is_odd(&self) -> bool { !self.is_even() }
}
impl Bitwise for $T {}
#[cfg(not(test))]
impl BitOr<$T,$T> for $T {
#[inline]
@ -446,6 +444,7 @@ mod tests {
use i32;
use num;
use num::CheckedDiv;
use num::Bitwise;
use mem;
#[test]

View File

@ -324,21 +324,27 @@ pub trait Real: Signed
/// Inverse hyperbolic tangent function.
#[inline(always)] pub fn atanh<T: Real>(value: T) -> T { value.atanh() }
/// Collects the bitwise operators under one trait.
pub trait Bitwise: Not<Self>
pub trait Bounded {
// FIXME (#5527): These should be associated constants
fn min_value() -> Self;
fn max_value() -> Self;
}
/// Numbers with a fixed binary representation.
pub trait Bitwise: Bounded
+ Not<Self>
+ BitAnd<Self,Self>
+ BitOr<Self,Self>
+ BitXor<Self,Self>
+ Shl<Self,Self>
+ Shr<Self,Self> {}
/// A trait for common counting operations on bits.
pub trait BitCount {
+ Shr<Self,Self> {
/// Returns the number of bits set in the number.
///
/// # Example
///
/// ```rust
/// use std::num::Bitwise;
///
/// let n = 0b0101000u16;
/// assert_eq!(n.population_count(), 2);
/// ```
@ -348,6 +354,8 @@ pub trait BitCount {
/// # Example
///
/// ```rust
/// use std::num::Bitwise;
///
/// let n = 0b0101000u16;
/// assert_eq!(n.leading_zeros(), 10);
/// ```
@ -357,18 +365,14 @@ pub trait BitCount {
/// # Example
///
/// ```rust
/// use std::num::Bitwise;
///
/// let n = 0b0101000u16;
/// assert_eq!(n.trailing_zeros(), 3);
/// ```
fn trailing_zeros(&self) -> Self;
}
pub trait Bounded {
// FIXME (#5527): These should be associated constants
fn min_value() -> Self;
fn max_value() -> Self;
}
/// Specifies the available operations common to all of Rust's core numeric primitives.
/// These may not always make sense from a purely mathematical point of view, but
/// may be useful for systems programming.
@ -394,8 +398,7 @@ pub trait Primitive: Clone
/// A collection of traits relevant to primitive signed and unsigned integers
pub trait Int: Integer
+ Primitive
+ Bitwise
+ BitCount {}
+ Bitwise {}
/// Used for representing the classification of floating point numbers
#[deriving(Eq)]

View File

@ -15,7 +15,7 @@
use prelude::*;
use default::Default;
use num::BitCount;
use num::{Bitwise, Bounded};
use num::{CheckedAdd, CheckedSub, CheckedMul};
use num::{CheckedDiv, Zero, One, strconv};
use num::{ToStrRadix, FromStrRadix};

View File

@ -15,7 +15,7 @@
use prelude::*;
use default::Default;
use num::BitCount;
use num::{Bitwise, Bounded};
use num::{CheckedAdd, CheckedSub, CheckedMul};
use num::{CheckedDiv, Zero, One, strconv};
use num::{ToStrRadix, FromStrRadix};

View File

@ -15,7 +15,7 @@
use prelude::*;
use default::Default;
use num::BitCount;
use num::{Bitwise, Bounded};
#[cfg(target_word_size = "64")]
use num::CheckedMul;
use num::{CheckedAdd, CheckedSub};

View File

@ -15,7 +15,7 @@
use prelude::*;
use default::Default;
use num::BitCount;
use num::{Bitwise, Bounded};
use num::{CheckedAdd, CheckedSub, CheckedMul};
use num::{CheckedDiv, Zero, One, strconv};
use num::{ToStrRadix, FromStrRadix};

View File

@ -16,7 +16,7 @@ use prelude::*;
use default::Default;
use mem;
use num::BitCount;
use num::{Bitwise, Bounded};
use num::{CheckedAdd, CheckedSub, CheckedMul};
use num::{CheckedDiv, Zero, One, strconv};
use num::{ToStrRadix, FromStrRadix};

View File

@ -176,8 +176,6 @@ impl Integer for $T {
fn is_odd(&self) -> bool { !self.is_even() }
}
impl Bitwise for $T {}
#[cfg(not(test))]
impl BitOr<$T,$T> for $T {
#[inline]
@ -298,7 +296,7 @@ impl Primitive for $T {
fn is_signed(_: Option<$T>) -> bool { false }
}
impl BitCount for $T {
impl Bitwise for $T {
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
#[inline]
fn population_count(&self) -> $T {
@ -325,6 +323,7 @@ mod tests {
use num;
use num::CheckedDiv;
use num::Bitwise;
use mem;
use u16;

View File

@ -58,8 +58,7 @@ pub use iter::{FromIterator, Extendable};
pub use iter::{Iterator, DoubleEndedIterator, RandomAccessIterator, CloneableIterator};
pub use iter::{OrdIterator, MutableDoubleEndedIterator, ExactSize};
pub use num::Times;
pub use num::{Bitwise, BitCount, Bounded, Integer, Real};
pub use num::{Num, NumCast, CheckedAdd, CheckedSub, CheckedMul};
pub use num::{Integer, Real, Num, NumCast, CheckedAdd, CheckedSub, CheckedMul};
pub use num::{Orderable, Signed, Unsigned, Round};
pub use num::{Primitive, Int, Float, ToStrRadix, ToPrimitive, FromPrimitive};
pub use path::{GenericPath, Path, PosixPath, WindowsPath};