/rust/registry/src/index.crates.io-1949cf8c6b5b557f/jiff-0.2.16/src/util/c.rs
Line | Count | Source |
1 | | /*! |
2 | | A module for constants and various base utilities. |
3 | | |
4 | | This module is a work-in-progress that may lead to helping us move off of |
5 | | ranged integers. I'm not quite sure where this will go. |
6 | | */ |
7 | | |
8 | | use crate::util::t; |
9 | | |
10 | | /// A representation of a numeric sign. |
11 | | /// |
12 | | /// Its `Display` impl emits the ASCII minus sign, `-` when this |
13 | | /// is negative. It emits the empty string in all other cases. |
14 | | #[derive( |
15 | | Clone, Copy, Debug, Default, Eq, Hash, PartialEq, PartialOrd, Ord, |
16 | | )] |
17 | | #[repr(i8)] |
18 | | pub(crate) enum Sign { |
19 | | #[default] |
20 | | Zero = 0, |
21 | | Positive = 1, |
22 | | Negative = -1, |
23 | | } |
24 | | |
25 | | impl Sign { |
26 | | /* |
27 | | pub(crate) fn is_zero(&self) -> bool { |
28 | | matches!(*self, Sign::Zero) |
29 | | } |
30 | | |
31 | | pub(crate) fn is_positive(&self) -> bool { |
32 | | matches!(*self, Sign::Positive) |
33 | | } |
34 | | */ |
35 | | |
36 | 0 | pub(crate) fn is_negative(&self) -> bool { |
37 | 0 | matches!(*self, Sign::Negative) |
38 | 0 | } |
39 | | |
40 | 0 | pub(crate) fn as_ranged_integer(&self) -> t::Sign { |
41 | 0 | match *self { |
42 | 0 | Sign::Zero => t::Sign::N::<0>(), |
43 | 0 | Sign::Positive => t::Sign::N::<1>(), |
44 | 0 | Sign::Negative => t::Sign::N::<-1>(), |
45 | | } |
46 | 0 | } |
47 | | } |
48 | | |
49 | | impl core::fmt::Display for Sign { |
50 | 0 | fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { |
51 | 0 | if self.is_negative() { |
52 | 0 | write!(f, "-") |
53 | | } else { |
54 | 0 | Ok(()) |
55 | | } |
56 | 0 | } |
57 | | } |