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
use num_traits::WrappingSub;
#[derive(Debug, Default, Copy, Clone)]
pub struct DeltaEncoder<T> {
    current: T,
}
impl<T: WrappingSub + Copy> DeltaEncoder<T> {
    
    pub fn encode(&mut self, value: T) -> T {
        let delta = value.wrapping_sub(&self.current);
        self.current = value;
        delta
    }
}
#[derive(Clone, Debug)]
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct DeltaEncoderIter<I>
where
    I: Iterator,
    <I as Iterator>::Item: WrappingSub + Copy,
{
    iter: I,
    encoder: DeltaEncoder<I::Item>,
}
impl<I> Iterator for DeltaEncoderIter<I>
where
    I: Iterator,
    <I as Iterator>::Item: WrappingSub + Copy,
{
    type Item = I::Item;
    fn next(&mut self) -> Option<Self::Item> {
        Some(self.encoder.encode(self.iter.next()?))
    }
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.iter.size_hint()
    }
}
impl<I> ExactSizeIterator for DeltaEncoderIter<I>
where
    I: ExactSizeIterator,
    <I as Iterator>::Item: WrappingSub + Copy,
{
}
pub trait DeltaEncoderExt: Iterator
where
    <Self as Iterator>::Item: Default + Copy + WrappingSub,
{
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    fn deltas(self) -> DeltaEncoderIter<Self>
    where
        Self: Sized,
    {
        DeltaEncoderIter {
            iter: self,
            encoder: Default::default(),
        }
    }
}
impl<I> DeltaEncoderExt for I
where
    I: Iterator,
    <I as Iterator>::Item: Default + Copy + WrappingSub,
{
}