Skip to content

Commit 8a79185

Browse files
shanecelisalice-i-cecilejoshkaBD103
authored
feature: Derive Hash for KeyboardInput. (#14263)
# Objective Derive `Hash` for `KeyboardInput`. ## Problem I was [writing code](cxreiff/bevy_ratatui#13) 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. ```rust #[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. --------- Co-authored-by: Alice Cecile <[email protected]> Co-authored-by: Josh McKinney <[email protected]> Co-authored-by: BD103 <[email protected]>
1 parent f0ff7fb commit 8a79185

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

crates/bevy_input/src/keyboard.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,12 @@ use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
8888
///
8989
/// The event is consumed inside of the [`keyboard_input_system`]
9090
/// to update the [`ButtonInput<KeyCode>`](ButtonInput<KeyCode>) resource.
91-
#[derive(Event, Debug, Clone, PartialEq, Eq)]
92-
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
91+
#[derive(Event, Debug, Clone, PartialEq, Eq, Hash)]
92+
#[cfg_attr(
93+
feature = "bevy_reflect",
94+
derive(Reflect),
95+
reflect(Debug, PartialEq, Hash)
96+
)]
9397
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
9498
#[cfg_attr(
9599
all(feature = "serialize", feature = "bevy_reflect"),

0 commit comments

Comments
 (0)