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
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright 2019 LEXUGE
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

// use(s)
use {
    crate::SCell,
    num_traits::{
        CheckedAdd, CheckedDiv, CheckedMul, CheckedNeg, CheckedRem, CheckedSub, Num, One, Signed,
        Zero,
    },
    std::{
        ops::{Add, Div, Mul, Not, Rem, Sub},
        str::FromStr,
    },
};

// Basic implementations
impl<T> SCell<T> {
    /// Create a new `SCell`
    pub fn new(data: T) -> Self {
        SCell { data }
    }
    /// Get the data of underlying type `T`
    pub fn get_data(&self) -> &T {
        &self.data
    }
}

// Implementations of methods which are given in `One` and `Zero`
impl<T: Zero> SCell<T> {
    /// See `num` crate for more information
    pub fn zero() -> Self {
        SCell::new(T::zero())
    }
    /// See `num` crate for more information
    pub fn is_zero(&self) -> bool {
        self.data.is_zero()
    }
}
impl<T: One + PartialEq> SCell<T> {
    /// See `num` crate for more information
    pub fn one() -> Self {
        SCell::new(T::one())
    }
    /// See `num` crate for more information
    pub fn is_one(&self) -> bool {
        self.data.is_one()
    }
}

// Implementations of `Ops`
ops!(+=, add, Add);
ops!(-=, sub, Sub);
ops!(*=, mul, Mul);
ops!(/=, div, Div);
ops!(%=, rem, Rem);
ops!(neg);

// Implementation of method which is given in `Num`
impl<T: Num + CheckedAdd + CheckedSub + CheckedMul + CheckedDiv + CheckedRem> SCell<T> {
    /// See `num` crate for more information
    pub fn from_str_radix(str: &str, radix: u32) -> Result<Self, T::FromStrRadixErr> {
        let data = T::from_str_radix(str, radix)?;
        Ok(SCell { data })
    }
}

// Implementation of methods which are given in `Signed`
impl<
        T: Signed
            + CheckedAdd
            + CheckedSub
            + CheckedMul
            + CheckedDiv
            + CheckedRem
            + CheckedNeg
            + Ord
            + Copy,
    > SCell<T>
{
    /// See `num` crate for more information
    pub fn abs(&self) -> Option<Self> {
        if *self < Self::zero() {
            -*self
        } else {
            Some(*self)
        }
    }
    /// See `num` crate for more information
    pub fn abs_sub(&self, other: &Self) -> Option<Self> {
        if *self <= *other {
            Some(Self::zero())
        } else {
            *self - *other
        }
    }
    /// See `num` crate for more information
    pub fn signum(&self) -> Self {
        SCell {
            data: self.data.signum(),
        }
    }
    /// See `num` crate for more information
    pub fn is_positive(&self) -> bool {
        self.data.is_positive()
    }
    /// See `num` crate for more information
    pub fn is_negative(&self) -> bool {
        self.data.is_negative()
    }
}

// Implementation of `FromStr`
impl<T: FromStr> FromStr for SCell<T> {
    type Err = T::Err;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(SCell::new(T::from_str(s)?))
    }
}

// Implementation of `Not`
impl<T: Not> Not for SCell<T> {
    type Output = SCell<T::Output>;
    fn not(self) -> Self::Output {
        SCell::new(!self.data)
    }
}