Repository navigation
fix: allow insert_at(len) when the key already exists - #81
Merged
martinohmann merged 1 commit intoOct 29, 2026
Merged
Conversation
insert_at removed the existing entry before inserting at index, so index == len was out of bounds for the shorter vector: it panicked, which the docs only allow for index > len, and lost the entry. Move the entry to the end instead, and check index > len before removing it.
Owner
|
Thanks for fixing this! |
Merged
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.
VecMap::insert_atdocuments a panic only forindex > len. With an existing key andindex == len, it removes the old entry first and then inserts into a vector that is one shorter, so it panics and the entry is gone:The fix checks
index > lenbefore anything is removed, and moves the entry to the end whenindex == len. The doc now says so. New keys andindex > lenbehave as before. Across maps of length 1 to 4, with every existing key and every index up tolen, only the 10 cases withindex == lenchange.If you'd rather keep the panic, like indexmap's
shift_insert, which rejectsindex == lenfor an existing key, the alternative is to assertindex < lenbefore removing and adjust the doc. That would still fix the lost entry.These pass:
cargo test --workspace --all-features,cargo fmt --check, andcargo docwith-D warnings. I didn't run miri, but the change has no unsafe code.Written with AI assistance (Claude); I have reviewed the change.