- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.9k
Add key_mut method to BTreeMap entries #112896
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
Conversation
| r? @m-ou-se (rustbot has picked a reviewer for you, use r? to override) | 
      
        
              This comment has been minimized.
        
        
      
    
  This comment has been minimized.
bad01d4    to
    41c5821      
    Compare
  
    | (side note, apparently  | 
      
        
              This comment has been minimized.
        
        
      
    
  This comment has been minimized.
41c5821    to
    be04e90      
    Compare
  
    be04e90    to
    7429e0c      
    Compare
  
    7429e0c    to
    0e444d5      
    Compare
  
    0e444d5    to
    09bba58      
    Compare
  
    09bba58    to
    b356c09      
    Compare
  
    b356c09    to
    be92783      
    Compare
  
    | Reposting my comment from #118208 (comment) here slightly adapted as I believe it is similarly relevant: Would it not be a better idea to rather than creating this  I suppose the disadvantage would be that performance wise you would always do up to two comparisons per key replacement. Perhaps then we could also add an  | 
| I could add that, but the main reason why I prefer a  I also am not sure how to perform those checks in the code at the moment, but I could probably figure it out. | 
| Hmm, that use-case sounds odd, why would you have data in the key not related to the  | 
| Basically the primary case that was brought up when making this API was range maps, where your keys are ranges of values instead of values themselves. A common way of implementing this is to make your  So, having to re-check that the start of the range here is valid when that's an invariant that must be upheld here anyway doesn't really help. And since you're probably going to make breaking this invariant  But I also am making a lot of assumptions here, so, your argument is probably also valid. It's just not what was on my mind when writing it! Also, it's worth adding that you're right that these bits could be stored in the value instead. It's just easier to make the map go from  | 
be92783    to
    079cca2      
    Compare
  
    | 
 This is going back a bit, so my memory is hazy, but I think I tried both in the  However, and I know this is a bit of a tangent to this work, I think all those concerns would be obviated by a "visitor" style search method that accepts a callback to decide where to go next ("found", "ascend", "descend right", etc.). Then the consumer of the API could use whatever information they want to decide where to go next. I don't know if that would be too low-level to expose in the public API but I've often found myself wishing I had something like that. | 
| It's also worth mentioning that any "coalescing" behaviour for map keys could also benefit from  If we have  Granted, there are multiple cases to be made for reducing unsafe code and not ever doing key mutation, instead preferring to replace keys, but at least for the sake of flexibility and replacing the existing proposed key mutation methods for cursors, this feels like a reasonable compromise. We can litigate whether key mutation APIs should really exist before this is stabilised, since at least for now, they do exist and people are using them. | 
| 
 I completely agree which is why I made https://github.com/ripytide/btree_monstrousity which essentially just removes the  | 
079cca2    to
    825eef4      
    Compare
  
    | /// # Safety | ||
| /// | ||
| /// Mutating this key *does not* change the position of the entry, meaning that any mutation | ||
| /// should preserve the ordering of the key with respect to all the others in the map. | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| /// should preserve the ordering of the key with respect to all the others in the map. | |
| /// must preserve the ordering of the key with respect to all the others in the map. | 
| @clarfonthey Could you please change the name to something like  That would leave more room for some potential future checked version, which couldn't return  | 
| So, I am fine with that change, but after thinking about it a bit more, it feels probably better to do something like  I think we might be able to do something that is both sound and allows an unchecked version, which I think would presumably bypass  Since this feels a bit more complicated than the existing method, I'll also draft up an ACP for it. | 
| @rustbot waiting-on-ACP | 
| @rustbot ACP | 
825eef4    to
    88fda75      
    Compare
  
    | Gonna just close this since it doesn't match the current ACP. If the ACP gets accepted I'll open a new PR with the implementation. | 
This implements part of the proposal in this comment to improve the
BTreeMapcursor API.I'm not a huge fan of including an entire
Ordimplementation in a doc example but this feels like the best way to show off how to use these methods. Definitely open to feedback on that.For now, I've included the cursor API tracking issue as the tracking issue for these but a separate feature flag, since I feel these are technically disjoint from it. Would be open to separating it into a different tracking issue.
Also open to feedback on the safety guarantee for
VacantEntry. Definitely open to changing that, but I figured being the most cautious upfront would be good, since we can always loosen the guarantees later.ACP: rust-lang/libs-team#356