]> git.lizzy.rs Git - rust.git/commitdiff
auto merge of #15407 : sneves/rust/master, r=aturon
authorbors <bors@rust-lang.org>
Thu, 24 Jul 2014 00:26:14 +0000 (00:26 +0000)
committerbors <bors@rust-lang.org>
Thu, 24 Jul 2014 00:26:14 +0000 (00:26 +0000)
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>`.

1  2 
src/libcore/num/mod.rs
src/libcore/ops.rs

Simple merge
Simple merge