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
//! Binary operations.
use core::ops::{Add, Div, Mul, Rem, Sub};
/// Arithmetic operation, such as `+`, `-`, `*`, `/`, `%`.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Math {
/// Addition
Add,
/// Subtraction
Sub,
/// Multiplication
Mul,
/// Division
Div,
/// Remainder
Rem,
}
impl Math {
/// Perform the arithmetic operation on the given inputs.
pub fn run<I, O>(&self, l: I, r: I) -> O
where
I: Add<Output = O> + Sub<Output = O> + Mul<Output = O> + Div<Output = O> + Rem<Output = O>,
{
match self {
Self::Add => l + r,
Self::Sub => l - r,
Self::Mul => l * r,
Self::Div => l / r,
Self::Rem => l % r,
}
}
}
impl Math {
/// String representation of an arithmetic operation.
pub fn as_str(&self) -> &'static str {
match self {
Self::Add => "+",
Self::Sub => "-",
Self::Mul => "*",
Self::Div => "/",
Self::Rem => "%",
}
}
}
/// An operation that orders two values, such as `<`, `<=`, `>`, `>=`, `==`, `!=`.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Cmp {
/// Less-than (<).
Lt,
/// Less-than or equal (<=).
Le,
/// Greater-than (>).
Gt,
/// Greater-than or equal (>=).
Ge,
/// Equals (=).
Eq,
/// Not equals (!=).
Ne,
}
impl Cmp {
/// Perform the ordering operation on the given inputs.
pub fn run<I: PartialOrd + PartialEq>(&self, l: &I, r: &I) -> bool {
match self {
Self::Gt => l > r,
Self::Ge => l >= r,
Self::Lt => l < r,
Self::Le => l <= r,
Self::Eq => l == r,
Self::Ne => l != r,
}
}
}
impl Cmp {
/// String representation of a comparison operation.
pub fn as_str(&self) -> &'static str {
match self {
Self::Lt => "<",
Self::Gt => ">",
Self::Le => "<=",
Self::Ge => ">=",
Self::Eq => "==",
Self::Ne => "!=",
}
}
}