Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance: pack mods & key into a u32 as id instead of hashing #104

Merged
merged 3 commits into from
Sep 9, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions src/hotkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub struct HotKey {
/// The hotkey key.
pub key: Code,
/// The hotkey id.
pub id: u32,
pub id: u64,
}

#[cfg(feature = "serde")]
Expand Down Expand Up @@ -92,20 +92,13 @@ impl HotKey {
}

let mut hotkey = Self { mods, key, id: 0 };
hotkey.id = hotkey.generate_hash();
hotkey.id = (mods.bits() as u64) << 32 | key as u64;
hotkey
mixy1 marked this conversation as resolved.
Show resolved Hide resolved
}

fn generate_hash(&self) -> u32 {
let hotkey_str = self.into_string();
let mut hasher = std::collections::hash_map::DefaultHasher::new();
hotkey_str.hash(&mut hasher);
std::hash::Hasher::finish(&hasher) as u32
}

/// Returns the id associated with this hotKey
/// which is a hash of the string represention of modifiers and key within this hotKey.
pub fn id(&self) -> u32 {
pub fn id(&self) -> u64 {
self.id
}

Expand Down Expand Up @@ -476,4 +469,4 @@ fn test_equality() {
&& h4.id() == h5.id()
&& h5.id() != h6.id()
);
}
}
Loading