smart_keymap/tuples.rs
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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
use core::fmt::Debug;
use core::ops::{Index, IndexMut};
use crate::key;
/// A tuple struct for 1 key.
#[derive(Debug)]
pub struct Keys1<
K0: key::Key<Context = Ctx, Event = Ev, PressedKey = PK>,
Ctx,
Ev,
PK,
const M: usize = { crate::key::MAX_KEY_EVENTS },
>(K0);
impl<
K0: key::Key<Context = Ctx, Event = Ev, PressedKey = PK> + Copy,
Ctx,
Ev,
PK,
const M: usize,
> Keys1<K0, Ctx, Ev, PK, M>
{
/// Constructs a KeysN for the given tuple.
pub const fn new((k0,): (K0,)) -> Self {
Keys1(k0)
}
}
impl<
K0: key::Key<Context = Ctx, Event = Ev, PressedKey = PK> + 'static,
Ctx,
Ev,
PK,
const M: usize,
> Index<usize> for Keys1<K0, Ctx, Ev, PK, M>
{
type Output = dyn key::Key<Context = Ctx, Event = Ev, PressedKey = PK>;
fn index(&self, idx: usize) -> &Self::Output {
match idx {
0 => &self.0,
_ => panic!("Index out of bounds"),
}
}
}
impl<
K0: key::Key<Context = Ctx, Event = Ev, PressedKey = PK> + 'static,
Ctx,
Ev,
PK,
const M: usize,
> IndexMut<usize> for Keys1<K0, Ctx, Ev, PK, M>
{
fn index_mut(&mut self, idx: usize) -> &mut Self::Output {
match idx {
0 => &mut self.0,
_ => panic!("Index out of bounds"),
}
}
}
// Use seq_macro's seq! to generate Keys2, Keys3, etc.
macro_rules! define_keys {
($n:expr) => {
paste::paste! {
seq_macro::seq!(I in 0..$n {
/// A tuple struct for some number of keys.
#[derive(core::fmt::Debug)]
pub struct [<Keys $n>]<
#(
K~I: crate::key::Key<Context = Ctx, Event = Ev, PressedKey = PK>,
)*
Ctx,
Ev,
PK,
const M: usize = { crate::key::MAX_KEY_EVENTS },
>(
#(
K~I,
)*
);
impl<
#(
K~I: crate::key::Key<Context = Ctx, Event = Ev, PressedKey = PK> + Copy,
)*
Ctx,
Ev,
PK,
const M: usize,
> [<Keys $n>]<
#(K~I,)*
Ctx, Ev, PK, M
>
{
/// Constructs a KeysN tuple struct with the given tuple.
pub const fn new((
#(k~I,)*
): (
#(K~I,)*
)) -> Self {
[<Keys $n>](
#(
(k~I),
)*
)
}
}
impl<
#(
K~I: crate::key::Key<Context = Ctx, Event = Ev, PressedKey = PK> + 'static,
)*
Ctx,
Ev,
PK,
const M: usize,
> core::ops::Index<usize> for [<Keys $n>]<
#(K~I,)*
Ctx, Ev, PK, M
>
{
type Output = dyn crate::key::Key<Context = Ctx, Event = Ev, PressedKey = PK>;
fn index(&self, idx: usize) -> &Self::Output {
match idx {
#(
I => &self.I,
)*
_ => panic!("Index out of bounds"),
}
}
}
impl<
#(
K~I: crate::key::Key<Context = Ctx, Event = Ev, PressedKey = PK> + 'static,
)*
Ctx,
Ev,
PK,
const M: usize,
> core::ops::IndexMut<usize> for [<Keys $n>]<
#(K~I,)*
Ctx, Ev, PK, M
>
{
fn index_mut(&mut self, idx: usize) -> &mut Self::Output {
match idx {
#(
I => &mut self.I,
)*
_ => panic!("Index out of bounds"),
}
}
}
});
}
};
}
pub(crate) use define_keys;
define_keys!(2);
define_keys!(4);