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 Display for HostAndPort #328

Merged
merged 4 commits into from
Jun 13, 2017

Conversation

tomecki
Copy link
Contributor

@tomecki tomecki commented May 7, 2017

Fix #300


This change is Reviewable

src/host.rs Outdated
@@ -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));
Copy link
Contributor

Choose a reason for hiding this comment

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

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".

Copy link
Member

Choose a reason for hiding this comment

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

@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.

Copy link
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Member

Choose a reason for hiding this comment

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

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

ooh yah, an allocation would be worse. LGTM then.

src/host.rs Outdated
@@ -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));
Copy link

Choose a reason for hiding this comment

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

what about using the "new" ? operator?

self.host.fmt(f)?;
self.write_str(":")?;
self.port.fmt(f)

Copy link
Contributor

@SamWhited SamWhited May 9, 2017

Choose a reason for hiding this comment

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

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

Copy link
Member

Choose a reason for hiding this comment

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

Let’s do this separately, on the whole repo: #330

Copy link
Member

Choose a reason for hiding this comment

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

Since #331 has landed first, please switch to ?. Thanks!

tests/unit.rs Outdated
)
];

for &(ref input, result) in &data {
Copy link
Member

Choose a reason for hiding this comment

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

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
Copy link
Contributor Author

tomecki commented May 9, 2017

I addressed your comments, and added the ? operator to Display for Host. Thank you very much for your input!

@tomecki
Copy link
Contributor Author

tomecki commented May 9, 2017

Oops, I noticed, that ? has been added to Display for Host, added a revert commit.

@sigmavirus24
Copy link

:lgtm: This looks good. Thanks for the extra tests too :)


Review status: 0 of 2 files reviewed at latest revision, 2 unresolved discussions.


Comments from Reviewable

Copy link
Contributor

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

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

Looks good to me. All of the feedback has been addressed.

@tmccombs
Copy link
Contributor

Reviewed 1 of 2 files at r2, 1 of 1 files at r3.
Review status: all files reviewed at latest revision, 2 unresolved discussions.


Comments from Reviewable

@SimonSapin
Copy link
Member

Looks good.

@bors-servo r+

@bors-servo
Copy link
Contributor

📌 Commit a6421f6 has been approved by SimonSapin

bors-servo pushed a commit that referenced this pull request Jun 13, 2017
Add Display for HostAndPort

Fix #300

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-url/328)
<!-- Reviewable:end -->
@bors-servo
Copy link
Contributor

⌛ Testing commit a6421f6 with merge 4e909f6...

@bors-servo
Copy link
Contributor

☀️ Test successful - status-travis
Approved by: SimonSapin
Pushing 4e909f6 to master...

@bors-servo bors-servo merged commit a6421f6 into servo:master Jun 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants