Skip to content

Commit

Permalink
auto merge of #11548 : bjz/rust/bitwise, r=alexcrichton
Browse files Browse the repository at this point in the history
One less trait in `std::num` and three less exported in the prelude.

cc. #10387
  • Loading branch information
bors committed Jan 16, 2014
2 parents bf2ab22 + 1dd6906 commit 6708558
Show file tree
Hide file tree
Showing 21 changed files with 45 additions and 42 deletions.
3 changes: 1 addition & 2 deletions src/etc/vim/syntax/rust.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/libextra/enum_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
2 changes: 1 addition & 1 deletion src/libextra/num/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/librustc/util/sha2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/librustpkg/sha1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/num/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/num/i16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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) } }
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/num/i32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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) } }
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/num/i64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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) } }
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/num/i8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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) } }
Expand Down
6 changes: 3 additions & 3 deletions src/libstd/num/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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 }
Expand All @@ -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 }
Expand Down
3 changes: 1 addition & 2 deletions src/libstd/num/int_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -446,6 +444,7 @@ mod tests {
use i32;
use num;
use num::CheckedDiv;
use num::Bitwise;
use mem;

#[test]
Expand Down
31 changes: 17 additions & 14 deletions src/libstd/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
/// ```
Expand All @@ -348,6 +354,8 @@ pub trait BitCount {
/// # Example
///
/// ```rust
/// use std::num::Bitwise;
///
/// let n = 0b0101000u16;
/// assert_eq!(n.leading_zeros(), 10);
/// ```
Expand All @@ -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.
Expand All @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/num/u16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/num/u32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/num/u64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/num/u8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/num/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
5 changes: 2 additions & 3 deletions src/libstd/num/uint_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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 {
Expand All @@ -325,6 +323,7 @@ mod tests {

use num;
use num::CheckedDiv;
use num::Bitwise;
use mem;
use u16;

Expand Down
3 changes: 1 addition & 2 deletions src/libstd/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down

0 comments on commit 6708558

Please sign in to comment.