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

Hash for hotkey id is unnecessary #101

Closed
mixy1 opened this issue Sep 8, 2024 · 1 comment · Fixed by #104
Closed

Hash for hotkey id is unnecessary #101

mixy1 opened this issue Sep 8, 2024 · 1 comment · Fixed by #104
Labels
enhancement New feature or request

Comments

@mixy1
Copy link
Contributor

mixy1 commented Sep 8, 2024

pub fn new(mods: Option<Modifiers>, key: Code) -> Self {
        let mut mods = mods.unwrap_or_else(Modifiers::empty);
        if mods.contains(Modifiers::META) {
            mods.remove(Modifiers::META);
            mods.insert(Modifiers::SUPER);
        }

        let mut hotkey = Self { mods, key, id: 0 };
        hotkey.id = hotkey.generate_hash();
        hotkey
    }

    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
    }

I'm curious why a hash is being used here rather than just leaving the base u32 value in, it's a guaranteed unique value + it'll always be 32 bits. Am I missing something?

i.e.

it should rather be

pub fn new(mods: Option<Modifiers>, key: Code) -> Self {
        let mut mods = mods.unwrap_or_else(Modifiers::empty);
        if mods.contains(Modifiers::META) {
            mods.remove(Modifiers::META);
            mods.insert(Modifiers::SUPER);
        }

        let mut hotkey = Self { mods, key, id: 0 };
        hotkey.id = mods as u32;
        hotkey
    }

If the id is related to modifiers I can just match on the concatenation of the flags again after throughout the codebase. Wouldn't mind opening a pr.

@amrbashir amrbashir added the enhancement New feature or request label Sep 9, 2024
@amrbashir
Copy link
Member

@mixy1 the hash does not just consist of modifiers, it also includes the key.

I think there maybe a way to avoid hashing by using a u64, 32 bit to store the modifiers id and the other 32 to store the key code (might need a function to map key Code to a u32).

Feel free to work on this if you want, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants