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

simplified code #694

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/uri/authority.rs
Expand Up @@ -235,7 +235,7 @@ impl Authority {
///
/// assert!(authority.port().is_none());
/// ```
pub fn port(&self) -> Option<Port<&str>> {
pub fn port(&self) -> Option<Port> {
let bytes = self.as_str();
bytes
.rfind(':')
Expand Down
2 changes: 1 addition & 1 deletion src/uri/mod.rs
Expand Up @@ -633,7 +633,7 @@ impl Uri {
///
/// assert!(uri.port().is_none());
/// ```
pub fn port(&self) -> Option<Port<&str>> {
pub fn port(&self) -> Option<Port> {
self.authority().and_then(|a| a.port())
}

Expand Down
50 changes: 19 additions & 31 deletions src/uri/port.rs
Expand Up @@ -3,12 +3,12 @@ use std::fmt;
use super::{ErrorKind, InvalidUri};

/// The port component of a URI.
pub struct Port<T> {
pub struct Port {
port: u16,
repr: T,
repr: String,
}

impl<T> Port<T> {
impl Port {
/// Returns the port number as a `u16`.
///
/// # Examples
Expand All @@ -27,18 +27,18 @@ impl<T> Port<T> {
}
}

impl<T> Port<T>
where
T: AsRef<str>,
{
impl Port {
/// Converts a `str` to a port number.
///
/// The supplied `str` must be a valid u16.
pub(crate) fn from_str(bytes: T) -> Result<Self, InvalidUri> {
pub(crate) fn from_str(bytes: impl AsRef<str>) -> Result<Self, InvalidUri> {
bytes
.as_ref()
.parse::<u16>()
.map(|port| Port { port, repr: bytes })
.map(|port| Port {
port,
repr: bytes.as_ref().to_string(),
})
.map_err(|_| ErrorKind::InvalidPort.into())
}

Expand All @@ -60,52 +60,40 @@ where
}
}

impl<T> fmt::Debug for Port<T>
where
T: fmt::Debug,
{
impl fmt::Debug for Port {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Port").field(&self.port).finish()
}
}

impl<T> fmt::Display for Port<T> {
impl fmt::Display for Port {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// Use `u16::fmt` so that it respects any formatting flags that
// may have been set (like padding, align, etc).
fmt::Display::fmt(&self.port, f)
}
}

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

impl<T> AsRef<str> for Port<T>
where
T: AsRef<str>,
{
fn as_ref(&self) -> &str {
self.as_str()
}
}

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

impl<T> PartialEq<u16> for Port<T> {
impl PartialEq<u16> for Port {
fn eq(&self, other: &u16) -> bool {
self.port == *other
}
}

impl<T> PartialEq<Port<T>> for u16 {
fn eq(&self, other: &Port<T>) -> bool {
impl PartialEq<Port> for u16 {
fn eq(&self, other: &Port) -> bool {
other.port == *self
}
}
Expand All @@ -124,7 +112,7 @@ mod tests {
#[test]
fn partialeq_port_different_reprs() {
let port_a = Port {
repr: "8081",
repr: "8081".to_string(),
port: 8081,
};
let port_b = Port {
Expand Down