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
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#![allow(deprecated)]

#[allow(unused_imports)]
use crate::internal_prelude::*;
use core::ops::{Div, DivAssign, Mul, MulAssign, Neg, Not};
use Sign::{Negative, Positive, Zero};

/// Contains the sign of a value: positive, negative, or zero.
///
/// For ease of use, `Sign` implements [`Mul`] and [`Div`] on all signed numeric
/// types. `Sign`s can also be multiplied and divided by another `Sign`, which
/// follows the same rules as real numbers.
#[repr(i8)]
#[cfg_attr(serde, derive(serde::Serialize))]
#[cfg_attr(serde, serde(into = "crate::serde::Sign"))]
#[deprecated(
    since = "0.2.7",
    note = "The only use for this (obtaining the sign of a `Duration`) can be replaced with \
            `Duration::is_{positive|negative|zero}`"
)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Sign {
    /// A positive value.
    Positive = 1,

    /// A negative value.
    Negative = -1,

    /// A value that is exactly zero.
    Zero = 0,
}

#[cfg(serde)]
impl<'a> serde::Deserialize<'a> for Sign {
    #[inline(always)]
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'a>,
    {
        crate::serde::Sign::deserialize(deserializer)?
            .try_into()
            .map_err(serde::de::Error::custom)
    }
}
impl Default for Sign {
    /// `Sign` defaults to `Zero`.
    ///
    /// ```rust
    /// # use time::Sign;
    /// assert_eq!(Sign::default(), Sign::Zero);
    /// ```
    #[inline(always)]
    fn default() -> Self {
        Zero
    }
}

macro_rules! sign_mul {
    ($($type:ty),+ $(,)?) => {
        $(
            impl Mul<$type> for Sign {
                type Output = $type;

                #[allow(trivial_numeric_casts)]
                #[inline(always)]
                fn mul(self, rhs: $type) -> Self::Output {
                    (self as i8) as $type * rhs
                }
            }

            impl Mul<Sign> for $type {
                type Output = Self;

                #[inline(always)]
                fn mul(self, rhs: Sign) -> Self::Output {
                    rhs * self
                }
            }

            impl MulAssign<Sign> for $type {
                #[inline(always)]
                fn mul_assign(&mut self, rhs: Sign) {
                    if rhs.is_negative() {
                        *self = -*self;
                    }
                }
            }

            impl Div<Sign> for $type {
                type Output = Self;

                #[inline(always)]
                fn div(self, rhs: Sign) -> Self::Output {
                    self * rhs
                }
            }

            impl DivAssign<Sign> for $type {
                #[inline(always)]
                fn div_assign(&mut self, rhs: Sign) {
                    *self *= rhs
                }
            }
        )*
    };
}
sign_mul![i8, i16, i32, i64, i128, f32, f64];

impl Mul<Sign> for Sign {
    type Output = Self;

    #[inline]
    fn mul(self, rhs: Self) -> Self::Output {
        match (self, rhs) {
            (Zero, _) | (_, Zero) => Zero,
            (Positive, Positive) | (Negative, Negative) => Positive,
            (Positive, Negative) | (Negative, Positive) => Negative,
        }
    }
}

impl MulAssign<Sign> for Sign {
    #[inline(always)]
    fn mul_assign(&mut self, rhs: Self) {
        *self = *self * rhs;
    }
}

impl Div<Sign> for Sign {
    type Output = Self;

    #[inline(always)]
    fn div(self, rhs: Self) -> Self::Output {
        self * rhs
    }
}

impl DivAssign<Sign> for Sign {
    #[inline(always)]
    fn div_assign(&mut self, rhs: Self) {
        *self *= rhs
    }
}

impl Neg for Sign {
    type Output = Self;

    #[inline(always)]
    fn neg(self) -> Self::Output {
        self.negate()
    }
}

impl Not for Sign {
    type Output = Self;

    #[inline(always)]
    fn not(self) -> Self::Output {
        self.negate()
    }
}

impl Sign {
    /// Return the opposite of the current sign.
    ///
    /// ```rust
    /// # use time::Sign;
    /// assert_eq!(Sign::Positive.negate(), Sign::Negative);
    /// assert_eq!(Sign::Negative.negate(), Sign::Positive);
    /// assert_eq!(Sign::Zero.negate(), Sign::Zero);
    /// ```
    #[inline(always)]
    pub fn negate(self) -> Self {
        match self {
            Positive => Negative,
            Negative => Positive,
            Zero => Zero,
        }
    }

    /// Is the sign positive?
    ///
    /// ```rust
    /// # use time::Sign;
    /// assert!(Sign::Positive.is_positive());
    /// assert!(!Sign::Negative.is_positive());
    /// assert!(!Sign::Zero.is_positive());
    /// ```
    #[inline(always)]
    pub const fn is_positive(self) -> bool {
        self as u8 == Positive as u8
    }

