Skip to content

Commit 0fcb859

Browse files
committed
Moved checked trait impls out of std::num
This follows the same pattern as the other numeric trait impls, and reduces the clutter in std::num.
1 parent 5591dce commit 0fcb859

File tree

11 files changed

+411
-383
lines changed

11 files changed

+411
-383
lines changed

src/libstd/num/i16.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
//! Operations and constants for `i16`
1212
13-
use num::BitCount;
13+
use num::{BitCount, CheckedAdd, CheckedSub, CheckedMul};
14+
use option::{Option, Some, None};
1415
use unstable::intrinsics;
1516

1617
pub use self::generated::*;
@@ -30,3 +31,33 @@ impl BitCount for i16 {
3031
#[inline]
3132
fn trailing_zeros(&self) -> i16 { unsafe { intrinsics::cttz16(*self) } }
3233
}
34+
35+
impl CheckedAdd for i16 {
36+
#[inline]
37+
fn checked_add(&self, v: &i16) -> Option<i16> {
38+
unsafe {
39+
let (x, y) = intrinsics::i16_add_with_overflow(*self, *v);
40+
if y { None } else { Some(x) }
41+
}
42+
}
43+
}
44+
45+
impl CheckedSub for i16 {
46+
#[inline]
47+
fn checked_sub(&self, v: &i16) -> Option<i16> {
48+
unsafe {
49+
let (x, y) = intrinsics::i16_sub_with_overflow(*self, *v);
50+
if y { None } else { Some(x) }
51+
}
52+
}
53+
}
54+
55+
impl CheckedMul for i16 {
56+
#[inline]
57+
fn checked_mul(&self, v: &i16) -> Option<i16> {
58+
unsafe {
59+
let (x, y) = intrinsics::i16_mul_with_overflow(*self, *v);
60+
if y { None } else { Some(x) }
61+
}
62+
}
63+
}

src/libstd/num/i32.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
//! Operations and constants for `i32`
1212
13-
use num::BitCount;
13+
use num::{BitCount, CheckedAdd, CheckedSub, CheckedMul};
14+
use option::{Option, Some, None};
1415
use unstable::intrinsics;
1516

1617
pub use self::generated::*;
@@ -30,3 +31,33 @@ impl BitCount for i32 {
3031
#[inline]
3132
fn trailing_zeros(&self) -> i32 { unsafe { intrinsics::cttz32(*self) } }
3233
}
34+
35+
impl CheckedAdd for i32 {
36+
#[inline]
37+
fn checked_add(&self, v: &i32) -> Option<i32> {
38+
unsafe {
39+
let (x, y) = intrinsics::i32_add_with_overflow(*self, *v);
40+
if y { None } else { Some(x) }
41+
}
42+
}
43+
}
44+
45+
impl CheckedSub for i32 {
46+
#[inline]
47+
fn checked_sub(&self, v: &i32) -> Option<i32> {
48+
unsafe {
49+
let (x, y) = intrinsics::i32_sub_with_overflow(*self, *v);
50+
if y { None } else { Some(x) }
51+
}
52+
}
53+
}
54+
55+
impl CheckedMul for i32 {
56+
#[inline]
57+
fn checked_mul(&self, v: &i32) -> Option<i32> {
58+
unsafe {
59+
let (x, y) = intrinsics::i32_mul_with_overflow(*self, *v);
60+
if y { None } else { Some(x) }
61+
}
62+
}
63+
}

src/libstd/num/i64.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
//! Operations and constants for `i64`
1212
13-
use num::BitCount;
13+
use num::{BitCount, CheckedAdd, CheckedSub, CheckedMul};
14+
use option::{Option, Some, None};
1415
use unstable::intrinsics;
1516

1617
pub use self::generated::*;
@@ -30,3 +31,35 @@ impl BitCount for i64 {
3031
#[inline]
3132
fn trailing_zeros(&self) -> i64 { unsafe { intrinsics::cttz64(*self) } }
3233
}
34+
35+
impl CheckedAdd for i64 {
36+
#[inline]
37+
fn checked_add(&self, v: &i64) -> Option<i64> {
38+
unsafe {
39+
let (x, y) = intrinsics::i64_add_with_overflow(*self, *v);
40+
if y { None } else { Some(x) }
41+
}
42+
}
43+
}
44+
45+
impl CheckedSub for i64 {
46+
#[inline]
47+
fn checked_sub(&self, v: &i64) -> Option<i64> {
48+
unsafe {
49+
let (x, y) = intrinsics::i64_sub_with_overflow(*self, *v);
50+
if y { None } else { Some(x) }
51+
}
52+
}
53+
}
54+
55+
// FIXME: #8449: should not be disabled on 32-bit
56+
#[cfg(target_word_size = "64")]
57+
impl CheckedMul for i64 {
58+
#[inline]
59+
fn checked_mul(&self, v: &i64) -> Option<i64> {
60+
unsafe {
61+
let (x, y) = intrinsics::i64_mul_with_overflow(*self, *v);
62+
if y { None } else { Some(x) }
63+
}
64+
}
65+
}

