Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upImplement `DebugStruct::non_exhaustive` and `DebugStruct::opaque_field`. #66716
Conversation
This comment has been minimized.
This comment has been minimized.
r? @kennytm (rust_highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
r? @scottmcm since they saw the internals post. |
/// # #![feature(debug_non_exhaustive)] | ||
/// use std::fmt; | ||
/// | ||
/// struct Foo; |
This comment has been minimized.
This comment has been minimized.
CAD97
Nov 24, 2019
Contributor
I'd add a field foo
to this structure, just for clarity's sake.
Probably Box<dyn (pick your favorite object safe trait that isn't `Debug`)>
.
This comment has been minimized.
This comment has been minimized.
Would it be simpler to print e.g. smth like pub fn non_exhaustive(&mut self) -> &mut DebugStruct<'a, 'b> {
self.result = self.result.and_then(|_| {
if self.is_pretty() {
if !self.has_fields {
self.fmt.write_str(" {\n")?;
}
let mut slot = None;
let mut state = Default::default();
let mut writer = PadAdapter::wrap(&mut self.fmt, &mut slot, &mut state);
writer.write_str("..\n")
} else {
let prefix = if self.has_fields { ", " } else { " { " };
self.fmt.write_str(prefix)?;
self.fmt.write_str("..")
}
});
self.has_fields = true;
self
}
pub fn finish_non_exhaustive(&mut self) -> fmt::Result {
self.non_exhaustive().finish()
} |
This comment has been minimized.
This comment has been minimized.
That is certainly another possibility, I'd be interested to hear other people's opinions. |
@@ -190,7 +195,7 @@ impl<'a, 'b: 'a> DebugStruct<'a, 'b> { | |||
/// } | |||
/// | |||
/// assert_eq!( | |||
/// format!("{:?}", Foo), | |||
/// format!("{:?}", Foo { foo: Box::new([1, 2].into_iter().copied()) }), |
This comment has been minimized.
This comment has been minimized.
CAD97
Nov 24, 2019
Contributor
You do not want to use .into_iter().copied()
here. This takes an autoref to &[_]
and will break when we eventually add impl IntoIterator for [_]
. Use .iter()
instead.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Ping from triage: @scottmcm - all checks pass, can you please review this PR? |
derekdreery commentedNov 24, 2019
•
edited
I suggested adding a method to
DebugStruct
that indicated there is more data contained in the struct that is hidden. It was suggested on internals that this would just be a PR, since it's a small change that goes in as unstable.The
opaque_field
method was super-simple to add so I added it, but maybe it doesn't belong in this PR. If so I'll remove it.Open questions
opaque_field
be removed?DebugTuple
andDebugMap
as well?Example
EDIT
The implementation of
finish
is a little complex, because there are three bimodal dimensions (alternate format, non-exhaustive, and has_fields), and so 2^3 = 8 possible permutations. If you include existing tests, most of these are covered by tests.