use core::{
fmt,
fmt::{Display, Formatter},
hash::{Hash, Hasher},
ops::{Mul, Not},
};
#[derive(Copy, Clone, Debug)]
pub enum Sign {
Plus,
Minus,
}
impl Sign {
#[inline]
pub const fn default() -> Self {
Self::Plus
}
#[inline]
pub const fn is_negative(self) -> bool {
matches!(self, Self::Minus)
}
#[inline]
pub const fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Plus, Self::Plus) => true,
(Self::Minus, Self::Minus) => true,
(_, _) => false,
}
}
#[inline]
pub const fn not(self) -> Self {
match self {
Sign::Minus => Sign::Plus,
Sign::Plus => Sign::Minus,
}
}
#[inline]
pub const fn mul(self, rhs: Self) -> Self {
match (self, rhs) {
(Sign::Plus, Sign::Plus) => Sign::Plus,
(Sign::Minus, Sign::Minus) => Sign::Plus,
(_, _) => Sign::Minus,
}
}
#[inline]
pub const fn div(self, rhs: Self) -> Self {
self.mul(rhs)
}
}
impl Default for Sign {
fn default() -> Self {
Self::default()
}
}
impl PartialEq for Sign {
fn eq(&self, other: &Self) -> bool {
self.eq(other)
}
}
impl Eq for Sign {}
impl Hash for Sign {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
let s = match self {
Sign::Minus => -1,
Sign::Plus => 1,
};
s.hash(state);
}
}
impl Not for Sign {
type Output = Sign;
#[inline]
fn not(self) -> Self::Output {
self.not()
}
}
impl Mul<Sign> for Sign {
type Output = Sign;
#[inline]
fn mul(self, other: Sign) -> Sign {
self.mul(other)
}
}
impl Display for Sign {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Sign::Minus => "-".fmt(f),
Sign::Plus => Ok(()),
}
}
}