summary refs log tree commit diff
path: root/src/libcore/num/uint-template/u8.rs
blob: 52bc56b955cc1cd1e660f6914fc07c1b9b9a6744 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Operations and constants for `u8`

pub use self::inst::is_ascii;

use num::NumCast;

mod inst {
    pub type T = u8;
    #[allow(non_camel_case_types)]
    pub type T_SIGNED = i8;
    pub static bits: uint = 8;

    // Type-specific functions here. These must be reexported by the
    // parent module so that they appear in core::u8 and not core::u8::u8;

    pub fn is_ascii(x: T) -> bool { return 0 as T == x & 128 as T; }
}

impl NumCast for u8 {
    /**
     * Cast `n` to a `u8`
     */
    #[inline(always)]
    fn from<N:NumCast>(n: N) -> u8 { n.to_u8() }

    #[inline(always)] fn to_u8(&self)    -> u8    { *self          }
    #[inline(always)] fn to_u16(&self)   -> u16   { *self as u16   }
    #[inline(always)] fn to_u32(&self)   -> u32   { *self as u32   }
    #[inline(always)] fn to_u64(&self)   -> u64   { *self as u64   }
    #[inline(always)] fn to_uint(&self)  -> uint  { *self as uint  }

    #[inline(always)] fn to_i8(&self)    -> i8    { *self as i8    }
    #[inline(always)] fn to_i16(&self)   -> i16   { *self as i16   }
    #[inline(always)] fn to_i32(&self)   -> i32   { *self as i32   }
    #[inline(always)] fn to_i64(&self)   -> i64   { *self as i64   }
    #[inline(always)] fn to_int(&self)   -> int   { *self as int   }

    #[inline(always)] fn to_f32(&self)   -> f32   { *self as f32   }
    #[inline(always)] fn to_f64(&self)   -> f64   { *self as f64   }
    #[inline(always)] fn to_float(&self) -> float { *self as float }
}

#[test]
fn test_numcast() {
    assert!((20u     == 20u8.to_uint()));
    assert!((20u8    == 20u8.to_u8()));
    assert!((20u16   == 20u8.to_u16()));
    assert!((20u32   == 20u8.to_u32()));
    assert!((20u64   == 20u8.to_u64()));
    assert!((20i     == 20u8.to_int()));
    assert!((20i8    == 20u8.to_i8()));
    assert!((20i16   == 20u8.to_i16()));
    assert!((20i32   == 20u8.to_i32()));
    assert!((20i64   == 20u8.to_i64()));
    assert!((20f     == 20u8.to_float()));
    assert!((20f32   == 20u8.to_f32()));
    assert!((20f64   == 20u8.to_f64()));

    assert!((20u8 == NumCast::from(20u)));
    assert!((20u8 == NumCast::from(20u8)));
    assert!((20u8 == NumCast::from(20u16)));
    assert!((20u8 == NumCast::from(20u32)));
    assert!((20u8 == NumCast::from(20u64)));
    assert!((20u8 == NumCast::from(20i)));
    assert!((20u8 == NumCast::from(20i8)));
    assert!((20u8 == NumCast::from(20i16)));
    assert!((20u8 == NumCast::from(20i32)));
    assert!((20u8 == NumCast::from(20i64)));
    assert!((20u8 == NumCast::from(20f)));
    assert!((20u8 == NumCast::from(20f32)));
    assert!((20u8 == NumCast::from(20f64)));

    assert!((20u8 == num::cast(20u)));
    assert!((20u8 == num::cast(20u8)));
    assert!((20u8 == num::cast(20u16)));
    assert!((20u8 == num::cast(20u32)));
    assert!((20u8 == num::cast(20u64)));
    assert!((20u8 == num::cast(20i)));
    assert!((20u8 == num::cast(20i8)));
    assert!((20u8 == num::cast(20i16)));
    assert!((20u8 == num::cast(20i32)));
    assert!((20u8 == num::cast(20i64)));
    assert!((20u8 == num::cast(20f)));
    assert!((20u8 == num::cast(20f32)));
    assert!((20u8 == num::cast(20f64)));
}