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
// License: see LICENSE file at root directory of `master` branch

//! # Range type

#[allow(deprecated)]
use crate::Ops;

/// # Helper for [`Range`][crate::Range]
///
/// [crate::Range]: struct.Range.html
#[deprecated(note="for internal use only")]
#[allow(deprecated)]
pub trait RangeType<U>: Ops where U: Ops {

    /// # _Estimates_ size from this value to one other
    fn estimate_size(&self, other: Self) -> U;

}

macro_rules! impl_range_type_for_signed_integers {
    ($($signed: ty, $unsigned: ty,)+) => {
        $(
            #[allow(deprecated)]
            impl RangeType<$unsigned> for $signed {

                fn estimate_size(&self, other: Self) -> $unsigned {
                    if self == &other {
                        return 1;
                    }

                    let min = (*self).min(other);
                    let max = (*self).max(other);

                    let tmp = match min >= 0 && max >= 0 {
                        true => max as $unsigned - min as $unsigned,
                        false => {
                            let tmp = match min == Self::min_value() {
                                true => Self::max_value() as $unsigned + 1,
                                false => min.abs() as $unsigned,
                            };
                            match min < 0 && max < 0 {
                                true => tmp - max.abs() as $unsigned,
                                false => tmp + max as $unsigned,
                            }
                        },
                    };
                    tmp.saturating_add(1)
                }

            }
        )+
    };
}

impl_range_type_for_signed_integers! {
    i8, u8,
    i16, u16,
    i32, u32,
    i64, u64,
    i128, u128,
    isize, usize,
}

macro_rules! impl_range_type_for_unsigned_integers {
    ($($unsigned: ty,)+) => {
        $(
            #[allow(deprecated)]
            impl RangeType<$unsigned> for $unsigned {

                fn estimate_size(&self, other: Self) -> $unsigned {
                    match self == &other {
                        true => 1,
                        false => ((*self).max(other) - (*self).min(other)).saturating_add(1),
                    }
                }

            }
        )+
    };
}

impl_range_type_for_unsigned_integers! {
    u8, u16, u32, u64, u128, usize,
}