scalc/
macros.rs

1// Copyright 2019 LEXUGE
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16macro_rules! ops {
17    (neg) => {
18        impl<T: num_traits::CheckedNeg> std::ops::Neg for SCell<T> {
19            type Output = Option<Self>;
20            fn neg(self) -> Self::Output {
21                self.data.checked_neg().map(SCell::new)
22            }
23        }
24    };
25    ($op: tt, $fn: ident, $trait: ident) => {
26        paste::item! {
27            impl<T: num_traits::[<Checked $trait>]> $trait for SCell<T> {
28                type Output = Option<Self>;
29                fn $fn(self, rhs: Self) -> Self::Output {
30                    self.data.[<checked_ $fn>](&rhs.data).map(SCell::new)
31                }
32            }
33        }
34    };
35}