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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use std::any::Any;

use super::{
    context::Context,
    dispatcher::{DispatchError, DispatchedData, DispatchedSendData, Dispatcher, DispatcherArgs},
};

pub struct BoxedData {
    data: Box<dyn Any>,
}

impl BoxedData {
    pub fn new(data: impl Any) -> Self {
        Self {
            data: Box::new(data),
        }
    }
    pub fn downcast_mut<T: 'static>(&mut self) -> Option<&mut T> {
        self.data.downcast_mut()
    }
    pub fn downcast_ref<T: 'static>(&self) -> Option<&T> {
        self.data.downcast_ref()
    }
    fn downcast<T: 'static>(self) -> Option<T> {
        Some(*(self.data.downcast().ok()?))
    }
}

pub struct BoxedAsyncData {
    data: Box<dyn Any + Send + Sync>,
}

impl BoxedAsyncData {
    pub fn new(data: impl Any + Send + Sync) -> Self {
        Self {
            data: Box::new(data),
        }
    }
    pub fn downcast_mut<T: 'static>(&mut self) -> Option<&mut T> {
        self.data.downcast_mut()
    }
    pub fn downcast_ref<T: 'static>(&self) -> Option<&T> {
        self.data.downcast_ref()
    }

    fn downcast<T: 'static>(self) -> Option<T> {
        Some(*(self.data.downcast().ok()?))
    }
}

pub trait ExclusiveSystem: Dispatcher<BoxedData, Context> {
    type Query: DispatchedData;

    fn run(&mut self, ctx: &Context, comps: &mut Self::Query);
}

impl<T: ExclusiveSystem> Dispatcher<BoxedData, Context> for T {
    fn dispatch(&mut self, args: &mut DispatcherArgs<'_>) -> Result<BoxedData, DispatchError> {
        match <T::Query as DispatchedData>::dispatch(args) {
            Ok(data) => Ok(BoxedData::new(data)),
            Err(err) => Err(err),
        }
    }

    fn try_run<'a>(&mut self, args: &Context, b: BoxedData) -> Result<(), DispatchError> {
        let Some(data) = b.downcast::<<T::Query as DispatchedData>::Target>() else {
            return Err(DispatchError);
        };
        self.run(
            args,
            &mut <T::Query as DispatchedData>::from_target_to_data(data),
        );
        Ok(())
    }
}

pub trait AsyncSystem: Dispatcher<BoxedAsyncData, Context> + Send + Sync {
    type Query: DispatchedSendData;

    fn run(&mut self, ctx: &Context, comps: &mut <Self as AsyncSystem>::Query);
}

impl<T: AsyncSystem> Dispatcher<BoxedAsyncData, Context> for T {
    fn dispatch(&mut self, args: &mut DispatcherArgs<'_>) -> Result<BoxedAsyncData, DispatchError> {
        match <T::Query as DispatchedSendData>::dispatch(args) {
            Ok(data) => Ok(BoxedAsyncData::new(data)),
            Err(err) => Err(err),
        }
    }

    fn try_run<'a>(&mut self, args: &Context, b: BoxedAsyncData) -> Result<(), DispatchError> {
        let Some(data) = b.downcast::<<T::Query as DispatchedSendData>::Target>() else {
            return Err(DispatchError);
        };
        self.run(
            args,
            &mut <T::Query as DispatchedSendData>::from_target_to_data(data),
        );
        Ok(())
    }
}

// pub trait IntoAsyncSystem<Q: DispatchedSendData> {
//     fn into_async_system(self) -> Box<dyn AsyncSystem<Query = Q>>;
// }

// impl<Sys: AsyncSystem> IntoAsyncSystem<Sys::Query> for Sys {
//     fn into_async_system(self) -> Box<dyn AsyncSystem<Query = Sys::Query>> {
//         Box::new(self)
//     }
// }

// pub trait IntoExclusiveSystem<Q: DispatchedData> {
//     fn into_exclusive_system(self) -> Box<dyn ExclusiveSystem<Query = Q>>;
// }

// impl<Sys: ExclusiveSystem> IntoExclusiveSystem<Sys::Query> for Sys {
//     fn into_exclusive_system(self) -> Box<dyn ExclusiveSystem<Query = Sys::Query>> {
//         Box::new(self)
//     }
// }

mod tests {

    #[test]
    #[allow(unused)]
    fn test() {
        use crate::components::component::{Component, ComponentCollection};

        use crate::dispatchers::context::Context;

        struct A;
        impl Component for A {}

        // fn a(a: &Context, b: &ComponentCollection<A>) {}
        let a = |a: &Context, b: &ComponentCollection<A>| {};
    }
}