Skip to content

Implement bitwise operator traits for ints and uints #5935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/libcore/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,26 +288,32 @@ impl num::One for f32 {

#[cfg(notest)]
impl ops::Add<f32,f32> for f32 {
#[inline(always)]
fn add(&self, other: &f32) -> f32 { *self + *other }
}
#[cfg(notest)]
impl ops::Sub<f32,f32> for f32 {
#[inline(always)]
fn sub(&self, other: &f32) -> f32 { *self - *other }
}
#[cfg(notest)]
impl ops::Mul<f32,f32> for f32 {
#[inline(always)]
fn mul(&self, other: &f32) -> f32 { *self * *other }
}
#[cfg(notest)]
impl ops::Div<f32,f32> for f32 {
#[inline(always)]
fn div(&self, other: &f32) -> f32 { *self / *other }
}
#[cfg(notest)]
impl ops::Modulo<f32,f32> for f32 {
#[inline(always)]
fn modulo(&self, other: &f32) -> f32 { *self % *other }
}
#[cfg(notest)]
impl ops::Neg<f32> for f32 {
#[inline(always)]
fn neg(&self) -> f32 { -*self }
}

Expand Down
6 changes: 6 additions & 0 deletions src/libcore/num/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,26 +310,32 @@ impl num::One for f64 {

#[cfg(notest)]
impl ops::Add<f64,f64> for f64 {
#[inline(always)]
fn add(&self, other: &f64) -> f64 { *self + *other }
}
#[cfg(notest)]
impl ops::Sub<f64,f64> for f64 {
#[inline(always)]
fn sub(&self, other: &f64) -> f64 { *self - *other }
}
#[cfg(notest)]
impl ops::Mul<f64,f64> for f64 {
#[inline(always)]
fn mul(&self, other: &f64) -> f64 { *self * *other }
}
#[cfg(notest)]
impl ops::Div<f64,f64> for f64 {
#[inline(always)]
fn div(&self, other: &f64) -> f64 { *self / *other }
}
#[cfg(notest)]
impl ops::Modulo<f64,f64> for f64 {
#[inline(always)]
fn modulo(&self, other: &f64) -> f64 { *self % *other }
}
#[cfg(notest)]
impl ops::Neg<f64> for f64 {
#[inline(always)]
fn neg(&self) -> f64 { -*self }
}

Expand Down
12 changes: 12 additions & 0 deletions src/libcore/num/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,15 +387,21 @@ pub fn tan(x: float) -> float {

#[cfg(notest)]
impl Eq for float {
#[inline(always)]
fn eq(&self, other: &float) -> bool { (*self) == (*other) }
#[inline(always)]
fn ne(&self, other: &float) -> bool { (*self) != (*other) }
}

#[cfg(notest)]
impl Ord for float {
#[inline(always)]
fn lt(&self, other: &float) -> bool { (*self) < (*other) }
#[inline(always)]
fn le(&self, other: &float) -> bool { (*self) <= (*other) }
#[inline(always)]
fn ge(&self, other: &float) -> bool { (*self) >= (*other) }
#[inline(always)]
fn gt(&self, other: &float) -> bool { (*self) > (*other) }
}

Expand Down Expand Up @@ -444,26 +450,32 @@ impl num::Round for float {

#[cfg(notest)]
impl ops::Add<float,float> for float {
#[inline(always)]
fn add(&self, other: &float) -> float { *self + *other }
}
#[cfg(notest)]
impl ops::Sub<float,float> for float {
#[inline(always)]
fn sub(&self, other: &float) -> float { *self - *other }
}
#[cfg(notest)]
impl ops::Mul<float,float> for float {
#[inline(always)]
fn mul(&self, other: &float) -> float { *self * *other }
}
#[cfg(notest)]
impl ops::Div<float,float> for float {
#[inline(always)]
fn div(&self, other: &float) -> float { *self / *other }
}
#[cfg(notest)]
impl ops::Modulo<float,float> for float {
#[inline(always)]
fn modulo(&self, other: &float) -> float { *self % *other }
}
#[cfg(notest)]
impl ops::Neg<float> for float {
#[inline(always)]
fn neg(&self) -> float { -*self }
}

Expand Down
47 changes: 47 additions & 0 deletions src/libcore/num/int-template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,29 +177,66 @@ impl num::One for T {

#[cfg(notest)]
impl ops::Add<T,T> for T {
#[inline(always)]
fn add(&self, other: &T) -> T { *self + *other }
}
#[cfg(notest)]
impl ops::Sub<T,T> for T {
#[inline(always)]
fn sub(&self, other: &T) -> T { *self - *other }
}
#[cfg(notest)]
impl ops::Mul<T,T> for T {
#[inline(always)]
fn mul(&self, other: &T) -> T { *self * *other }
}
#[cfg(notest)]
impl ops::Div<T,T> for T {
#[inline(always)]
fn div(&self, other: &T) -> T { *self / *other }
}
#[cfg(notest)]
impl ops::Modulo<T,T> for T {
#[inline(always)]
fn modulo(&self, other: &T) -> T { *self % *other }
}
#[cfg(notest)]
impl ops::Neg<T> for T {
#[inline(always)]
fn neg(&self) -> T { -*self }
}

#[cfg(notest)]
impl ops::BitOr<T,T> for T {
#[inline(always)]
fn bitor(&self, other: &T) -> T { *self | *other }
}
#[cfg(notest)]
impl ops::BitAnd<T,T> for T {
#[inline(always)]
fn bitand(&self, other: &T) -> T { *self & *other }
}
#[cfg(notest)]
impl ops::BitXor<T,T> for T {
#[inline(always)]
fn bitxor(&self, other: &T) -> T { *self ^ *other }
}
#[cfg(notest)]
impl ops::Shl<T,T> for T {
#[inline(always)]
fn shl(&self, other: &T) -> T { *self << *other }
}
#[cfg(notest)]
impl ops::Shr<T,T> for T {
#[inline(always)]
fn shr(&self, other: &T) -> T { *self >> *other }
}
#[cfg(notest)]
impl ops::Not<T> for T {
#[inline(always)]
fn not(&self) -> T { !*self }
}

// String conversion functions and impl str -> num

/// Parse a string as a number in base 10.
Expand Down Expand Up @@ -283,6 +320,16 @@ mod tests {
use super::inst::T;
use prelude::*;

#[test]
fn test_bitwise_ops() {
assert!(0b1110 as T == (0b1100 as T).bitor(&(0b1010 as T)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these could be assert_eq! now

assert!(0b1000 as T == (0b1100 as T).bitand(&(0b1010 as T)));
assert!(0b0110 as T == (0b1100 as T).bitxor(&(0b1010 as T)));
assert!(0b1110 as T == (0b0111 as T).shl(&(1 as T)));
assert!(0b0111 as T == (0b1110 as T).shr(&(1 as T)));
assert!(-(0b11 as T) - (1 as T) == (0b11 as T).not());
}

#[test]
fn test_from_str() {
assert!(from_str(~"0") == Some(0 as T));
Expand Down
48 changes: 48 additions & 0 deletions src/libcore/num/uint-template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,29 +142,66 @@ impl num::One for T {

#[cfg(notest)]
impl ops::Add<T,T> for T {
#[inline(always)]
fn add(&self, other: &T) -> T { *self + *other }
}
#[cfg(notest)]
impl ops::Sub<T,T> for T {
#[inline(always)]
fn sub(&self, other: &T) -> T { *self - *other }
}
#[cfg(notest)]
impl ops::Mul<T,T> for T {
#[inline(always)]
fn mul(&self, other: &T) -> T { *self * *other }
}
#[cfg(notest)]
impl ops::Div<T,T> for T {
#[inline(always)]
fn div(&self, other: &T) -> T { *self / *other }
}
#[cfg(notest)]
impl ops::Modulo<T,T> for T {
#[inline(always)]
fn modulo(&self, other: &T) -> T { *self % *other }
}
#[cfg(notest)]
impl ops::Neg<T> for T {
#[inline(always)]
fn neg(&self) -> T { -*self }
}

#[cfg(notest)]
impl ops::BitOr<T,T> for T {
#[inline(always)]
fn bitor(&self, other: &T) -> T { *self | *other }
}
#[cfg(notest)]
impl ops::BitAnd<T,T> for T {
#[inline(always)]
fn bitand(&self, other: &T) -> T { *self & *other }
}
#[cfg(notest)]
impl ops::BitXor<T,T> for T {
#[inline(always)]
fn bitxor(&self, other: &T) -> T { *self ^ *other }
}
#[cfg(notest)]
impl ops::Shl<T,T> for T {
#[inline(always)]
fn shl(&self, other: &T) -> T { *self << *other }
}
#[cfg(notest)]
impl ops::Shr<T,T> for T {
#[inline(always)]
fn shr(&self, other: &T) -> T { *self >> *other }
}
#[cfg(notest)]
impl ops::Not<T> for T {
#[inline(always)]
fn not(&self) -> T { !*self }
}

// String conversion functions and impl str -> num

/// Parse a string as a number in base 10.
Expand Down Expand Up @@ -247,6 +284,17 @@ mod tests {
use super::*;
use super::inst::T;
use prelude::*;

#[test]
fn test_bitwise_ops() {
assert!(0b1110 as T == (0b1100 as T).bitor(&(0b1010 as T)));
assert!(0b1000 as T == (0b1100 as T).bitand(&(0b1010 as T)));
assert!(0b0110 as T == (0b1100 as T).bitxor(&(0b1010 as T)));
assert!(0b1110 as T == (0b0111 as T).shl(&(1 as T)));
assert!(0b0111 as T == (0b1110 as T).shr(&(1 as T)));
assert!(max_value - (0b1011 as T) == (0b1011 as T).not());
}

#[test]
pub fn test_to_str() {
assert!(to_str_radix(0 as T, 10u) == ~"0");
Expand Down