diff options
| author | Brendan Zabarauskas <bjzaba@yahoo.com.au> | 2013-09-07 17:15:35 +1000 |
|---|---|---|
| committer | Brendan Zabarauskas <bjzaba@yahoo.com.au> | 2013-09-08 13:48:15 +1000 |
| commit | 0fcb85997db852350c104b40e3d7c78214bb0708 (patch) | |
| tree | 4f7b4e70847825e1b9790383cd18a224e99595e1 /src/libstd/num/u32.rs | |
| parent | 5591dce52eb35730e89070c7e104e1f1bf0a8ab3 (diff) | |
| download | rust-0fcb85997db852350c104b40e3d7c78214bb0708.tar.gz rust-0fcb85997db852350c104b40e3d7c78214bb0708.zip | |
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.
Diffstat (limited to 'src/libstd/num/u32.rs')
| -rw-r--r-- | src/libstd/num/u32.rs | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/libstd/num/u32.rs b/src/libstd/num/u32.rs index f87fa7fcd42..4dbd543da7b 100644 --- a/src/libstd/num/u32.rs +++ b/src/libstd/num/u32.rs @@ -10,5 +10,40 @@ //! Operations and constants for `u32` +use num::{CheckedAdd, CheckedSub, CheckedMul}; +use option::{Option, Some, None}; +use unstable::intrinsics; + pub use self::generated::*; + uint_module!(u32, i32, 32) + +impl CheckedAdd for u32 { + #[inline] + fn checked_add(&self, v: &u32) -> Option<u32> { + unsafe { + let (x, y) = intrinsics::u32_add_with_overflow(*self, *v); + if y { None } else { Some(x) } + } + } +} + +impl CheckedSub for u32 { + #[inline] + fn checked_sub(&self, v: &u32) -> Option<u32> { + unsafe { + let (x, y) = intrinsics::u32_sub_with_overflow(*self, *v); + if y { None } else { Some(x) } + } + } +} + +impl CheckedMul for u32 { + #[inline] + fn checked_mul(&self, v: &u32) -> Option<u32> { + unsafe { + let (x, y) = intrinsics::u32_mul_with_overflow(*self, *v); + if y { None } else { Some(x) } + } + } +} |
