about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2015-02-27 21:04:15 +0100
committerGuillaume Gomez <guillaume1.gomez@gmail.com>2015-03-01 01:58:55 +0100
commit1c4fb909ffad8328983d893f2fefdb17215f2b21 (patch)
treeb53f0a0dbca3cfa46cf74f6dd3d43d598314b019
parent890293655251c372ea99694c0c9f0795e2663286 (diff)
downloadrust-1c4fb909ffad8328983d893f2fefdb17215f2b21.tar.gz
rust-1c4fb909ffad8328983d893f2fefdb17215f2b21.zip
Make Int::pow() take exp as u32 instead usize
-rw-r--r--src/libcore/num/mod.rs3
-rw-r--r--src/libcoretest/num/int_macros.rs11
-rw-r--r--src/libstd/num/mod.rs2
-rw-r--r--src/test/bench/shootout-binarytrees.rs2
4 files changed, 15 insertions, 3 deletions
diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs
index b1039f79f23..318799f59a8 100644
--- a/src/libcore/num/mod.rs
+++ b/src/libcore/num/mod.rs
@@ -372,9 +372,10 @@ pub trait Int
     #[unstable(feature = "core",
                reason = "pending integer conventions")]
     #[inline]
-    fn pow(self, mut exp: uint) -> Self {
+    fn pow(self, mut exp: u32) -> Self {
         let mut base = self;
         let mut acc: Self = Int::one();
+
         while exp > 0 {
             if (exp & 1) == 1 {
                 acc = acc * base;
diff --git a/src/libcoretest/num/int_macros.rs b/src/libcoretest/num/int_macros.rs
index f5657d939b2..c33729876cc 100644
--- a/src/libcoretest/num/int_macros.rs
+++ b/src/libcoretest/num/int_macros.rs
@@ -201,6 +201,17 @@ mod tests {
         assert_eq!(FromStrRadix::from_str_radix("Z", 35).ok(), None::<$T>);
         assert_eq!(FromStrRadix::from_str_radix("-9", 2).ok(), None::<$T>);
     }
+
+    #[test]
+    fn test_pow() {
+        let mut r = 2 as $T;
+
+        assert_eq!(r.pow(2u32), 4 as $T);
+        assert_eq!(r.pow(0u32), 1 as $T);
+        r = -2 as $T;
+        assert_eq!(r.pow(2u32), 4 as $T);
+        assert_eq!(r.pow(3u32), -8 as $T);
+    }
 }
 
 )}
diff --git a/src/libstd/num/mod.rs b/src/libstd/num/mod.rs
index 15ae8b027e1..d776079efae 100644
--- a/src/libstd/num/mod.rs
+++ b/src/libstd/num/mod.rs
@@ -1830,6 +1830,6 @@ mod bench {
     #[bench]
     fn bench_pow_function(b: &mut Bencher) {
         let v = (0..1024).collect::<Vec<_>>();
-        b.iter(|| {v.iter().fold(0, |old, new| old.pow(*new));});
+        b.iter(|| {v.iter().fold(0, |old, new| old.pow(*new as u32));});
     }
 }
diff --git a/src/test/bench/shootout-binarytrees.rs b/src/test/bench/shootout-binarytrees.rs
index 1e23da3020f..38fc53ccd36 100644
--- a/src/test/bench/shootout-binarytrees.rs
+++ b/src/test/bench/shootout-binarytrees.rs
@@ -109,7 +109,7 @@ fn main() {
 
     let messages = range_step(min_depth, max_depth + 1, 2).map(|depth| {
         use std::num::Int;
-        let iterations = 2.pow((max_depth - depth + min_depth) as usize);
+        let iterations = 2.pow((max_depth - depth + min_depth) as u32);
         thread::scoped(move || inner(depth, iterations))
     }).collect::<Vec<_>>();