Skip to content

Commit d6d9e47

Browse files
committed
---
yaml --- r: 57206 b: refs/heads/try c: b624210 h: refs/heads/master v: v3
1 parent fd1eba8 commit d6d9e47

File tree

19 files changed

+259
-1
lines changed

19 files changed

+259
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
refs/heads/master: c081ffbd1e845687202a975ea2e698b623e5722f
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 79a2b2eafc3c766cecec8a5f76317693bae9ed17
5-
refs/heads/try: f40be999cab20ee51758620d9ccf435c62fcb51f
5+
refs/heads/try: b62421000c8e0e9b08590b81af03e05c6edd3c10
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c

branches/try/src/libcore/core.rc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ pub use iter::{ExtendedMutableIter};
106106
pub use num::{Num, NumCast};
107107
pub use num::{Signed, Unsigned, Integer};
108108
pub use num::{Round, Fractional, Real, RealExt};
109+
pub use num::{Bitwise, Bounded};
110+
pub use num::{Primitive, PrimitiveInt};
109111
pub use ptr::Ptr;
110112
pub use to_str::ToStr;
111113
pub use clone::Clone;

branches/try/src/libcore/num/f32.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,14 @@ impl Real for f32 {
509509
fn tanh(&self) -> f32 { tanh(*self) }
510510
}
511511

512+
impl Primitive for f32 {
513+
#[inline(always)]
514+
fn bits() -> uint { 32 }
515+
516+
#[inline(always)]
517+
fn bytes() -> uint { Primitive::bits::<f32>() / 8 }
518+
}
519+
512520
//
513521
// Section: String Conversions
514522
//
@@ -873,6 +881,12 @@ mod tests {
873881
assert!((1f32/neg_infinity).is_negative());
874882
assert!(!NaN.is_negative());
875883
}
884+
885+
#[test]
886+
fn test_primitive() {
887+
assert_eq!(Primitive::bits::<f32>(), sys::size_of::<f32>() * 8);
888+
assert_eq!(Primitive::bytes::<f32>(), sys::size_of::<f32>());
889+
}
876890
}
877891

878892
//

branches/try/src/libcore/num/f64.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,14 @@ impl RealExt for f64 {
549549
fn yn(&self, n: int) -> f64 { yn(n as c_int, *self) }
550550
}
551551

552+
impl Primitive for f64 {
553+
#[inline(always)]
554+
fn bits() -> uint { 64 }
555+
556+
#[inline(always)]
557+
fn bytes() -> uint { Primitive::bits::<f64>() / 8 }
558+
}
559+
552560
//
553561
// Section: String Conversions
554562
//
@@ -914,6 +922,12 @@ mod tests {
914922
assert!((1f64/neg_infinity).is_negative());
915923
assert!(!NaN.is_negative());
916924
}
925+
926+
#[test]
927+
fn test_primitive() {
928+
assert_eq!(Primitive::bits::<f64>(), sys::size_of::<f64>() * 8);
929+
assert_eq!(Primitive::bytes::<f64>(), sys::size_of::<f64>());
930+
}
917931
}
918932

919933
//

