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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use super::{Btrit, ShiftTernary, Trit};

use crate::convert;

use num_traits::{CheckedAdd, CheckedSub, Num};

use std::fmt;

#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[repr(i8)]
#[allow(missing_docs)]
pub enum Utrit {
    Zero = 0,
    One = 1,
    Two = 2,
}

use Utrit::*;

impl fmt::Display for Utrit {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", *self as i8)
    }
}

impl From<Utrit> for i8 {
    fn from(value: Utrit) -> Self {
        value as i8
    }
}

impl ShiftTernary for Utrit {
    type Target = Btrit;

    fn shift(self) -> Self::Target {
        match self {
            Zero => Self::Target::NegOne,
            One => Self::Target::Zero,
            Two => Self::Target::PlusOne,
        }
    }
}

impl Trit for Utrit {
    fn checked_increment(self) -> Option<Self> {
        match self {
            Zero => Some(One),
            One => Some(Two),
            Two => None,
        }
    }

    fn zero() -> Self {
        Self::Zero
    }

    fn as_arbitrary_ref<'a>(&self) -> &'a Self {
        static ZERO: Utrit = Utrit::Zero;
        static ONE: Utrit = Utrit::One;
        static TWO: Utrit = Utrit::Two;

        match self {
            Utrit::Zero => &ZERO,
            Utrit::One => &ONE,
            Utrit::Two => &TWO,
        }
    }

    fn add_to_num<I: Num + CheckedAdd + CheckedSub>(&self, n: I) -> Result<I, convert::Error> {
        match self {
            Utrit::Zero => Ok(n),
            Utrit::One => n.checked_add(&I::one()).ok_or(convert::Error::Overflow),
            Utrit::Two => n.checked_add(&(I::one() + I::one())).ok_or(convert::Error::Overflow),
        }
    }
}

impl Utrit {
    pub(crate) fn from_u8(x: u8) -> Self {
        match x {
            0 => Zero,
            1 => One,
            2 => Two,
            x => panic!("Invalid trit representation '{}'", x),
        }
    }

    pub(crate) fn into_u8(self) -> u8 {
        match self {
            Zero => 0,
            One => 1,
            Two => 2,
        }
    }
}

impl TryFrom<i8> for Utrit {
    type Error = ();

    fn try_from(x: i8) -> Result<Self, Self::Error> {
        let converted = match x {
            0 => Zero,
            1 => One,
            2 => Two,
            _ => return Err(()),
        };
        Ok(converted)
    }
}

impl TryFrom<u8> for Utrit {
    type Error = ();

    fn try_from(x: u8) -> Result<Self, Self::Error> {
        let converted = match x {
            0 => Zero,
            1 => One,
            2 => Two,
            _ => return Err(()),
        };
        Ok(converted)
    }
}