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 trait impls for uri::Port #255

Merged
merged 1 commit into from Oct 8, 2018
Merged

Add trait impls for uri::Port #255

merged 1 commit into from Oct 8, 2018

Conversation

joshleeb
Copy link
Contributor

Implement some useful trait impls for uri::Port. This follows on from #252 and specifically this comment.

Copy link
Collaborator

@carllerche carllerche left a comment

Choose a reason for hiding this comment

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

Thanks for following up. Some thoughts inline.

src/uri/port.rs Outdated
impl<'a> fmt::Display for Port<'a> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "{}", self.bytes)
impl<'a, T: From<u16> + PartialEq> PartialEq<T> for Port<'a> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would rather avoid a blanket impl for this partialeq. I believe you did this to avoid having to add impls for each u* type. Perhaps a macro would work instead?

The problem is that adding this blanket impl would prevent us from adding a different blanket impl. Perhaps we would like to impl PartialEq<T> for T: AsRef<Port> or something.

src/uri/port.rs Outdated
}
}

impl<'a> Into<&'a str> for Port<'a> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

It also looks like we could impl AsRef<str> for Port.

@joshleeb
Copy link
Contributor Author

@carllerche updated 😄

src/uri/port.rs Outdated
port_partialeq!(u32);
port_partialeq!(u64);

port_partialeq!(i16);
Copy link
Collaborator

Choose a reason for hiding this comment

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

At a glance, an unchecked cast from u16 -> i16 is problematic unless there is a guarantee that the value will not be greater than i16::MAX. Where is this checked?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added the check in

src/uri/port.rs Outdated
}
}

impl<'a> Into<u16> for Port<'a> {
Copy link
Member

Choose a reason for hiding this comment

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

It's preferred to use the inverse of this, impl From<Port> for u16. Otherwise, u16::from(port) cannot work.

src/uri/port.rs Outdated
impl<'a> fmt::Display for Port<'a> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "{}", self.bytes)
macro_rules! port_partialeq {
Copy link
Member

Choose a reason for hiding this comment

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

I personally would suggest just being for u16, since that's what ports usually are. (Though, it'd be nice to remember the to implement PartialEq<Port> for u16 as well, to allow both port == num and num == port.)

@seanmonstar seanmonstar merged commit f8b3693 into hyperium:master Oct 8, 2018
@seanmonstar
Copy link
Member

Thanks!

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.

None yet

3 participants