Repository navigation
fix(event): make KeyEvent ordering agree with its equality - #1159
Open
NgoQuocViet2001 wants to merge 1 commit into
Open
NgoQuocViet2001 wants to merge 1 commit into
NgoQuocViet2001 wants to merge 1 commit into
Conversation
`KeyEvent` compares and hashes its case-normalized form, so
`Char('D')`, `Char('D')` with `SHIFT` and `Char('d')` with `SHIFT` are
equal. `PartialOrd` and `Ord` were still derived and compared the raw
fields, so those equal events ordered as `Less` or `Greater`, and a
`BTreeMap` or `BTreeSet` keyed by `KeyEvent` or `Event` missed
Shift+letter bindings that a `HashMap` found.
Implement `Ord` on the same normalized fields, in the same field order
as the old derive, and have `PartialOrd` delegate to it. `Event`'s
derived ordering picks this up for `Event::Key`.
There was a problem hiding this comment.
🟢 Approval recommended
The focused change aligns ordering with equality and hashing, preserves field precedence, and includes regression coverage with no blocking issues identified.
No open issues 🎉
What changed in this PR
This PR aligns KeyEvent ordering with its existing case-normalized equality and hashing, fixing ordered keybinding lookups while preserving ordering for unchanged events.
Changes:
- Replace derived ordering with normalized field comparison, with
PartialOrddelegating toOrd. - Add regression coverage for equivalent keys,
Event::Keyordering, andBTreeSetlookups. - Document the fix in the changelog.
| File | Description |
|---|---|
src/event/key_event.rs |
Implement consistent ordering and add regression tests. |
CHANGELOG.md |
Describe the ordering mismatch and affected keybinding lookups. |
Review effort: Balanced
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
KeyEventcompares and hashes its case-normalized form (#541), soKeyEvent::new(Char('D'), NONE),(Char('D'), SHIFT)and(Char('d'), SHIFT)are all equal.PartialOrdandOrdwere still derived on the raw fields, so('D', NONE).cmp(&('D', SHIFT))isLessand('d', SHIFT).cmp(&('D', SHIFT))isGreater. ABTreeMap<KeyEvent, _>holdingKeyCode::Char('D').into()returnsNonefor the('D', SHIFT)event reported for Shift+d, while aHashMapfinds it.Event::Keyinherits this through its derivedOrd.This implements
Ordon the same normalized fields thateqandhashuse, in the old derive's field order, and makesPartialOrddelegate to it.Ordstays, since #951 added it for ordered keybinding maps, and the change is not breaking. Events thatnormalize_caseleaves unchanged keep their relative order.('d', SHIFT)and('D', NONE)now sort together with the equal('D', SHIFT). The fields are destructured exhaustively, as ineqandhash, so a new field such as the one in #1074 must be added tocmptoo.Ran:
cargo test --locked --lib --no-default-features --features events -- --test-threads 1 key_event: the new test fails on master (GreatervsEqual) and passes with the fixcargo test --locked --all-targets --all-features -- --test-threads 1: all pass (93 lib tests)cargo test --locked --doc --all-features: 63 passedChecked:
cargo clippy --locked --all-targets --all-features -- -D warningsand the same with--no-default-features --features events: cleancargo fmt --all -- --checkandRUSTDOCFLAGS='-D warnings' cargo doc --locked --all-features --no-deps: clean