From cf6f875de7301c19d9785eac6a6b36af1318b103 Mon Sep 17 00:00:00 2001 From: Thayne McCombs Date: Mon, 15 Jul 2019 00:00:09 -0600 Subject: [PATCH] Change the closure taken by `with_default_port` to return Option instead of Result. `Result` seems like overkill for the closure, especially now that the `?` operator has been stabilized for `Option`. --- src/lib.rs | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 203b973bb..06cee8b57 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -978,7 +978,7 @@ impl Url { /// the closure is called to obtain a port number. /// Typically, this closure can match on the result `Url::scheme` /// to have per-scheme default port numbers, - /// and panic for schemes it’s not prepared to handle. + /// and return `None` for schemes it’s not prepared to handle. /// For example: /// /// ```rust @@ -989,26 +989,24 @@ impl Url { /// TcpStream::connect(url.with_default_port(default_port)?) /// } /// - /// fn default_port(url: &Url) -> Result { + /// fn default_port(url: &Url) -> Option { /// match url.scheme() { - /// "git" => Ok(9418), - /// "git+ssh" => Ok(22), - /// "git+https" => Ok(443), - /// "git+http" => Ok(80), - /// _ => Err(()), + /// "git" => Some(9418), + /// "git+ssh" => Some(22), + /// "git+https" => Some(443), + /// "git+http" => Some(80), + /// _ => None, /// } /// } /// ``` pub fn with_default_port(&self, f: F) -> io::Result> - where F: FnOnce(&Url) -> Result { + where F: FnOnce(&Url) -> Option { Ok(HostAndPort { host: self.host() - .ok_or(()) - .or_else(|()| io_error("URL has no host"))?, + .ok_or_else(|| io_error("URL has no host"))?, port: self.port_or_known_default() - .ok_or(()) - .or_else(|()| f(self)) - .or_else(|()| io_error("URL has no port number"))? + .or_else(|| f(self)) + .ok_or_else(|| io_error("URL has no port number"))? }) } @@ -2128,7 +2126,7 @@ impl ToSocketAddrs for Url { type Iter = SocketAddrs; fn to_socket_addrs(&self) -> io::Result { - self.with_default_port(|_| Err(()))?.to_socket_addrs() + self.with_default_port(|_| None)?.to_socket_addrs() } } @@ -2346,7 +2344,6 @@ fn path_to_file_url_segments_windows(path: &Path, serialization: &mut String) fn file_url_segments_to_pathbuf(host: Option<&str>, segments: str::Split) -> Result { use std::ffi::OsStr; use std::os::unix::prelude::OsStrExt; - use std::path::PathBuf; if host.is_some() { return Err(()); @@ -2422,8 +2419,8 @@ fn file_url_segments_to_pathbuf_windows(host: Option<&str>, mut segments: str::S Ok(path) } -fn io_error(reason: &str) -> io::Result { - Err(io::Error::new(io::ErrorKind::InvalidData, reason)) +fn io_error(reason: &str) -> io::Error { + io::Error::new(io::ErrorKind::InvalidData, reason) } /// Implementation detail of `Url::query_pairs_mut`. Typically not used directly.