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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 46 additions & 4 deletions src/uri/port.rs
Expand Up @@ -20,7 +20,7 @@ impl<'a> Port<'a> {
///
/// # Examples
///
/// Port as `u16`
/// Port as `u16`.
///
/// ```
/// # use http::uri::Authority;
Expand Down Expand Up @@ -51,14 +51,56 @@ impl<'a> Port<'a> {
}
}

impl<'a> fmt::Display for Port<'a> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "{}", self.bytes)
}
}

impl<'a> From<Port<'a>> for u16 {
fn from(port: Port) -> Self {
port.as_u16()
}
}

impl<'a> AsRef<str> for Port<'a> {
fn as_ref(&self) -> &str {
self.as_str()
}
}

impl<'a> PartialEq for Port<'a> {
fn eq(&self, other: &Self) -> bool {
self.port == other.port
}
}

impl<'a> fmt::Display for Port<'a> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "{}", self.bytes)
impl<'a> PartialEq<u16> for Port<'a> {
fn eq(&self, other: &u16) -> bool {
self.port == *other
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn partialeq_port() {
let port_a = Port::from_str("8080").unwrap();
let port_b = Port::from_str("8080").unwrap();
assert_eq!(port_a, port_b);
}

#[test]
fn partialeq_u16() {
let port = Port::from_str("8080").unwrap();
assert_eq!(port, 8080u16);
}

#[test]
fn u16_from_port() {
let port = Port::from_str("8080").unwrap();
assert_eq!(8080, u16::from(port));
}
}