mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 07:04:33 +00:00
8a79185880
# Objective Derive `Hash` for `KeyboardInput`. ## Problem I was [writing code](https://github.com/joshka/bevy_ratatui/pull/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 <alice.i.cecile@gmail.com> Co-authored-by: Josh McKinney <joshka@users.noreply.github.com> Co-authored-by: BD103 <59022059+BD103@users.noreply.github.com> |
||
---|---|---|
.. | ||
bevy_a11y | ||
bevy_animation | ||
bevy_app | ||
bevy_asset | ||
bevy_audio | ||
bevy_color | ||
bevy_core | ||
bevy_core_pipeline | ||
bevy_derive | ||
bevy_dev_tools | ||
bevy_diagnostic | ||
bevy_dylib | ||
bevy_dynamic_plugin | ||
bevy_ecs | ||
bevy_encase_derive | ||
bevy_gilrs | ||
bevy_gizmos | ||
bevy_gltf | ||
bevy_hierarchy | ||
bevy_input | ||
bevy_internal | ||
bevy_log | ||
bevy_macro_utils | ||
bevy_math | ||
bevy_mikktspace | ||
bevy_pbr | ||
bevy_picking | ||
bevy_ptr | ||
bevy_reflect | ||
bevy_render | ||
bevy_scene | ||
bevy_sprite | ||
bevy_state | ||
bevy_tasks | ||
bevy_text | ||
bevy_time | ||
bevy_transform | ||
bevy_ui | ||
bevy_utils | ||
bevy_window | ||
bevy_winit |