geometry/
sign.rs

1//! Signs: positive or negative.
2
3use array_map::Indexable;
4use serde::{Deserialize, Serialize};
5
6/// Enumeration over possible signs.
7#[derive(Debug, Clone, Copy, Serialize, Deserialize, Hash, PartialEq, Eq)]
8#[repr(u8)]
9#[derive(Indexable)]
10pub enum Sign {
11    /// Positive.
12    Pos,
13    /// Negative.
14    Neg,
15}
16
17impl Sign {
18    /// Converts this sign to +1 (if positive) or -1 (if negative).
19    #[inline]
20    pub const fn as_int(&self) -> i64 {
21        match self {
22            Self::Pos => 1,
23            Self::Neg => -1,
24        }
25    }
26
27    /// Returns true if the sign is positive.
28    #[inline]
29    pub const fn is_pos(&self) -> bool {
30        matches!(self, Sign::Pos)
31    }
32
33    /// Returns true if the sign is negative.
34    #[inline]
35    pub const fn is_neg(&self) -> bool {
36        matches!(self, Sign::Neg)
37    }
38}
39
40impl std::ops::Not for Sign {
41    type Output = Self;
42    /// Flips the [`Sign`].
43    fn not(self) -> Self::Output {
44        match self {
45            Self::Pos => Self::Neg,
46            Self::Neg => Self::Pos,
47        }
48    }
49}