wheel_timer2/
handle.rs

1use std::any::Any;
2use std::time::Duration;
3
4use crossbeam_channel::Sender;
5
6use crate::callback::{BoxedCallback, Callback};
7
8/// This means the wheel has been dropped
9#[derive(Clone, Copy, Debug)]
10pub struct DanglingAddHandle;
11
12/// Allow adding tasks while wheel is running.
13/// These tasks will be added to the wheel in the next tick.
14#[derive(Clone, Debug)]
15pub struct AddHandle(pub(crate) Sender<(BoxedCallback, Duration, Box<dyn Any + Send>)>);
16
17impl AddHandle {
18    /// Add tasks to after a certain interval
19    #[inline]
20    pub fn add<C>(&self, cb: C, dur: Duration) -> Result<(), DanglingAddHandle>
21    where
22        C: Callback<()> + Send + 'static,
23    {
24        self.0
25            .send((cb.boxed(), dur, Box::new(())))
26            .map_err(|_| DanglingAddHandle)
27    }
28
29    // like `add` but with context
30    #[inline]
31    pub fn add_with_ctx<C, T>(&self, cb: C, dur: Duration, ctx: T) -> Result<(), DanglingAddHandle>
32    where
33        C: Callback<(T,)> + Send + 'static,
34        T: Send + 'static,
35    {
36        self.0
37            .send((cb.boxed(), dur, Box::new(ctx)))
38            .map_err(|_| DanglingAddHandle)
39    }
40}