alloy_primitives/signed/
sign.rs1use core::{
2 fmt::{self, Write},
3 ops,
4};
5
6#[derive(Clone, Copy, Debug, PartialEq, Eq)]
8#[repr(i8)]
9pub enum Sign {
10 Negative = -1,
12 Positive = 1,
14}
15
16impl ops::Mul for Sign {
17 type Output = Self;
18
19 #[inline]
20 fn mul(self, rhs: Self) -> Self::Output {
21 match (self, rhs) {
22 (Self::Positive, Self::Positive) => Self::Positive,
23 (Self::Positive, Self::Negative) => Self::Negative,
24 (Self::Negative, Self::Positive) => Self::Negative,
25 (Self::Negative, Self::Negative) => Self::Positive,
26 }
27 }
28}
29
30impl ops::Neg for Sign {
31 type Output = Self;
32
33 #[inline]
34 fn neg(self) -> Self::Output {
35 match self {
36 Self::Positive => Self::Negative,
37 Self::Negative => Self::Positive,
38 }
39 }
40}
41
42impl ops::Not for Sign {
43 type Output = Self;
44
45 #[inline]
46 fn not(self) -> Self::Output {
47 match self {
48 Self::Positive => Self::Negative,
49 Self::Negative => Self::Positive,
50 }
51 }
52}
53
54impl fmt::Display for Sign {
55 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56 match (self, f.sign_plus()) {
57 (Self::Positive, false) => Ok(()),
58 _ => f.write_char(self.as_char()),
59 }
60 }
61}
62
63impl Sign {
64 #[inline]
66 pub const fn const_eq(self, other: Self) -> bool {
67 self as i8 == other as i8
68 }
69
70 #[inline]
72 pub const fn is_positive(&self) -> bool {
73 matches!(self, Self::Positive)
74 }
75
76 #[inline]
78 pub const fn is_negative(&self) -> bool {
79 matches!(self, Self::Negative)
80 }
81
82 #[inline]
84 pub const fn as_char(&self) -> char {
85 match self {
86 Self::Positive => '+',
87 Self::Negative => '-',
88 }
89 }
90}