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
/// For types which have adjacent values.
pub trait AdjacentBound: Ord {
    /// Determines whether `self` and `other` are adjacent.
    fn is_adjacent(&self, other: &Self) -> bool {
        self.is_immediately_before(other) || self.is_immediately_after(other)
    }

    /// Determines whether `self` is immediately before `other`.
    ///
    /// ```
    /// use lz_diet::AdjacentBound;
    ///
    /// assert!(5u32.is_immediately_before(&6));
    /// assert_eq!(5u32.is_immediately_before(&7), false);
    /// ```
    fn is_immediately_before(&self, other: &Self) -> bool;

    /// Determines whether `self` is immediately after `other`.
    ///
    /// ```
    /// use lz_diet::AdjacentBound;
    ///
    /// assert!(6u32.is_immediately_after(&5));
    /// assert_eq!(7u32.is_immediately_after(&5), false);
    /// ```
    fn is_immediately_after(&self, other: &Self) -> bool;

    /// Returns a new value which is `self` incremented.
    ///
    /// ```
    /// use lz_diet::AdjacentBound;
    ///
    /// assert_eq!(6u32.increment(), 7);
    /// ```
    fn increment(&self) -> Self;

    /// Returns a new value which is `self` decremented.
    ///
    /// ```
    /// use lz_diet::AdjacentBound;
    ///
    /// assert_eq!(6u32.decrement(), 5);
    /// ```
    fn decrement(&self) -> Self;

    /// Increments `self` in place.
    ///
    /// ```
    /// use lz_diet::AdjacentBound;
    ///
    /// let mut value = 6u32;
    /// value.increment_ref();
    /// assert_eq!(value, 7);
    /// ```
    fn increment_ref(&mut self);

    /// Decrements `self` in place.
    ///
    /// ```
    /// use lz_diet::AdjacentBound;
    ///
    /// let mut value = 6u32;
    /// value.decrement_ref();
    /// assert_eq!(value, 5);
    /// ```
    fn decrement_ref(&mut self);
}

/// Implements `AdjacentBound` for a type using the specified value to
/// represent an increment of "one".
///
/// For numeric types $one should be 1, however for other types this
/// will be up the implementor, for dates $one may be a single day.
#[macro_export]
macro_rules! adjacent_bound_impl {
    ($type:ty, $one:expr) => {
        impl crate::AdjacentBound for $type {
            fn is_immediately_before(&self, other: &Self) -> bool {
                *self == other.decrement()
            }

            fn is_immediately_after(&self, other: &Self) -> bool {
                *self == other.increment()
            }

            fn increment(&self) -> Self {
                self.to_owned() + $one
            }

            fn decrement(&self) -> Self {
                self.to_owned() - $one
            }

            fn increment_ref(&mut self) {
                *self = self.increment();
            }

            fn decrement_ref(&mut self) {
                *self = self.decrement();
            }
        }
    };
}

macro_rules! adjacent_bound_impl_and_wrapping {
    ($type:ty, $one:expr) => {
        adjacent_bound_impl!($type, $one);
        adjacent_bound_impl!(::std::num::Wrapping<$type>, ::std::num::Wrapping($one));
    };
}

adjacent_bound_impl_and_wrapping!(u8, 1u8);
adjacent_bound_impl_and_wrapping!(i8, 1i8);

adjacent_bound_impl_and_wrapping!(u16, 1u16);
adjacent_bound_impl_and_wrapping!(i16, 1i16);

adjacent_bound_impl_and_wrapping!(u32, 1u32);
adjacent_bound_impl_and_wrapping!(i32, 1i32);

adjacent_bound_impl_and_wrapping!(u64, 1u64);
adjacent_bound_impl_and_wrapping!(i64, 1i64);

adjacent_bound_impl_and_wrapping!(usize, 1usize);
adjacent_bound_impl_and_wrapping!(isize, 1isize);

adjacent_bound_impl_and_wrapping!(u128, 1u128);
adjacent_bound_impl_and_wrapping!(i128, 1i128);

#[cfg(test)]
mod tests {
    use crate::AdjacentBound;
    use std::num::Wrapping;

    #[test]
    fn overflow_wrapping_tests() {
        assert!(Wrapping(u32::max_value()).is_immediately_before(&Wrapping(0)));
        assert!(Wrapping(0).is_immediately_after(&Wrapping(u32::max_value())));
    }
}

#[cfg(feature = "chrono")]
mod chrono_impl {
    use chrono::{Date, Duration, FixedOffset, Local, NaiveDate, Utc};

    adjacent_bound_impl!(NaiveDate, Duration::days(1));

    adjacent_bound_impl!(Date<Utc>, Duration::days(1));
    adjacent_bound_impl!(Date<FixedOffset>, Duration::days(1));
    adjacent_bound_impl!(Date<Local>, Duration::days(1));
}

#[cfg(feature = "chrono")]
pub use self::chrono_impl::*;

#[cfg(feature = "extprim")]
mod extprim_impl {
    use extprim::i128::i128;
    use extprim::u128::u128;

    adjacent_bound_impl!(i128, i128::one());
    adjacent_bound_impl!(u128, u128::one());
}

#[cfg(feature = "extprim")]
pub use self::extprim_impl::*;

#[cfg(feature = "bigint")]
mod num_bigint_impl {
    use num_bigint::{BigInt, BigUint};
    use num_traits::identities::One;

    adjacent_bound_impl!(BigInt, BigInt::one());
    adjacent_bound_impl!(BigUint, BigUint::one());
}

#[cfg(feature = "bigint")]
pub use self::num_bigint_impl::*;