pendulum/
pendulum.rs

1use error::PendulumResult;
2use std::time::Duration;
3
4/// Identifier for objects inserted into a `Pendulum`.
5#[derive(Copy, Clone)]
6pub struct Token {
7    token: usize
8}
9
10pub fn create_token(token: usize) -> Token {
11    Token{ token: token }
12}
13
14pub fn retrieve_token(token: Token) -> usize {
15    token.token
16}
17
18//--------------------------------------------------------------//
19
20/// Trait for working with generic timer wheel implementations.
21pub trait Pendulum<T> {
22    /// Insert a timeout with the given duration and the given item into the `Pendulum`.
23    fn insert_timeout(&mut self, timeout: Duration, item: T) -> PendulumResult<Token, T>;
24
25    /// Removes the timeout corresponding with the given `Token`, if one exists.
26    fn remove_timeout(&mut self, token: Token) -> Option<T>;
27
28    /// Retrieve the next expired timeout from the `Pendulum`.
29    ///
30    /// This is a non-blocking operation.
31    fn expired_timeout(&mut self) -> Option<T>;
32
33    /// Tick the `Pendulum` once.
34    fn tick(&mut self);
35
36    /// Configured tick duration for this `Pendulum`.
37    fn tick_duration(&self) -> Duration;
38
39    /// Configured max timeout capacity for this `Pendulum`.
40    fn max_capacity(&self) -> usize;
41
42    /// Configured maximum timeout for this `Pendulum`.
43    fn max_timeout(&self) -> Duration;
44}