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
use std::{thread, time};

pub struct Fence {
  duration: time::Duration,
  block_until: time::Instant,
}

/// Fence provides a timed rate limiter. It's useful for imposing a framerate
/// cap on a UI thread.
/// 
/// In a typical usage, place a fence at the end of a loop to control the
/// invocation rate.
impl Fence {

    /// Construct a fence from the specified seconds.
    pub fn from_secs(s: u64) -> Fence {
        Fence::from_duration(time::Duration::from_secs(s))
    }

    /// Construct a fence from the specified milliseconds.
    pub fn from_millis(m: u64) -> Fence {
        Fence::from_duration(time::Duration::from_millis(m))
    }

    /// Construct a fence from the given duration.
    pub fn from_duration(dur: time::Duration) -> Fence {
        Fence {
            duration: dur,
            block_until: time::Instant::now() + dur,
        }
    }

    /// Sleep the current thread until at least the specified passage of time.
    pub fn sleep(&mut self) {
        let now = time::Instant::now();
        if now < self.block_until {
          thread::sleep(self.block_until.duration_since(now))
        }
        self.block_until = time::Instant::now() + self.duration;
    }
}

#[cfg(test)]
mod tests {
    use std::time;
    use Fence;

    #[test]
    fn fence_blocks() {
        let fence_dur = time::Duration::from_millis(10);
        let mut f = Fence::from_duration(fence_dur);
        let before = time::Instant::now();
        
        f.sleep();
        let after = time::Instant::now();
        assert!(after >= before + fence_dur);
    }

    #[test]
    fn fence_rate_limits() {
        let fence_dur = time::Duration::from_millis(5);
        let mut f = Fence::from_duration(fence_dur);
        let before = time::Instant::now();

        let lim = 10;
        for _ in 0..lim {
          f.sleep()
        }
        let after = time::Instant::now();
        assert!(after >= before + fence_dur * lim);
    }
}