    /// Is the sign negative?
    ///
    /// ```rust
    /// # use time::Sign;
    /// assert!(!Sign::Positive.is_negative());
    /// assert!(Sign::Negative.is_negative());
    /// assert!(!Sign::Zero.is_negative());
    /// ```
    #[inline(always)]
    pub const fn is_negative(self) -> bool {
        self as u8 == Negative as u8
    }

    /// Is the value exactly zero?
    ///
    /// ```rust
    /// # use time::Sign;
    /// assert!(!Sign::Positive.is_zero());
    /// assert!(!Sign::Negative.is_zero());
    /// assert!(Sign::Zero.is_zero());
    /// ```
    #[inline(always)]
    pub const fn is_zero(self) -> bool {
        self as u8 == Zero as u8
    }
}

#[cfg(test)]
mod test {
    use super::*;

    macro_rules! op_assign {
        ($a:ident $op:tt $b:ident) => {{
            let mut v = $a;
            v $op $b;
            v
        }};
    }

    #[test]
    fn default() {
        assert_eq!(Sign::default(), Zero);
    }

    #[test]
    fn sign_mul_int() {
        assert_eq!(Positive * 2, 2);
        assert_eq!(Negative * 2, -2);
        assert_eq!(Zero * 2, 0);
    }

    #[test]
    #[allow(clippy::float_cmp)]
    fn sign_mul_float() {
        assert_eq!(Positive * 2., 2.);
        assert_eq!(Negative * 2., -2.);
        assert_eq!(Zero * 2., 0.);
    }

    #[test]
    fn sign_mul_sign() {
        assert_eq!(Zero * Positive, Zero);
        assert_eq!(Zero * Negative, Zero);
        assert_eq!(Zero * Zero, Zero);
        assert_eq!(Positive * Zero, Zero);
        assert_eq!(Negative * Zero, Zero);
        assert_eq!(Positive * Positive, Positive);
        assert_eq!(Positive * Negative, Negative);
        assert_eq!(Negative * Positive, Negative);
        assert_eq!(Negative * Negative, Positive);
    }

    #[test]
    fn sign_mul_assign_sign() {
        assert_eq!(op_assign!(Zero *= Positive), Zero);
        assert_eq!(op_assign!(Zero *= Negative), Zero);
        assert_eq!(op_assign!(Zero *= Zero), Zero);
        assert_eq!(op_assign!(Positive *= Zero), Zero);
        assert_eq!(op_assign!(Negative *= Zero), Zero);
        assert_eq!(op_assign!(Positive *= Positive), Positive);
        assert_eq!(op_assign!(Positive *= Negative), Negative);
        assert_eq!(op_assign!(Negative *= Positive), Negative);
        assert_eq!(op_assign!(Negative *= Negative), Positive);
    }

    #[test]
    #[allow(clippy::eq_op)]
    fn sign_div_sign() {
        assert_eq!(Zero / Positive, Zero);
        assert_eq!(Zero / Negative, Zero);
        assert_eq!(Zero / Zero, Zero);
        assert_eq!(Positive / Zero, Zero);
        assert_eq!(Negative / Zero, Zero);
        assert_eq!(Positive / Positive, Positive);
        assert_eq!(Positive / Negative, Negative);
        assert_eq!(Negative / Positive, Negative);
        assert_eq!(Negative / Negative, Positive);
    }

    #[test]
    fn sign_div_assign_sign() {
        assert_eq!(op_assign!(Zero /= Positive), Zero);
        assert_eq!(op_assign!(Zero /= Negative), Zero);
        assert_eq!(op_assign!(Zero /= Zero), Zero);
        assert_eq!(op_assign!(Positive /= Zero), Zero);
        assert_eq!(op_assign!(Negative /= Zero), Zero);
        assert_eq!(op_assign!(Positive /= Positive), Positive);
        assert_eq!(op_assign!(Positive /= Negative), Negative);
        assert_eq!(op_assign!(Negative /= Positive), Negative);
        assert_eq!(op_assign!(Negative /= Negative), Positive);
    }

    #[test]
    fn neg() {
        assert_eq!(-Positive, Negative);
        assert_eq!(-Negative, Positive);
        assert_eq!(-Zero, Zero);
    }

    #[test]
    fn not() {
        assert_eq!(!Positive, Negative);
        assert_eq!(!Negative, Positive);
        assert_eq!(!Zero, Zero);
    }

    #[test]
    fn negate() {
        assert_eq!(Positive.negate(), Negative);
        assert_eq!(Negative.negate(), Positive);
        assert_eq!(Zero.negate(), Zero);
    }

    #[test]
    fn is_positive() {
        assert!(Positive.is_positive());
        assert!(!Negative.is_positive());
        assert!(!Zero.is_positive());
    }

    #[test]
    fn is_negative() {
        assert!(!Positive.is_negative());
        assert!(Negative.is_negative());
        assert!(!Zero.is_negative());
    }

    #[test]
    fn is_zero() {
        assert!(!Positive.is_zero());
        assert!(!Negative.is_zero());
        assert!(Zero.is_zero());
    }
}