1use std::any::Any;
2use std::time::Duration;
3
4use crossbeam_channel::Sender;
5
6use crate::callback::{BoxedCallback, Callback};
7
8#[derive(Clone, Copy, Debug)]
10pub struct DanglingAddHandle;
11
12#[derive(Clone, Debug)]
15pub struct AddHandle(pub(crate) Sender<(BoxedCallback, Duration, Box<dyn Any + Send>)>);
16
17impl AddHandle {
18 #[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 #[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}