Skip to content
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

Add key and value methods to DebugMap #2696

Merged
merged 5 commits into from
Jul 7, 2019
Merged

Add key and value methods to DebugMap #2696

merged 5 commits into from
Jul 7, 2019

Conversation

KodrAus
Copy link
Contributor

@KodrAus KodrAus commented May 1, 2019

Rendered

Add two new methods to std::fmt::DebugMap for writing the key and value part of a map entry separately:

impl<'a, 'b: 'a> DebugMap<'a, 'b> {
    pub fn key(&mut self, key: &dyn Debug) -> &mut Self;
    pub fn value(&mut self, value: &dyn Debug) -> &mut Self;
}

@KodrAus KodrAus added A-fmt Proposals relating to std::fmt and formatting macros. T-libs-api Relevant to the library API team, which will review and decide on the RFC. labels May 1, 2019
# Drawbacks
[drawbacks]: #drawbacks

The proposed `key` and `value` methods are't immediately useful for `Debug` implementors that are able to call `entry` instead. This creates a decision point where there wasn't one before. The proposed implementation is also going to be less efficient than the one that exists now because it introduces a few conditionals.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding efficiency, I would suggest testing this out with a PR and then doing a timer build to see how much perf regresses (that just affects compile times but it's better than nothing). With that data we can evaluate the drawback better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened up a PR (rust-lang/rust#60458) that we can use to check correctness and get some timings 👍

I don't expect the difference to be significant, since it amounts to a few conditionals but it'll be good to verify!

Copy link
Contributor Author

@KodrAus KodrAus May 13, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alrighty, I posted a quick benchmark in the PR, but also inlining the results here:

Before (current .entry()) After (.key().value())
36 ns/iter (+/- 7) 44 ns/iter (+/- 2)


- Write an alternative implementation of the format builders. The output from this alternative implementation would need to be kept reasonably in-sync with the one in the standard library. It doesn't change very frequently, but does from time to time. It would also have to take the same care as the standard library implementation to retain formatting flags when working with entries.
- Buffer keys and format them together with values when the whole entry is available. Unless the key is guaranteed to live until the value is supplied (meaning it probably needs to be `'static`) then the key will need to be formatted into a string first. This means allocating (though the cost could be amortized over the whole map) and potentially losing formatting flags when buffering.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A third option here is to simply not error when keys and values come alone. That might actually be useful in some weird semi-map semi-array cases. That would also be more efficient?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, that's an interesting thought 🤔 I think that might just mask incorrect implementations though. I imagine we'd be tracking the same additional state too so that we knew when a key or value called 'out of order' should be formatted in a set-like way rather than a map-like way.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah quite possibly.
Would be good to record this in the text of the RFC in any case. :)

pub fn finish(&mut self) -> fmt::Result {
// Make sure there isn't a partial entry
if self.has_key {
return Err(fmt::Error);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating a fmt::Error like this isn't allowed. From https://doc.rust-lang.org/nightly/std/fmt/index.html#formatting-traits:

a formatting implementation must and may only return an error if the passed-in Formatter returns an error.

As this indicates a programming error I think an explicit panic here would be correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! A panic seems reasonable.

@KodrAus
Copy link
Contributor Author

KodrAus commented May 26, 2019

CC @japaric in case you're interested, given your work on an alternative formatting stack.

@KodrAus
Copy link
Contributor Author

KodrAus commented May 28, 2019

Nominating for discussion at the next triage to get some more input from the wider libs team.

@alexcrichton
Copy link
Member

I'll go ahead an propose to merge, seems like a reasonable feature to me!

@rfcbot fcp merge

@rfcbot
Copy link
Collaborator

rfcbot commented May 28, 2019

Team member @alexcrichton has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Currently awaiting signoff of all team members in order to enter the final comment period. disposition-merge This RFC is in PFCP or FCP with a disposition to merge it. labels May 28, 2019
@rfcbot rfcbot added the final-comment-period Will be merged/postponed/closed in ~10 calendar days unless new substational objections are raised. label Jun 27, 2019
@rfcbot
Copy link
Collaborator

rfcbot commented Jun 27, 2019

🔔 This is now entering its final comment period, as per the review above. 🔔

@rfcbot rfcbot removed the proposed-final-comment-period Currently awaiting signoff of all team members in order to enter the final comment period. label Jun 27, 2019
@rfcbot
Copy link
Collaborator

rfcbot commented Jul 7, 2019

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

The RFC will be merged soon.

@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this RFC. and removed final-comment-period Will be merged/postponed/closed in ~10 calendar days unless new substational objections are raised. labels Jul 7, 2019
@KodrAus KodrAus merged commit 158eaea into rust-lang:master Jul 7, 2019
@KodrAus KodrAus deleted the debug_map_entry branch July 7, 2019 23:49
Centril added a commit to Centril/rust that referenced this pull request Jul 9, 2019
…hton

Add key and value methods to DebugMap

Implementation PR for an active (not approved) RFC: rust-lang/rfcs#2696.

Add two new methods to `std::fmt::DebugMap` for writing the key and value part of a map entry separately:

```rust
impl<'a, 'b: 'a> DebugMap<'a, 'b> {
    pub fn key(&mut self, key: &dyn Debug) -> &mut Self;
    pub fn value(&mut self, value: &dyn Debug) -> &mut Self;
}
```

I want to do this so that I can write a `serde::Serializer` that forwards to our format builders, so that any `T: Serialize` can also be treated like a `T: Debug`.
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Jan 28, 2020
…ue, r=alexcrichton

Stabilize the debug_map_key_value feature

RFC: rust-lang/rfcs#2696
Tracking issue: rust-lang#62482

Stabilizes the `debug_map_key_value` feature, which covers:

```rust
impl<'a, 'b> DebugMap<'a, 'b> {
    pub fn key(&mut self, key: &dyn fmt::Debug) -> &mut DebugMap<'a, 'b> {}
    pub fn value(&mut self, value: &dyn fmt::Debug) -> &mut DebugMap<'a, 'b> {}
}
```

These methods are small and self-contained, and are used as the basis for the existing `DebugMap::entry` method, so have been used in the wild for the last 6 months or so.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-fmt Proposals relating to std::fmt and formatting macros. disposition-merge This RFC is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this RFC. T-libs-api Relevant to the library API team, which will review and decide on the RFC.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants