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 upAdd Display for HostAndPort #328
Conversation
SamWhited
reviewed
May 9, 2017
| @@ -192,6 +192,15 @@ impl<'a> HostAndPort<&'a str> { | |||
| } | |||
| } | |||
|
|
|||
| impl<S: AsRef<str>> fmt::Display for HostAndPort<S> { | |||
| fn fmt(&self, f: &mut Formatter) -> fmt::Result { | |||
| try!(self.host.fmt(f)); | |||
This comment has been minimized.
This comment has been minimized.
SamWhited
May 9, 2017
Contributor
I wonder if there's an easy way to write this all at once so that we return an Err Result and don't write anything to the formatter, or we return Ok and the formatter has the full thing? I don't think Display makes that guarantee, but it seems like a "nice to have".
This comment has been minimized.
This comment has been minimized.
SimonSapin
May 9, 2017
Member
@SamWhited I don’t understand what you’re proposing. Could you explain some more or give an example?
In any case, it is incorrect for a Display::fmt impl to return Err other than for propagating errors that ultimately come from the Formatter. Formatting is supposed to be infallible, it’s only writing to the output stream (for example to a file on a full disk) that can fail.
This comment has been minimized.
This comment has been minimized.
SamWhited
May 9, 2017
Contributor
Yes, sorry, on rereading that was confusing.
I'm suggesting that if possible we do a single, atomic, write to the formatter. Right now we might write host: and then something bad happens and writing the port fails so we end up with a half written thing.
It's probably not actually an issue.
This comment has been minimized.
This comment has been minimized.
SimonSapin
May 9, 2017
Member
Display impls are pretty much never "atomic" in that sense. On the contrary, they are designed to be composable like this. The only way I can think of to make on "atomic" would be to first format to a String and then write that, but that would add unnecessary costly memory allocations. A large number of them, for deeply nested Display::fmt calls.
This comment has been minimized.
This comment has been minimized.
Pistahh
reviewed
May 9, 2017
| @@ -192,6 +192,15 @@ impl<'a> HostAndPort<&'a str> { | |||
| } | |||
| } | |||
|
|
|||
| impl<S: AsRef<str>> fmt::Display for HostAndPort<S> { | |||
| fn fmt(&self, f: &mut Formatter) -> fmt::Result { | |||
| try!(self.host.fmt(f)); | |||
This comment has been minimized.
This comment has been minimized.
Pistahh
May 9, 2017
what about using the "new" ? operator?
self.host.fmt(f)?;
self.write_str(":")?;
self.port.fmt(f)
This comment has been minimized.
This comment has been minimized.
SamWhited
May 9, 2017
•
Contributor
I was about to suggest that as a nit pick :) please do (IMO)! It doesn't affect the other thing being discussed, but it is probably worth doing either way
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
SimonSapin
reviewed
May 9, 2017
| ) | ||
| ]; | ||
|
|
||
| for &(ref input, result) in &data { |
This comment has been minimized.
This comment has been minimized.
SimonSapin
May 9, 2017
Member
Avoid for loops like this for unit tests. When a test fail it will show the line number for assert_eq!, but that doesn’t show which of the entries failed.
In this case I think duplicating the assert_eq! + format! line is fine, since it’s not long and there’s only two test cases. In a case when de-duplicating testing code is desirable, consider using a macro like assert_from_file_path! in this file. This makes panic messages use the line number of the macro usage.
tomecki
added some commits
May 9, 2017
This comment has been minimized.
This comment has been minimized.
|
I addressed your comments, and added the |
This comment has been minimized.
This comment has been minimized.
|
Oops, I noticed, that |
This comment has been minimized.
This comment has been minimized.
sigmavirus24
commented
May 10, 2017
|
Review status: 0 of 2 files reviewed at latest revision, 2 unresolved discussions. Comments from Reviewable |
dtolnay
reviewed
May 12, 2017
dtolnay left a comment
|
Looks good to me. All of the feedback has been addressed. |
This comment has been minimized.
This comment has been minimized.
|
Reviewed 1 of 2 files at r2, 1 of 1 files at r3. Comments from Reviewable |
This comment has been minimized.
This comment has been minimized.
|
Looks good. @bors-servo r+ |
This comment has been minimized.
This comment has been minimized.
|
|
bors-servo
added a commit
that referenced
this pull request
Jun 13, 2017
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
|
tomecki commentedMay 7, 2017
•
edited by larsbergstrom
Fix #300
This change is