Skip to content

Commit

Permalink
Auto merge of servo#331 - SamWhited:no_try, r=SimonSapin
Browse files Browse the repository at this point in the history
Replace usage of try! with ?

Replaces all uses of `try!` with the `?` operator.

Fixes servo#330

<!-- 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/331)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo authored May 9, 2017
2 parents a44ccbe + b4d7400 commit a46c4e9
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 60 deletions.
22 changes: 11 additions & 11 deletions src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl ::serde::Serialize for HostInternal {
impl ::serde::Deserialize for HostInternal {
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error> where D: ::serde::Deserializer {
use std::net::IpAddr;
Ok(match try!(::serde::Deserialize::deserialize(deserializer)) {
Ok(match ::serde::Deserialize::deserialize(deserializer)? {
None => HostInternal::None,
Some(None) => HostInternal::Domain,
Some(Some(IpAddr::V4(addr))) => HostInternal::Ipv4(addr),
Expand Down Expand Up @@ -105,7 +105,7 @@ impl<S: ::serde::Serialize> ::serde::Serialize for Host<S> {
impl<S: ::serde::Deserialize> ::serde::Deserialize for Host<S> {
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error> where D: ::serde::Deserializer {
use std::net::IpAddr;
Ok(match try!(::serde::Deserialize::deserialize(deserializer)) {
Ok(match ::serde::Deserialize::deserialize(deserializer)? {
Ok(s) => Host::Domain(s),
Err(IpAddr::V4(addr)) => Host::Ipv4(addr),
Err(IpAddr::V6(addr)) => Host::Ipv6(addr),
Expand Down Expand Up @@ -146,13 +146,13 @@ impl Host<String> {
return parse_ipv6addr(&input[1..input.len() - 1]).map(Host::Ipv6)
}
let domain = percent_decode(input.as_bytes()).decode_utf8_lossy();
let domain = try!(idna::domain_to_ascii(&domain));
let domain = idna::domain_to_ascii(&domain)?;
if domain.find(|c| matches!(c,
'\0' | '\t' | '\n' | '\r' | ' ' | '#' | '%' | '/' | ':' | '?' | '@' | '[' | '\\' | ']'
)).is_some() {
return Err(ParseError::InvalidDomainCharacter)
}
if let Some(address) = try!(parse_ipv4addr(&domain)) {
if let Some(address) = parse_ipv4addr(&domain)? {
Ok(Host::Ipv4(address))
} else {
Ok(Host::Domain(domain.into()))
Expand All @@ -166,8 +166,8 @@ impl<S: AsRef<str>> fmt::Display for Host<S> {
Host::Domain(ref domain) => domain.as_ref().fmt(f),
Host::Ipv4(ref addr) => addr.fmt(f),
Host::Ipv6(ref addr) => {
try!(f.write_str("["));
try!(write_ipv6(addr, f));
f.write_str("[")?;
write_ipv6(addr, f)?;
f.write_str("]")
}
}
Expand Down Expand Up @@ -200,7 +200,7 @@ impl<S: AsRef<str>> ToSocketAddrs for HostAndPort<S> {
match self.host {
Host::Domain(ref domain) => Ok(SocketAddrs {
// FIXME: use std::net::lookup_host when it’s stable.
state: SocketAddrsState::Domain(try!((domain.as_ref(), port).to_socket_addrs()))
state: SocketAddrsState::Domain((domain.as_ref(), port).to_socket_addrs()?)
}),
Host::Ipv4(address) => Ok(SocketAddrs {
state: SocketAddrsState::One(SocketAddr::V4(SocketAddrV4::new(address, port)))
Expand Down Expand Up @@ -243,19 +243,19 @@ fn write_ipv6(addr: &Ipv6Addr, f: &mut Formatter) -> fmt::Result {
let mut i = 0;
while i < 8 {
if i == compress_start {
try!(f.write_str(":"));
f.write_str(":")?;
if i == 0 {
try!(f.write_str(":"));
f.write_str(":")?;
}
if compress_end < 8 {
i = compress_end;
} else {
break;
}
}
try!(write!(f, "{:x}", segments[i as usize]));
write!(f, "{:x}", segments[i as usize])?;
if i < 7 {
try!(f.write_str(":"));
f.write_str(":")?;
}
i += 1;
}
Expand Down
38 changes: 19 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ impl Url {
/// # use std::io;
///
/// fn connect(url: &Url) -> io::Result<TcpStream> {
/// TcpStream::connect(try!(url.with_default_port(default_port)))
/// TcpStream::connect(url.with_default_port(default_port)?)
/// }
///
/// fn default_port(url: &Url) -> Result<u16, ()> {
Expand All @@ -797,13 +797,13 @@ impl Url {
pub fn with_default_port<F>(&self, f: F) -> io::Result<HostAndPort<&str>>
where F: FnOnce(&Url) -> Result<u16, ()> {
Ok(HostAndPort {
host: try!(self.host()
.ok_or(())
.or_else(|()| io_error("URL has no host"))),
port: try!(self.port_or_known_default()
.ok_or(())
.or_else(|()| f(self))
.or_else(|()| io_error("URL has no port number")))
host: self.host()
.ok_or(())
.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"))?
})
}

Expand Down Expand Up @@ -1192,7 +1192,7 @@ impl Url {
if host == "" && SchemeType::from(self.scheme()).is_special() {
return Err(ParseError::EmptyHost);
}
self.set_host_internal(try!(Host::parse(host)), None)
self.set_host_internal(Host::parse(host)?, None)
} else if self.has_host() {
if SchemeType::from(self.scheme()).is_special() {
return Err(ParseError::EmptyHost)
Expand Down Expand Up @@ -1407,7 +1407,7 @@ impl Url {
/// ```
pub fn set_scheme(&mut self, scheme: &str) -> Result<(), ()> {
let mut parser = Parser::for_setter(String::new());
let remaining = try!(parser.parse_scheme(parser::Input::new(scheme)));
let remaining = parser.parse_scheme(parser::Input::new(scheme))?;
if !remaining.is_empty() ||
(!self.has_host() && SchemeType::from(&parser.serialization).is_special()) {
return Err(())
Expand Down Expand Up @@ -1458,7 +1458,7 @@ impl Url {
pub fn from_file_path<P: AsRef<Path>>(path: P) -> Result<Url, ()> {
let mut serialization = "file://".to_owned();
let path_start = serialization.len() as u32;
try!(path_to_file_url_segments(path.as_ref(), &mut serialization));
path_to_file_url_segments(path.as_ref(), &mut serialization)?;
Ok(Url {
serialization: serialization,
scheme_end: "file".len() as u32,
Expand Down Expand Up @@ -1491,7 +1491,7 @@ impl Url {
/// Note that `std::path` does not consider trailing slashes significant
/// and usually does not include them (e.g. in `Path::parent()`).
pub fn from_directory_path<P: AsRef<Path>>(path: P) -> Result<Url, ()> {
let mut url = try!(Url::from_file_path(path));
let mut url = Url::from_file_path(path)?;
if !url.serialization.ends_with('/') {
url.serialization.push('/')
}
Expand Down Expand Up @@ -1532,7 +1532,7 @@ impl Url {
use serde::{Deserialize, Error};
let (serialization, scheme_end, username_end,
host_start, host_end, host, port, path_start,
query_start, fragment_start) = try!(Deserialize::deserialize(deserializer));
query_start, fragment_start) = Deserialize::deserialize(deserializer)?;
let url = Url {
serialization: serialization,
scheme_end: scheme_end,
Expand All @@ -1546,7 +1546,7 @@ impl Url {
fragment_start: fragment_start
};
if cfg!(debug_assertions) {
try!(url.check_invariants().map_err(|ref reason| Error::invalid_value(&reason)))
url.check_invariants().map_err(|ref reason| Error::invalid_value(&reason))?
}
Ok(url)
}
Expand Down Expand Up @@ -1598,7 +1598,7 @@ impl ToSocketAddrs for Url {
type Iter = SocketAddrs;

fn to_socket_addrs(&self) -> io::Result<Self::Iter> {
try!(self.with_default_port(|_| Err(()))).to_socket_addrs()
self.with_default_port(|_| Err(()))?.to_socket_addrs()
}
}

Expand Down Expand Up @@ -1707,7 +1707,7 @@ impl rustc_serialize::Encodable for Url {
#[cfg(feature="rustc-serialize")]
impl rustc_serialize::Decodable for Url {
fn decode<D: rustc_serialize::Decoder>(decoder: &mut D) -> Result<Url, D::Error> {
Url::parse(&*try!(decoder.read_str())).map_err(|error| {
Url::parse(&*decoder.read_str()?).map_err(|error| {
decoder.error(&format!("URL parsing error: {}", error))
})
}
Expand All @@ -1729,7 +1729,7 @@ impl serde::Serialize for Url {
#[cfg(feature="serde")]
impl serde::Deserialize for Url {
fn deserialize<D>(deserializer: &mut D) -> Result<Url, D::Error> where D: serde::Deserializer {
let string_representation: String = try!(serde::Deserialize::deserialize(deserializer));
let string_representation: String = serde::Deserialize::deserialize(deserializer)?;
Url::parse(&string_representation).map_err(|err| {
serde::Error::invalid_value(err.description())
})
Expand Down Expand Up @@ -1789,7 +1789,7 @@ fn path_to_file_url_segments_windows(path: &Path, serialization: &mut String) ->
for component in components {
if component == Component::RootDir { continue }
// FIXME: somehow work with non-unicode?
let component = try!(component.as_os_str().to_str().ok_or(()));
let component = component.as_os_str().to_str().ok_or(())?;
serialization.push('/');
serialization.extend(percent_encode(component.as_bytes(), PATH_SEGMENT_ENCODE_SET));
}
Expand Down Expand Up @@ -1822,7 +1822,7 @@ fn file_url_segments_to_pathbuf(segments: str::Split<char>) -> Result<PathBuf, (
// Build this unconditionally to alleviate https://github.com/servo/rust-url/issues/102
#[cfg_attr(not(windows), allow(dead_code))]
fn file_url_segments_to_pathbuf_windows(mut segments: str::Split<char>) -> Result<PathBuf, ()> {
let first = try!(segments.next().ok_or(()));
let first = segments.next().ok_or(())?;

let mut string = match first.len() {
2 => {
Expand Down
Loading

0 comments on commit a46c4e9

Please sign in to comment.