branches/try/src/libcore/num/float.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,14 @@ impl Signed for float {
689689
fn is_negative(&self) -> bool { *self < 0.0 || (1.0 / *self) == neg_infinity }
690690
}
691691
692+
impl Primitive for float {
693+
#[inline(always)]
694+
fn bits() -> uint { Primitive::bits::<f64>() }
695+
696+
#[inline(always)]
697+
fn bytes() -> uint { Primitive::bytes::<f64>() }
698+
}
699+
692700
#[cfg(test)]
693701
mod tests {
694702
use super::*;
@@ -836,6 +844,12 @@ mod tests {
836844
assert!(!NaN.is_negative());
837845
}
838846
847+
#[test]
848+
fn test_primitive() {
849+
assert_eq!(Primitive::bits::<float>(), sys::size_of::<float>() * 8);
850+
assert_eq!(Primitive::bytes::<float>(), sys::size_of::<float>());
851+
}
852+
839853
#[test]
840854
pub fn test_to_str_exact_do_decimal() {
841855
let s = to_str_exact(5.0, 4u);

branches/try/src/libcore/num/int-template.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,8 @@ impl Integer for T {
399399
fn is_odd(&self) -> bool { !self.is_even() }
400400
}
401401
402+
impl Bitwise for T {}
403+
402404
#[cfg(notest)]
403405
impl BitOr<T,T> for T {
404406
#[inline(always)]
@@ -435,6 +437,16 @@ impl Not<T> for T {
435437
fn not(&self) -> T { !*self }
436438
}
437439
440+
impl Bounded for T {
441+
#[inline(always)]
442+
fn min_value() -> T { min_value }
443+
444+
#[inline(always)]
445+
fn max_value() -> T { max_value }
446+
}
447+
448+
impl PrimitiveInt for T {}
449+
438450
// String conversion functions and impl str -> num
439451
440452
/// Parse a string as a number in base 10.
@@ -641,6 +653,12 @@ mod tests {
641653
assert_eq!(-(0b11 as T) - (1 as T), (0b11 as T).not());
642654
}
643655
656+
#[test]
657+
fn test_primitive() {
658+
assert_eq!(Primitive::bits::<T>(), sys::size_of::<T>() * 8);
659+
assert_eq!(Primitive::bytes::<T>(), sys::size_of::<T>());
660+
}
661+
644662
#[test]
645663
fn test_from_str() {
646664
assert_eq!(from_str(~"0"), Some(0 as T));

branches/try/src/libcore/num/int-template/i16.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111
//! Operations and constants for `i16`
1212
1313
mod inst {
14+
use num::Primitive;
15+
1416
pub type T = i16;
1517
pub static bits: uint = ::u16::bits;
18+
19+
impl Primitive for i16 {
20+
#[inline(always)]
21+
fn bits() -> uint { 16 }
22+
23+
#[inline(always)]
24+
fn bytes() -> uint { Primitive::bits::<i16>() / 8 }
25+
}
1626
}

branches/try/src/libcore/num/int-template/i32.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111
//! Operations and constants for `i32`
1212
1313
mod inst {
14+
use num::Primitive;
15+
1416
pub type T = i32;
1517
pub static bits: uint = ::u32::bits;
18+
19+
impl Primitive for i32 {
20+
#[inline(always)]
21+
fn bits() -> uint { 32 }
22+
23+
#[inline(always)]
24+
fn bytes() -> uint { Primitive::bits::<i32>() / 8 }
25+
}
1626
}

branches/try/src/libcore/num/int-template/i64.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111
//! Operations and constants for `i64`
1212
1313
mod inst {
14+
use num::Primitive;
15+
1416
pub type T = i64;
1517
pub static bits: uint = ::u64::bits;
18+
19+
impl Primitive for i64 {
20+
#[inline(always)]
21+
fn bits() -> uint { 64 }
22+
23+
#[inline(always)]
24+
fn bytes() -> uint { Primitive::bits::<i64>() / 8 }
25+
}
1626
}

branches/try/src/libcore/num/int-template/i8.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111
//! Operations and constants for `i8`
1212
1313
mod inst {
14+
use num::Primitive;
15+
1416
pub type T = i8;
1517
pub static bits: uint = ::u8::bits;
18+
19+
impl Primitive for i8 {
20+
#[inline(always)]
21+
fn bits() -> uint { 8 }
22+
23+
#[inline(always)]
24+
fn bytes() -> uint { Primitive::bits::<i8>() / 8 }
25+
}
1626
}

branches/try/src/libcore/num/int-template/int.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,30 @@
1313
pub use self::inst::pow;
1414

1515
mod inst {
16+
use num::Primitive;
17+
1618
pub type T = int;
1719
pub static bits: uint = ::uint::bits;
1820

21+
impl Primitive for int {
22+
#[cfg(target_word_size = "32")]
23+
#[inline(always)]
24+
fn bits() -> uint { 32 }
25+
26+
#[cfg(target_word_size = "64")]
27+
#[inline(always)]
28+
fn bits() -> uint { 64 }
29+
30+
// fallback if we don't have access to the current word size
31+
#[cfg(not(target_word_size = "32"),
32+
not(target_word_size = "64"))]
33+
#[inline(always)]
34+
fn bits() -> uint { sys::size_of::<int>() * 8 }
35+
36+
#[inline(always)]
37+
fn bytes() -> uint { Primitive::bits::<int>() / 8 }
38+
}
39+
1940
/// Returns `base` raised to the power of `exponent`
2041
pub fn pow(base: int, exponent: uint) -> int {
2142
if exponent == 0u {

branches/try/src/libcore/num/num.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use Quot = ops::Div;
1818
use Rem = ops::Modulo;
1919
#[cfg(not(stage0))]
2020
use ops::{Add, Sub, Mul, Quot, Rem, Neg};
21+
use ops::{Not, BitAnd, BitOr, BitXor, Shl, Shr};
2122
use option::Option;
2223
use kinds::Copy;
2324

@@ -173,6 +174,60 @@ pub trait RealExt: Real {
173174
fn yn(&self, n: int) -> Self;
174175
}
175176

177+
///
178+
/// Collects the bitwise operators under one trait.
179+
///
180+
pub trait Bitwise: Not<Self>
181+
+ BitAnd<Self,Self>
182+
+ BitOr<Self,Self>
183+
+ BitXor<Self,Self>
184+
+ Shl<Self,Self>
185+
+ Shr<Self,Self> {}
186+
187+
pub trait Bounded {
188+
// FIXME (#5527): These should be associated constants
189+
fn min_value() -> Self;
190+
fn max_value() -> Self;
191+
}
192+
193+
///
194+
/// Specifies the available operations common to all of Rust's core numeric primitives.
195+
/// These may not always make sense from a purely mathematical point of view, but
196+
/// may be useful for systems programming.
197+
///
198+
pub trait Primitive: Num
199+
+ NumCast
200+
+ Neg<Self>
201+
+ Add<Self,Self>
202+
+ Sub<Self,Self>
203+
+ Mul<Self,Self>
204+
+ Quot<Self,Self>
205+
+ Rem<Self,Self> {
206+
// FIXME (#5527): These should be associated constants
207+
fn bits() -> uint;
208+
fn bytes() -> uint;
209+
}
210+
211+
///
212+
/// A collection of traits relevant to primitive signed and unsigned integers
213+
///
214+
pub trait PrimitiveInt: Integer
215+
+ Primitive
216+
+ Bounded
217+
+ Bitwise {}
218+
219+
///
220+
/// Specialisation of `PrimitiveInt` for unsigned integers
221+
///
222+
pub trait Uint: PrimitiveInt
223+
+ Unsigned {}
224+
225+
///
226+
/// Specialisation of `PrimitiveInt` for signed integers
227+
///
228+
pub trait Int: PrimitiveInt
229+
+ Signed {}
230+
176231
///
177232
/// Cast from one machine scalar to another
178233
///

branches/try/src/libcore/num/uint-template.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ impl Integer for T {
231231
fn is_odd(&self) -> bool { !self.is_even() }
232232
}
233233
234+
impl Bitwise for T {}
235+
234236
#[cfg(notest)]
235237
impl BitOr<T,T> for T {
236238
#[inline(always)]
@@ -267,6 +269,16 @@ impl Not<T> for T {
267269
fn not(&self) -> T { !*self }
268270
}
269271
272+
impl Bounded for T {
273+
#[inline(always)]
274+
fn min_value() -> T { min_value }
275+
276+
#[inline(always)]
277+
fn max_value() -> T { max_value }
278+
}
279+
280+
impl PrimitiveInt for T {}
281+
270282
// String conversion functions and impl str -> num
271283
272284
/// Parse a string as a number in base 10.
@@ -384,6 +396,12 @@ mod tests {
384396
assert_eq!(max_value - (0b1011 as T), (0b1011 as T).not());
385397
}
386398
399+
#[test]
400+
fn test_primitive() {
401+
assert_eq!(Primitive::bits::<T>(), sys::size_of::<T>() * 8);
402+
assert_eq!(Primitive::bytes::<T>(), sys::size_of::<T>());
403+
}
404+
387405
#[test]
388406
pub fn test_to_str() {
389407
assert_eq!(to_str_radix(0 as T, 10u), ~"0");

branches/try/src/libcore/num/uint-template/u16.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,18 @@
1111
//! Operations and constants for `u16`
1212
1313
mod inst {
14+
use num::Primitive;
15+
1416
pub type T = u16;
1517
#[allow(non_camel_case_types)]
1618
pub type T_SIGNED = i16;
1719
pub static bits: uint = 16;
20+
21+
impl Primitive for u16 {
22+
#[inline(always)]
23+
fn bits() -> uint { 16 }
24+
25+
#[inline(always)]
26+
fn bytes() -> uint { Primitive::bits::<u16>() / 8 }
27+
}
1828
}

branches/try/src/libcore/num/uint-template/u32.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,18 @@
1111
//! Operations and constants for `u32`
1212
1313
mod inst {
14+
use num::Primitive;
15+
1416
pub type T = u32;
1517
#[allow(non_camel_case_types)]
1618
pub type T_SIGNED = i32;
1719
pub static bits: uint = 32;
20+
21+
impl Primitive for u32 {
22+
#[inline(always)]
23+
fn bits() -> uint { 32 }
24+
25+
#[inline(always)]
26+
fn bytes() -> uint { Primitive::bits::<u32>() / 8 }
27+
}
1828
}

0 commit comments

Comments
 (0)