Skip to content

Commit 2224edc

Browse files
committed
auto merge of #15407 : sneves/rust/master, r=aturon
At the moment, writing generic functions for integer types that involve shifting is rather verbose. For example, a function at shifts an integer left by 1 currently requires use std::num::One; fn f<T: Int>(x : T) -> T { x << One::one() } If the shift amount is not 1, it's even worse: use std::num::FromPrimitive; fn f<T: Int + FromPrimitive>(x: T) -> T { x << FromPrimitive::from_int(2).unwrap() } This patch allows the much simpler implementation fn f<T: Int>(x: T) -> T { x << 2 } It accomplishes this by changing the built-in integer types (and the `Int` trait) to implement `Shl<uint, T>` instead of `Shl<T, T>` as it currently is defined. Note that the internal implementations of `shl` already cast the right-hand side to `uint`. `BigInt` also implements `Shl<uint, BigInt>`, so this increases consistency. All of the above applies similarly to right shifts, i.e., `Shr<uint, T>`.
2 parents fb72c47 + 7b7d23c commit 2224edc

File tree

4 files changed

+17
-17
lines changed

4 files changed

+17
-17
lines changed

src/libcore/num/mod.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,8 @@ pub trait Int: Primitive
391391
+ BitAnd<Self,Self>
392392
+ BitOr<Self,Self>
393393
+ BitXor<Self,Self>
394-
+ Shl<Self,Self>
395-
+ Shr<Self,Self> {
394+
+ Shl<uint,Self>
395+
+ Shr<uint,Self> {
396396
/// Returns the number of ones in the binary representation of the integer.
397397
///
398398
/// # Example
@@ -658,12 +658,12 @@ int_cast_impl!(i64, u64)
658658
/// Returns the smallest power of 2 greater than or equal to `n`.
659659
#[inline]
660660
pub fn next_power_of_two<T: Unsigned + Int>(n: T) -> T {
661-
let halfbits: T = cast(size_of::<T>() * 4).unwrap();
661+
let halfbits = size_of::<T>() * 4;
662662
let mut tmp: T = n - one();
663-
let mut shift: T = one();
663+
let mut shift = 1u;
664664
while shift <= halfbits {
665665
tmp = tmp | (tmp >> shift);
666-
shift = shift << one();
666+
shift = shift << 1u;
667667
}
668668
tmp + one()
669669
}
@@ -679,12 +679,12 @@ pub fn is_power_of_two<T: Unsigned + Int>(n: T) -> bool {
679679
/// otherwise the power of 2 is wrapped in `Some`.
680680
#[inline]
681681
pub fn checked_next_power_of_two<T: Unsigned + Int>(n: T) -> Option<T> {
682-
let halfbits: T = cast(size_of::<T>() * 4).unwrap();
682+
let halfbits = size_of::<T>() * 4;
683683
let mut tmp: T = n - one();
684-
let mut shift: T = one();
684+
let mut shift = 1u;
685685
while shift <= halfbits {
686686
tmp = tmp | (tmp >> shift);
687-
shift = shift << one();
687+
shift = shift << 1u;
688688
}
689689
tmp.checked_add(&one())
690690
}

src/libcore/ops.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -558,10 +558,10 @@ pub trait Shl<RHS,Result> {
558558

559559
macro_rules! shl_impl(
560560
($($t:ty)*) => ($(
561-
impl Shl<$t, $t> for $t {
561+
impl Shl<uint, $t> for $t {
562562
#[inline]
563-
fn shl(&self, other: &$t) -> $t {
564-
(*self) << (*other as uint)
563+
fn shl(&self, other: &uint) -> $t {
564+
(*self) << (*other)
565565
}
566566
}
567567
)*)
@@ -601,9 +601,9 @@ pub trait Shr<RHS,Result> {
601601

602602
macro_rules! shr_impl(
603603
($($t:ty)*) => ($(
604-
impl Shr<$t, $t> for $t {
604+
impl Shr<uint, $t> for $t {
605605
#[inline]
606-
fn shr(&self, other: &$t) -> $t { (*self) >> (*other as uint) }
606+
fn shr(&self, other: &uint) -> $t { (*self) >> (*other) }
607607
}
608608
)*)
609609
)

src/libcoretest/num/int_macros.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ mod tests {
7474
assert!(0b1110 as $T == (0b1100 as $T).bitor(&(0b1010 as $T)));
7575
assert!(0b1000 as $T == (0b1100 as $T).bitand(&(0b1010 as $T)));
7676
assert!(0b0110 as $T == (0b1100 as $T).bitxor(&(0b1010 as $T)));
77-
assert!(0b1110 as $T == (0b0111 as $T).shl(&(1 as $T)));
78-
assert!(0b0111 as $T == (0b1110 as $T).shr(&(1 as $T)));
77+
assert!(0b1110 as $T == (0b0111 as $T).shl(&1));
78+
assert!(0b0111 as $T == (0b1110 as $T).shr(&1));
7979
assert!(-(0b11 as $T) - (1 as $T) == (0b11 as $T).not());
8080
}
8181

src/libcoretest/num/uint_macros.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ mod tests {
3434
assert!(0b1110 as $T == (0b1100 as $T).bitor(&(0b1010 as $T)));
3535
assert!(0b1000 as $T == (0b1100 as $T).bitand(&(0b1010 as $T)));
3636
assert!(0b0110 as $T == (0b1100 as $T).bitxor(&(0b1010 as $T)));
37-
assert!(0b1110 as $T == (0b0111 as $T).shl(&(1 as $T)));
38-
assert!(0b0111 as $T == (0b1110 as $T).shr(&(1 as $T)));
37+
assert!(0b1110 as $T == (0b0111 as $T).shl(&1u));
38+
assert!(0b0111 as $T == (0b1110 as $T).shr(&1u));
3939
assert!(MAX - (0b1011 as $T) == (0b1011 as $T).not());
4040
}
4141

0 commit comments

Comments
 (0)