rust-lang / rust-lang/rfcs

Make BTreeMap's from_sorted_iter public

Open
#1,805 4 comments 8 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

T-libs
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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.