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
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
#![no_std]

use core::fmt;

#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct U9(u16);

impl U9 {
    #[inline]
    pub fn from_u16_masked(val: u16) -> Self {
        U9(val & 0x1FF)
    }

    #[inline]
    pub fn try_from_u16(val: u16) -> Option<Self> {
        match val {
            0...0x1FF => Some(U9(val)),
            _ => None,
        }
    }
}

// TODO: other impls
impl fmt::Debug for U9 {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        <u16 as fmt::Debug>::fmt(&self.0, f)
    }
}

impl fmt::Display for U9 {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        <u16 as fmt::Display>::fmt(&self.0, f)
    }
}

impl From<u8> for U9 {
    #[inline]
    fn from(val: u8) -> Self {
        U9(val.into())
    }
}

impl From<U9> for u16 {
    #[inline]
    fn from(val: U9) -> Self {
        val.0.into()
    }
}

impl From<U9> for i16 {
    #[inline]
    fn from(val: U9) -> Self {
        // this will never overflow because U9 maintains invariant self.0 < 2^9
        val.0 as i16
    }
}

impl From<U9> for u32 {
    #[inline]
    fn from(val: U9) -> Self {
        val.0.into()
    }
}

impl From<U9> for i32 {
    #[inline]
    fn from(val: U9) -> Self {
        val.0.into()
    }
}

impl From<U9> for u64 {
    #[inline]
    fn from(val: U9) -> Self {
        val.0.into()
    }
}

impl From<U9> for i64 {
    #[inline]
    fn from(val: U9) -> Self {
        val.0.into()
    }
}

#[cfg(test)]
mod tests {
    use ::U9;
    use ::core::fmt::Debug;

    fn check_conversion<T: From<U9> + Eq + Debug>(u9: U9, val: T) {
        let u9: T = u9.into();
        assert_eq!(u9, val);
    }

    #[test]
    fn conversions() {
        check_conversion(0u8.into(), 0u16);
        check_conversion(42u8.into(), 42u16);
        check_conversion(U9::from_u16_masked(0u16), 0u16);
        check_conversion(U9::from_u16_masked(42u16), 42u16);
        check_conversion(U9::from_u16_masked(511), 511u16);
        check_conversion(U9::from_u16_masked(512), 0u16);

        check_conversion(U9::try_from_u16(0u16).unwrap(), 0u16);
        check_conversion(U9::try_from_u16(42u16).unwrap(), 42u16);
        check_conversion(U9::try_from_u16(511).unwrap(), 511u16);

        assert!(U9::try_from_u16(512).is_none());
    }
}