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
use std::sync::atomic::Ordering::SeqCst;
use std::{num::NonZeroU64, sync::atomic::AtomicU64};

/// this is the handle that you use to expire scheduled events.
#[derive(Hash, Eq, PartialEq, Debug, Copy)]
pub struct ExpireHandle(NonZeroU64);

impl ExpireHandle {
    unsafe fn new_unchecked(value: u64) -> Self {
        ExpireHandle(NonZeroU64::new_unchecked(value))
    }
}

impl Clone for ExpireHandle {
    fn clone(&self) -> Self {
        ExpireHandle(self.0)
    }
}

#[derive(Debug)]
pub(super) struct ExpireHandleFactory(AtomicU64);

impl ExpireHandleFactory {
    pub fn new() -> Self {
        ExpireHandleFactory(AtomicU64::from(1))
    }

    pub fn next(&self) -> ExpireHandle {
        unsafe { ExpireHandle::new_unchecked(self.0.fetch_add(1, SeqCst)) }
    }
}

impl Clone for ExpireHandleFactory {
    fn clone(&self) -> Self {
        let internal = self.0.load(SeqCst);
        ExpireHandleFactory(AtomicU64::from(internal))
    }
}