Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upimplement fmt traits for collections #25319
Conversation
rust-highfive
assigned
huonw
May 12, 2015
This comment has been minimized.
This comment has been minimized.
|
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @huonw (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. The way Github handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see CONTRIBUTING.md for more information. |
This comment has been minimized.
This comment has been minimized.
|
r? @Gankro |
rust-highfive
assigned
Gankro
and unassigned
huonw
May 12, 2015
Gankro
reviewed
May 12, 2015
| use core::result::Result; | ||
|
|
||
| pub fn seq_fmt_debug<I: Iterator>(s: I, f: &mut Formatter) -> fmt::Result | ||
| where I::Item:fmt::Debug |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Gankro
reviewed
May 12, 2015
| Result::Ok(()) | ||
| } | ||
|
|
||
| pub fn vec_map_fmt_debug<K, V, I: Iterator<Item=(K, V)>>(s: I, f: &mut Formatter) -> fmt::Result |
This comment has been minimized.
This comment has been minimized.
Gankro
May 12, 2015
Contributor
I don't see any obvious reason why vec_map is different from a normal Map?
This comment has been minimized.
This comment has been minimized.
Gankro
May 12, 2015
Contributor
Ah, you want to treat the keys special... I'm not sure that's the right call. In general I'd expect BTreeMap<usize, V> to be interchangeable with VecMap<V>. Any particular reason?
This comment has been minimized.
This comment has been minimized.
skullzzz
May 12, 2015
Author
vec_map currently implements Debug this way, ie. write!(f, "{}: {:?}", k, v)
I wasn't sure if that was some convention I wasn't aware of so I didn't change the behavior.
This comment has been minimized.
This comment has been minimized.
Gankro
May 12, 2015
Contributor
I would just change the behaviour. Debug and Display are identical for primitives as far as I know, anyway.
Gankro
reviewed
May 12, 2015
| fmt::Debug::fmt(&&self[..], f) | ||
| } | ||
| } | ||
| fmt_array! { $N, Debug, Display, Octal, Binary, UpperHex, LowerHex, UpperExp, LowerExp } |
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.
Gankro
May 12, 2015
Contributor
Nah, this is a problem that needs to be solved at a higher level (generic integers or rustdoc patches).
Gankro
reviewed
May 12, 2015
| try!(write!(f, "[")); | ||
| let mut is_first = true; | ||
| for x in self.iter() { | ||
| if is_first { |
This comment has been minimized.
This comment has been minimized.
Gankro
May 12, 2015
Contributor
Different style here than from everything else. (for (i, x) in iter.enumerate() { if i != 0 { .. } ..)
Intentional?
This comment has been minimized.
This comment has been minimized.
skullzzz
May 12, 2015
Author
This was how Debug was implemented for [T] before the builder stuff landed. I assumed it was because enumerate() was in std::iter and this is implemented in libcore. I'll check.
This comment has been minimized.
This comment has been minimized.
Gankro
reviewed
May 12, 2015
| @@ -1124,6 +1124,39 @@ impl<T: Debug> Debug for [T] { | |||
| } | |||
| } | |||
|
|
|||
| macro_rules! fmt_dst { | |||
This comment has been minimized.
This comment has been minimized.
Gankro
reviewed
May 12, 2015
| @@ -246,3 +248,96 @@ fn test_order() { | |||
| foo(), foo(), foo(), a=foo(), b=foo(), c=foo()), | |||
| "1 2 4 5 3 6".to_string()); | |||
| } | |||
|
|
|||
| fn test_collections() { | |||
This comment has been minimized.
This comment has been minimized.
Gankro
May 12, 2015
Contributor
I think this would be better placed in libcollectionstest. I think this file is mostly just verifying that the format macro itself works right.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
This is a pretty nice approach to the problem |
This comment has been minimized.
This comment has been minimized.
|
I feel like this would require an RFC—there are alternative approaches to this (e.g., adding formatting syntax for explicitly applying a formatter to the items in an iterable). Also, the precise details aren’t easy to settle on; e.g., should |
This comment has been minimized.
This comment has been minimized.
|
@P1start There have been two RFCs, both of which provide guidance on collection formatting: https://github.com/rust-lang/rfcs/blob/master/text/0565-show-string-guidelines.md |
This comment has been minimized.
This comment has been minimized.
|
Actually the first RFC explicitly states that Display should:
|
This comment has been minimized.
This comment has been minimized.
|
CC @aturon |
skullzzz
added some commits
May 12, 2015
This comment has been minimized.
This comment has been minimized.
|
Oh! Just saw you added commits (Github doesn't notify anyone when that happens). Are you still working on stuff or are you ready for another pass? |
This comment has been minimized.
This comment has been minimized.
|
Oops, I assumed new commits would send a notification. |
This comment has been minimized.
This comment has been minimized.
|
It has come to my attention that current standards are ambiguous: Is Octal et al. Displayish, or Debugish? Needs decision. |
rust-highfive
assigned
alexcrichton
and unassigned
Gankro
May 18, 2015
Gankro
added
the
I-needs-decision
label
May 18, 2015
alexcrichton
added
the
T-libs
label
May 20, 2015
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
|
Thanks again for the PR @skullzzz! For now, though, there is enough consensus to merge this. There are remaining concerns about how ambitious collections should be in their formatting (especially when it comes to the various flags formatters have). It's also possible to add these formattings as adaptors if necessary, or just writing a helper method. If you're interested in pursuing this, though, I'd recommend one of two courses of action:
One of the unfortunate parts about stability today is that any trait implementation is insta-stable, so there's a bit higher of a bar for entry for trait impls than new structures (as we can't introduce this as stable functionality). |
skullzzz commentedMay 12, 2015
reopen #20932
closes #19670
Still need to fix up tests and move them to libcollectionstest, but figured I should get comments on this implementation.