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
use super::transfer_frame::FrameCount;

#[derive(Debug, Default)]
pub struct Synchronizer {
    counter: Option<FrameCount>,
}

impl Synchronizer {
    pub fn next(&mut self, frame_count: FrameCount) -> Result<(), FrameCount> {
        if let Some(counter) = self.counter {
            let is_contiguous = frame_count.is_next_to(counter);
            self.counter = Some(frame_count);
            if is_contiguous {
                Ok(())
            } else {
                Err(counter.next())
            }
        } else {
            self.counter = Some(frame_count);
            Ok(())
        }
    }

    pub fn reset(&mut self) {
        self.counter = None;
    }
}