(index<- ) ./libcore/num/u8.rs
git branch: * master 5200215 auto merge of #14035 : alexcrichton/rust/experimental, r=huonw
modified: Fri May 9 13:02:28 2014
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Operations and constants for unsigned 8-bits integers (`u8` type)
12
13 use default::Default;
14 use intrinsics;
15 use num::{Bitwise, Bounded, Zero, One, Unsigned, Num, Int, Primitive};
16 use num::{CheckedAdd, CheckedSub, CheckedMul, CheckedDiv};
17 use option::{Some, None, Option};
18
19 #[cfg(not(test))]
20 use cmp::{Eq, Ord, TotalEq, TotalOrd, Less, Greater, Equal, Ordering};
21 #[cfg(not(test))]
22 use ops::{Add, Sub, Mul, Div, Rem, Neg, BitAnd, BitOr, BitXor};
23 #[cfg(not(test))]
24 use ops::{Shl, Shr, Not};
25
26 uint_module!(u8, i8, 8)
27
28 impl CheckedAdd for u8 {
29 #[inline]
30 fn checked_add(&self, v: &u8) -> Option<u8> {
31 unsafe {
32 let (x, y) = intrinsics::u8_add_with_overflow(*self, *v);
33 if y { None } else { Some(x) }
34 }
35 }
36 }
37
38 impl CheckedSub for u8 {
39 #[inline]
40 fn checked_sub(&self, v: &u8) -> Option<u8> {
41 unsafe {
42 let (x, y) = intrinsics::u8_sub_with_overflow(*self, *v);
43 if y { None } else { Some(x) }
44 }
45 }
46 }
47
48 impl CheckedMul for u8 {
49 #[inline]
50 fn checked_mul(&self, v: &u8) -> Option<u8> {
51 unsafe {
52 let (x, y) = intrinsics::u8_mul_with_overflow(*self, *v);
53 if y { None } else { Some(x) }
54 }
55 }
56 }