src/libstd/num/i8.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
//! Operations and constants for `i8`
1212
13-
use num::BitCount;
13+
use num::{BitCount, CheckedAdd, CheckedSub, CheckedMul};
14+
use option::{Option, Some, None};
1415
use unstable::intrinsics;
1516

1617
pub use self::generated::*;
@@ -30,3 +31,33 @@ impl BitCount for i8 {
3031
#[inline]
3132
fn trailing_zeros(&self) -> i8 { unsafe { intrinsics::cttz8(*self) } }
3233
}
34+
35+
impl CheckedAdd for i8 {
36+
#[inline]
37+
fn checked_add(&self, v: &i8) -> Option<i8> {
38+
unsafe {
39+
let (x, y) = intrinsics::i8_add_with_overflow(*self, *v);
40+
if y { None } else { Some(x) }
41+
}
42+
}
43+
}
44+
45+
impl CheckedSub for i8 {
46+
#[inline]
47+
fn checked_sub(&self, v: &i8) -> Option<i8> {
48+
unsafe {
49+
let (x, y) = intrinsics::i8_sub_with_overflow(*self, *v);
50+
if y { None } else { Some(x) }
51+
}
52+
}
53+
}
54+
55+
impl CheckedMul for i8 {
56+
#[inline]
57+
fn checked_mul(&self, v: &i8) -> Option<i8> {
58+
unsafe {
59+
let (x, y) = intrinsics::i8_mul_with_overflow(*self, *v);
60+
if y { None } else { Some(x) }
61+
}
62+
}
63+
}

src/libstd/num/int.rs

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
1313
#[allow(non_uppercase_statics)];
1414

15-
use num::BitCount;
15+
use num::{BitCount, CheckedAdd, CheckedSub, CheckedMul};
16+
use option::{Option, Some, None};
17+
use unstable::intrinsics;
1618

1719
pub use self::generated::*;
1820

@@ -51,6 +53,72 @@ impl BitCount for int {
5153
fn trailing_zeros(&self) -> int { (*self as i64).trailing_zeros() as int }
5254
}
5355

56+
#[cfg(target_word_size = "32")]
57+
impl CheckedAdd for int {
58+
#[inline]
59+
fn checked_add(&self, v: &int) -> Option<int> {
60+
unsafe {
61+
let (x, y) = intrinsics::i32_add_with_overflow(*self as i32, *v as i32);
62+
if y { None } else { Some(x as int) }
63+
}
64+
}
65+
}
66+
67+
#[cfg(target_word_size = "64")]
68+
impl CheckedAdd for int {
69+
#[inline]
70+
fn checked_add(&self, v: &int) -> Option<int> {
71+
unsafe {
72+
let (x, y) = intrinsics::i64_add_with_overflow(*self as i64, *v as i64);
73+
if y { None } else { Some(x as int) }
74+
}
75+
}
76+
}
77+
78+
#[cfg(target_word_size = "32")]
79+
impl CheckedSub for int {
80+
#[inline]
81+
fn checked_sub(&self, v: &int) -> Option<int> {
82+
unsafe {
83+
let (x, y) = intrinsics::i32_sub_with_overflow(*self as i32, *v as i32);
84+
if y { None } else { Some(x as int) }
85+
}
86+
}
87+
}
88+
89+
#[cfg(target_word_size = "64")]
90+
impl CheckedSub for int {
91+
#[inline]
92+
fn checked_sub(&self, v: &int) -> Option<int> {
93+
unsafe {
94+
let (x, y) = intrinsics::i64_sub_with_overflow(*self as i64, *v as i64);
95+
if y { None } else { Some(x as int) }
96+
}
97+
}
98+
}
99+
100+
#[cfg(target_word_size = "32")]
101+
impl CheckedMul for int {
102+
#[inline]
103+
fn checked_mul(&self, v: &int) -> Option<int> {
104+
unsafe {
105+
let (x, y) = intrinsics::i32_mul_with_overflow(*self as i32, *v as i32);
106+
if y { None } else { Some(x as int) }
107+
}
108+
}
109+
}
110+
111+
#[cfg(target_word_size = "64")]
112+
impl CheckedMul for int {
113+
#[inline]
114+
fn checked_mul(&self, v: &int) -> Option<int> {
115+
unsafe {
116+
let (x, y) = intrinsics::i64_mul_with_overflow(*self as i64, *v as i64);
117+
if y { None } else { Some(x as int) }
118+
}
119+
}
120+
}
121+
54122
/// Returns `base` raised to the power of `exponent`
55123
pub fn pow(base: int, exponent: uint) -> int {
56124
if exponent == 0u {

0 commit comments

Comments
 (0)