egui_task_manager/execution/
handler.rs1use std::any::Any;
2
3use crate::any::{HigherKinded, IntoAny};
4
5pub type AnyHandler<'h> = Handler<'h, Box<dyn Any + Send>>;
7
8pub struct Handler<'h, T>(Box<dyn FnMut(T) + 'h>);
10
11impl<'h, T: 'static> Handler<'h, T> {
12 pub fn new(handler: impl FnMut(T) + 'h) -> Self {
14 Self(Box::new(handler))
15 }
16
17 pub fn apply(&mut self, value: T) {
19 (self.0)(value)
20 }
21}
22
23impl<'h, T> HigherKinded for Handler<'h, T> {
24 type T<A> = Handler<'h, A>;
25}
26
27impl<'h, T> IntoAny for Handler<'h, T>
28where
29 T: 'static,
30{
31 fn into_any(mut self) -> Self::T<Box<dyn Any + Send>> {
32 let handler = Box::new(move |boxed_any: Box<dyn Any + Send>| {
33 let reference = boxed_any.downcast::<T>().unwrap();
34 (self.0)(*reference)
35 });
36
37 Handler(handler)
38 }
39}