1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use core::marker::PhantomData;
/// The sign of a value.
#[derive(Debug)]
pub struct Sign<T> {
/// Whether the sign value is positive.
is_positive: bool,
/// Required for the Rust compiler.
marker: PhantomData<fn() -> T>,
}
impl<T> Clone for Sign<T> {
fn clone(&self) -> Self {
*self
}
}
impl<T> Copy for Sign<T> {}
impl<T> PartialEq for Sign<T> {
fn eq(&self, other: &Self) -> bool {
self.is_positive == other.is_positive
}
}
impl<T> Eq for Sign<T> {}
impl<T> Sign<T> {
/// Create a new typed [`Sign`] with the given value.
pub fn new(is_positive: bool) -> Self {
Self {
is_positive,
marker: PhantomData,
}
}
/// Creates a new typed [`Sign`] that has positive polarity.
pub fn pos() -> Self {
Self::new(true)
}
/// Creates a new typed [`Sign`] that has negative polarity.
pub fn neg() -> Self {
Self::new(false)
}
/// Returns `true` if [`Sign`] is positive.
pub fn is_pos(self) -> bool {
self.is_positive
}
/// Returns `true` if [`Sign`] is negative.
pub fn is_neg(self) -> bool {
!self.is_pos()
}
}
macro_rules! impl_sign_for {
( $($ty:ty),* $(,)? ) => {
$(
impl From<$ty> for Sign<$ty> {
fn from(value: $ty) -> Self {
Self::new(value.is_sign_positive())
}
}
impl From<Sign<$ty>> for $ty {
fn from(sign: Sign<$ty>) -> Self {
match sign.is_positive {
true => 1.0,
false => -1.0,
}
}
}
)*
};
}
impl_sign_for!(f32, f64);