RKTK API Docs RKTK Home Repo

rktk/hooks/
mod.rs

1//! Hooks are used to customize the behavior of the application.
2
3use interface::*;
4
5pub mod interface;
6pub use empty_hooks::create_empty_hooks;
7
8/// Hooks that can be passed to [`crate::task::start`] function.
9/// See earch trait's documentation for more information.
10pub struct Hooks<CH: CommonHooks, MH: MasterHooks, SH: SlaveHooks, RH: RgbHooks> {
11    pub common: CH,
12    pub master: MH,
13    pub slave: SH,
14    pub rgb: RH,
15}
16
17/// Makes it easier to pass around all hooks as a single type.
18///
19/// Without this trait, users would need to write out the full type of hooks like: `Hooks<impl
20/// CommonHooks, impl MasterHooks, impl SlaveHooks, impl RgbHooks>`.
21/// And this trait is implemented for `Hooks<CH, MH, SH, RH>` so users can just write `impl
22/// AllHooks`.
23///
24/// Though this trait is not sealed, it is not recommended to implement this trait for your own
25/// types.
26pub trait AllHooks {
27    type Common: CommonHooks;
28    type Master: MasterHooks;
29    type Slave: SlaveHooks;
30    type Rgb: RgbHooks;
31
32    fn destructure(self) -> Hooks<Self::Common, Self::Master, Self::Slave, Self::Rgb>;
33}
34
35impl<CH: CommonHooks, MH: MasterHooks, SH: SlaveHooks, RH: RgbHooks> AllHooks
36    for Hooks<CH, MH, SH, RH>
37{
38    type Common = CH;
39    type Master = MH;
40    type Slave = SH;
41    type Rgb = RH;
42
43    fn destructure(self) -> Hooks<CH, MH, SH, RH> {
44        self
45    }
46}
47
48/// Collection of sender/receiver that can be used with hooks.
49pub mod channels {
50    pub use crate::task::channels::*;
51}
52
53/// Collection of empty hooks and utility functions.
54pub mod empty_hooks {
55    use crate::hooks::AllHooks;
56
57    use super::{
58        Hooks, MasterHooks, SlaveHooks,
59        interface::{CommonHooks, RgbHooks},
60    };
61
62    pub struct EmptyCommonHooks;
63    impl CommonHooks for EmptyCommonHooks {}
64
65    pub struct EmptyMasterHooks;
66    impl MasterHooks for EmptyMasterHooks {}
67
68    pub struct EmptySlaveHooks;
69    impl SlaveHooks for EmptySlaveHooks {}
70
71    pub struct EmptyRgbHooks;
72    impl RgbHooks for EmptyRgbHooks {}
73
74    pub const fn create_empty_hooks() -> impl AllHooks {
75        Hooks {
76            common: EmptyCommonHooks,
77            master: EmptyMasterHooks,
78            slave: EmptySlaveHooks,
79            rgb: EmptyRgbHooks,
80        }
81    }
82}