smart_keymap/
tuples.rs

1use core::fmt::Debug;
2use core::ops::Index;
3
4use crate::key;
5
6/// A tuple struct for 1 key.
7#[derive(Debug)]
8pub struct Keys1<
9    K0: key::Key<Context = Ctx, Event = Ev, PendingKeyState = PKS, KeyState = KS>,
10    Ctx,
11    Ev,
12    PKS,
13    KS,
14    const M: usize = { crate::key::MAX_KEY_EVENTS },
15>(K0);
16
17impl<
18        K0: key::Key<Context = Ctx, Event = Ev, PendingKeyState = PKS, KeyState = KS> + Copy,
19        Ctx,
20        Ev,
21        PKS,
22        KS,
23        const M: usize,
24    > Keys1<K0, Ctx, Ev, PKS, KS, M>
25{
26    /// Constructs a KeysN for the given tuple.
27    pub const fn new((k0,): (K0,)) -> Self {
28        Keys1(k0)
29    }
30}
31
32impl<
33        K0: key::Key<Context = Ctx, Event = Ev, PendingKeyState = PKS, KeyState = KS> + 'static,
34        Ctx,
35        Ev,
36        PKS,
37        KS,
38        const M: usize,
39    > Index<usize> for Keys1<K0, Ctx, Ev, PKS, KS, M>
40{
41    type Output = dyn key::Key<Context = Ctx, Event = Ev, PendingKeyState = PKS, KeyState = KS>;
42
43    fn index(&self, idx: usize) -> &Self::Output {
44        match idx {
45            0 => &self.0,
46            _ => panic!("Index out of bounds"),
47        }
48    }
49}
50
51// Use seq_macro's seq! to generate Keys2, Keys3, etc.
52
53#[macro_export]
54/// Defines tuple structs KeysN for N keys, where N is the given expression.
55macro_rules! define_keys {
56    ($n:expr) => {
57        paste::paste! {
58            seq_macro::seq!(I in 0..$n {
59                /// A tuple struct for some number of keys.
60                #[derive(core::fmt::Debug)]
61                pub struct [<Keys $n>]<
62                    #(
63                        K~I: $crate::key::Key<Context = Ctx, Event = Ev, PendingKeyState = PKS, KeyState = KS>,
64                    )*
65                    Ctx,
66                    Ev,
67                    PKS,
68                    KS,
69                    const M: usize = { $crate::key::MAX_KEY_EVENTS },
70                >(
71                    #(
72                        K~I,
73                    )*
74                );
75
76                impl<
77                    #(
78                        K~I: $crate::key::Key<Context = Ctx, Event = Ev, PendingKeyState = PKS, KeyState = KS> + Copy,
79                    )*
80                    Ctx,
81                    Ev,
82                    PKS,
83                    KS,
84                    const M: usize,
85                > [<Keys $n>]<
86                    #(K~I,)*
87                    Ctx, Ev, PKS, KS, M
88                >
89                {
90                    /// Constructs a KeysN tuple struct with the given tuple.
91                    pub const fn new((
92                        #(k~I,)*
93                    ): (
94                        #(K~I,)*
95                    )) -> Self {
96                        [<Keys $n>](
97                            #(
98                                (k~I),
99                            )*
100                        )
101                    }
102                }
103
104                impl<
105                    #(
106                        K~I: $crate::key::Key<Context = Ctx, Event = Ev, PendingKeyState = PKS, KeyState = KS> + 'static,
107                    )*
108                    Ctx,
109                    Ev,
110                    PKS,
111                    KS,
112                    const M: usize,
113                > core::ops::Index<usize> for [<Keys $n>]<
114                    #(K~I,)*
115                    Ctx, Ev, PKS, KS, M
116                    >
117                {
118                    type Output = dyn $crate::key::Key<Context = Ctx, Event = Ev, PendingKeyState = PKS, KeyState = KS>;
119
120                    fn index(&self, idx: usize) -> &Self::Output {
121                        match idx {
122                            #(
123                                I => &self.I,
124                            )*
125                            _ => panic!("Index out of bounds"),
126                        }
127                    }
128                }
129            });
130        }
131    };
132}
133
134pub use define_keys;
135
136define_keys!(2);
137
138define_keys!(4);