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
//! Generic range type abstraction that allows
//! ranges to be handled a little more generically.

use std::ops::{Range, RangeFrom, RangeFull, RangeTo};

/// Abstracts the std range types.
///
/// Based upon the nightly `RangeArgument` trait.
pub trait RangeArg<T> {
    /// Start index bound.
    fn start(&self) -> Option<&T>;
    /// End index bound.
    fn end(&self) -> Option<&T>;
}

impl<T> RangeArg<T> for Range<T> {
    fn start(&self) -> Option<&T> {
        Some(&self.start)
    }
    fn end(&self) -> Option<&T> {
        Some(&self.end)
    }
}

impl<T> RangeArg<T> for RangeTo<T> {
    fn start(&self) -> Option<&T> {
        None
    }
    fn end(&self) -> Option<&T> {
        Some(&self.end)
    }
}

impl<T> RangeArg<T> for RangeFrom<T> {
    fn start(&self) -> Option<&T> {
        Some(&self.start)
    }
    fn end(&self) -> Option<&T> {
        None
    }
}

impl<T> RangeArg<T> for RangeFull {
    fn start(&self) -> Option<&T> {
        None
    }
    fn end(&self) -> Option<&T> {
        None
    }
}

impl<T> RangeArg<T> for (Option<T>, Option<T>) {
    fn start(&self) -> Option<&T> {
        self.0.as_ref()
    }
    fn end(&self) -> Option<&T> {
        self.1.as_ref()
    }
}