-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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 #60458
Conversation
Some changes occurred in HTML/CSS. |
r? @rkruppe (rust_highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
buf: &'a mut (dyn fmt::Write + 'a), | ||
struct PadAdapter<'buf, 'state> { | ||
buf: &'buf mut (dyn fmt::Write + 'buf), | ||
state: &'state mut PadAdapterState, |
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.
Shifting the state of the PadAdapter
into a dedicated struct here lets value
pick up where key
left off without having to guess what the state was before.
@rust-lang/infra: seems like a condition is broken:
|
Highfive is working as expected though: |
Ah indeed! My bad. |
Very quick, very dirty benchmark: #![feature(test)]
extern crate test;
use std::{
cell::Cell,
fmt::{self, Write},
};
struct NoOpWrite;
impl Write for NoOpWrite {
fn write_str(&mut self, _: &str) -> fmt::Result {
Ok(())
}
}
#[bench]
fn debugmap_entry(b: &mut test::Bencher) {
struct Map<'a>(Cell<Option<&'a mut test::Bencher>>);
impl<'a> fmt::Debug for Map<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut map = f.debug_map();
let b = self.0.take().unwrap();
b.iter(|| {
map.entry(&"a", &42);
});
map.finish()
}
}
let _ = write!(NoOpWrite, "{:?}", Map(Cell::new(Some(b))));
} On my machine, running this on the current
So there's a bit of a performance impact like you'd expect, but I don't think it's too significant, and if it is we can inline the implementation in |
This comment has been minimized.
This comment has been minimized.
This all looks great to me! I think this is still waiting on the RFC, though, right? (FWIW I wouldn't personally consider this as requiring an RFC, but I don't mind waiting if one's open!) |
Thanks! Yeh, there's an RFC for this at the moment, so I'm also happy to let this sit and wait on that. |
Alrighty, the RFC has been merged and I've updated the stability attributes to point to the tracking issue. I think this should be ready to go now! |
r=me, thanks! Looks like CI may be failing though? |
Looks like we're all good! @bors r=alexcrichton |
@bors r=alexcrichton |
📌 Commit 70d630f has been approved by |
…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`.
Rollup of 4 pull requests Successful merges: - #60458 (Add key and value methods to DebugMap) - #62090 (typeck: merge opaque type inference logic) - #62403 (Replace SliceConcatExt trait with inherent methods and SliceConcat helper trait) - #62494 (Remove unused dependencies) Failed merges: r? @ghost
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:I want to do this so that I can write a
serde::Serializer
that forwards to our format builders, so that anyT: Serialize
can also be treated like aT: Debug
.