/rust/registry/src/index.crates.io-1949cf8c6b5b557f/fastnum-0.7.4/src/decimal/sign.rs
Line | Count | Source |
1 | | use core::{ |
2 | | fmt, |
3 | | fmt::{Display, Formatter}, |
4 | | hash::{Hash, Hasher}, |
5 | | ops::{Mul, Not}, |
6 | | }; |
7 | | |
8 | | /// A `Sign` represents `sign` associated with a decimal number. |
9 | | #[derive(Copy, Clone, Debug)] |
10 | | #[repr(u8)] |
11 | | pub enum Sign { |
12 | | /// Positive: plus "+" or no sign. |
13 | | Plus, |
14 | | |
15 | | /// Negative: minus "-" sign. |
16 | | Minus, |
17 | | } |
18 | | |
19 | | impl Sign { |
20 | | /// Returns the default `Plus`. |
21 | | #[inline(always)] |
22 | 0 | pub const fn default() -> Self { |
23 | 0 | Self::Plus |
24 | 0 | } |
25 | | |
26 | | /// Returns `true` if sign is negative, and `false` otherwise. |
27 | | #[inline(always)] |
28 | 0 | pub const fn is_negative(self) -> bool { |
29 | 0 | matches!(self, Self::Minus) |
30 | 0 | } |
31 | | |
32 | | /// Tests for `self` and `other` signs to be equal, and is used by `==` |
33 | | /// operator. |
34 | | #[inline(always)] |
35 | 0 | pub const fn eq(&self, other: &Self) -> bool { |
36 | 0 | match (self, other) { |
37 | 0 | (Self::Plus, Self::Plus) => true, |
38 | 0 | (Self::Minus, Self::Minus) => true, |
39 | 0 | (_, _) => false, |
40 | | } |
41 | 0 | } |
42 | | |
43 | | /// Invert `Sign` value. |
44 | | /// |
45 | | /// # Example |
46 | | /// |
47 | | /// ``` |
48 | | /// use fastnum::decimal::Sign; |
49 | | /// |
50 | | /// assert_eq!(Sign::Plus.not(), Sign::Minus); |
51 | | /// assert_eq!(Sign::Minus.not(), Sign::Plus); |
52 | | /// ``` |
53 | | #[inline(always)] |
54 | 0 | pub const fn not(self) -> Self { |
55 | 0 | match self { |
56 | 0 | Sign::Minus => Sign::Plus, |
57 | 0 | Sign::Plus => Sign::Minus, |
58 | | } |
59 | 0 | } |
60 | | |
61 | | /// Sign "multiplication". |
62 | | #[inline(always)] |
63 | 0 | pub const fn mul(self, rhs: Self) -> Self { |
64 | 0 | match (self, rhs) { |
65 | 0 | (Sign::Plus, Sign::Plus) => Sign::Plus, |
66 | 0 | (Sign::Minus, Sign::Minus) => Sign::Plus, |
67 | 0 | (_, _) => Sign::Minus, |
68 | | } |
69 | 0 | } |
70 | | |
71 | | /// Sign "division". |
72 | | #[inline(always)] |
73 | 0 | pub const fn div(self, rhs: Self) -> Self { |
74 | 0 | self.mul(rhs) |
75 | 0 | } |
76 | | } |
77 | | |
78 | | impl Default for Sign { |
79 | 0 | fn default() -> Self { |
80 | 0 | Self::default() |
81 | 0 | } |
82 | | } |
83 | | |
84 | | impl PartialEq for Sign { |
85 | 0 | fn eq(&self, other: &Self) -> bool { |
86 | 0 | self.eq(other) |
87 | 0 | } |
88 | | } |
89 | | |
90 | | impl Eq for Sign {} |
91 | | |
92 | | impl Hash for Sign { |
93 | | #[inline] |
94 | 0 | fn hash<H: Hasher>(&self, state: &mut H) { |
95 | 0 | let s = match self { |
96 | 0 | Sign::Minus => -1, |
97 | 0 | Sign::Plus => 1, |
98 | | }; |
99 | 0 | s.hash(state); |
100 | 0 | } |
101 | | } |
102 | | |
103 | | impl Not for Sign { |
104 | | type Output = Sign; |
105 | | |
106 | | #[inline] |
107 | 0 | fn not(self) -> Self::Output { |
108 | 0 | self.not() |
109 | 0 | } |
110 | | } |
111 | | |
112 | | impl Mul<Sign> for Sign { |
113 | | type Output = Sign; |
114 | | |
115 | | #[inline] |
116 | 0 | fn mul(self, other: Sign) -> Sign { |
117 | 0 | self.mul(other) |
118 | 0 | } |
119 | | } |
120 | | |
121 | | impl Display for Sign { |
122 | | #[inline] |
123 | 0 | fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { |
124 | 0 | match self { |
125 | 0 | Sign::Minus => "-".fmt(f), |
126 | 0 | Sign::Plus => Ok(()), |
127 | | } |
128 | 0 | } Unexecuted instantiation: <fastnum::decimal::sign::Sign as core::fmt::Display>::fmt Unexecuted instantiation: <fastnum::decimal::sign::Sign as core::fmt::Display>::fmt |
129 | | } |