egui_task_manager/execution/
handler.rs

1use std::any::Any;
2
3use crate::any::{HigherKinded, IntoAny};
4
5/// Handler that has `Box<dyn Any + Send>` as a type parameter.
6pub type AnyHandler<'h> = Handler<'h, Box<dyn Any + Send>>;
7
8/// Handler that is used to handle task's result.
9pub struct Handler<'h, T>(Box<dyn FnMut(T) + 'h>);
10
11impl<'h, T: 'static> Handler<'h, T> {
12    /// Creates a new handle.
13    pub fn new(handler: impl FnMut(T) + 'h) -> Self {
14        Self(Box::new(handler))
15    }
16
17    /// Applies handle on some value
18    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}