Make BTreeMap's from_sorted_iter public
Nobody has claimed this yet.
- Dominant language
- Markdown
- Stars
- 6.6k
- Forks
- 1.7k
- Avg merge
- 16h 14m
- Merged PRs (30d)
- 1
Description
Use case: I'm trying to merge BTreeMaps, but in the case of a duplicate key, I'd prefer to add the values instead of ignoring the first. To do this, I was planning on implementing a custom MergeIter, which seems simple enough. However, I encountered one problem: BTreeMap's from_sorted_iter is private, meaning that I cannot efficiently use this iterator to merge maps.
I think the reasoning behind this being private is that being passed an unsorted iterator would result in a logic error in the BTreeMap. However, it's already possible to cause this in safe code with an unreliable Cmp implementation.
If the function is made public, I'd recommend a couple of changes to make it ready for use without other internal functions. First, don't take self, but instead start with an empty map. Note that we might also consider just asserting that the map is empty to start with, as I think this change would require either an unnecessary BTreeMap creation or mem::uninitialized in BTreeMap::append. Second, add fix_right_edge() to the end of the function. This change shouldn't cause any problems for BTreeMap::append as it's already calling it afterwords.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating BTreeMap::from_sorted_iter and BTreeMap::append in the Rust implementation, then read how fix_right_edge() is used. Check the API and invariants around sorted input, initialization, and empty maps. Done means the requested public interface works for merging sorted entries without breaking BTreeMap::append or its safety assumptions.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100