bevy
bevy copied to clipboard
feature: Derive Hash for KeyboardInput.
Objective
Derive Hash for KeyboardInput.
Problem
I was writing code to take crossterm events and republish them as bevy input events. One scenario requires I check if the same key press was happening repeatedly; in a regular terminal we don't get key released events, so I was simulating them.
I was surprised to find that I couldn't put KeyboardInput into a HashSet.
Work Around
My work around was to add a new type that implemented Hash.
#[derive(Deref, DerefMut, PartialEq, Eq)]
struct KeyInput(KeyboardInput);
impl Hash for KeyInput {
fn hash<H>(&self, state: &mut H)
where
H: Hasher,
{
self.key_code.hash(state);
self.logical_key.hash(state);
self.state.hash(state);
self.window.hash(state);
}
}
Solution
A better solution since all members of KeyboardInput implement Hash is to have it derive Hash as well.
Testing
My newtype solution works for its purpose.