Skip to content

Commit a46c4e9

Browse files
author
bors-servo
authored
Auto merge of #331 - SamWhited:no_try, r=SimonSapin
Replace usage of try! with ? Replaces all uses of `try!` with the `?` operator. Fixes #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 -->
2 parents a44ccbe + b4d7400 commit a46c4e9

File tree

5 files changed

+59
-60
lines changed

5 files changed

+59
-60
lines changed

src/host.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl ::serde::Serialize for HostInternal {
5050
impl ::serde::Deserialize for HostInternal {
5151
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error> where D: ::serde::Deserializer {
5252
use std::net::IpAddr;
53-
Ok(match try!(::serde::Deserialize::deserialize(deserializer)) {
53+
Ok(match ::serde::Deserialize::deserialize(deserializer)? {
5454
None => HostInternal::None,
5555
Some(None) => HostInternal::Domain,
5656
Some(Some(IpAddr::V4(addr))) => HostInternal::Ipv4(addr),
@@ -105,7 +105,7 @@ impl<S: ::serde::Serialize> ::serde::Serialize for Host<S> {
105105
impl<S: ::serde::Deserialize> ::serde::Deserialize for Host<S> {
106106
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error> where D: ::serde::Deserializer {
107107
use std::net::IpAddr;
108-
Ok(match try!(::serde::Deserialize::deserialize(deserializer)) {
108+
Ok(match ::serde::Deserialize::deserialize(deserializer)? {
109109
Ok(s) => Host::Domain(s),
110110
Err(IpAddr::V4(addr)) => Host::Ipv4(addr),
111111
Err(IpAddr::V6(addr)) => Host::Ipv6(addr),
@@ -146,13 +146,13 @@ impl Host<String> {
146146
return parse_ipv6addr(&input[1..input.len() - 1]).map(Host::Ipv6)
147147
}
148148
let domain = percent_decode(input.as_bytes()).decode_utf8_lossy();
149-
let domain = try!(idna::domain_to_ascii(&domain));
149+
let domain = idna::domain_to_ascii(&domain)?;
150150
if domain.find(|c| matches!(c,
151151
'\0' | '\t' | '\n' | '\r' | ' ' | '#' | '%' | '/' | ':' | '?' | '@' | '[' | '\\' | ']'
152152
)).is_some() {
153153
return Err(ParseError::InvalidDomainCharacter)
154154
}
155-
if let Some(address) = try!(parse_ipv4addr(&domain)) {
155+
if let Some(address) = parse_ipv4addr(&domain)? {
156156
Ok(Host::Ipv4(address))
157157
} else {
158158
Ok(Host::Domain(domain.into()))
@@ -166,8 +166,8 @@ impl<S: AsRef<str>> fmt::Display for Host<S> {
166166
Host::Domain(ref domain) => domain.as_ref().fmt(f),
167167
Host::Ipv4(ref addr) => addr.fmt(f),
168168
Host::Ipv6(ref addr) => {
169-
try!(f.write_str("["));
170-
try!(write_ipv6(addr, f));
169+
f.write_str("[")?;
170+
write_ipv6(addr, f)?;
171171
f.write_str("]")
172172
}
173173
}
@@ -200,7 +200,7 @@ impl<S: AsRef<str>> ToSocketAddrs for HostAndPort<S> {
200200
match self.host {
201201
Host::Domain(ref domain) => Ok(SocketAddrs {
202202
// FIXME: use std::net::lookup_host when it’s stable.
203-
state: SocketAddrsState::Domain(try!((domain.as_ref(), port).to_socket_addrs()))
203+
state: SocketAddrsState::Domain((domain.as_ref(), port).to_socket_addrs()?)
204204
}),
205205
Host::Ipv4(address) => Ok(SocketAddrs {
206206
state: SocketAddrsState::One(SocketAddr::V4(SocketAddrV4::new(address, port)))
@@ -243,19 +243,19 @@ fn write_ipv6(addr: &Ipv6Addr, f: &mut Formatter) -> fmt::Result {
243243
let mut i = 0;
244244
while i < 8 {
245245
if i == compress_start {
246-
try!(f.write_str(":"));
246+
f.write_str(":")?;
247247
if i == 0 {
248-
try!(f.write_str(":"));
248+
f.write_str(":")?;
249249
}
250250
if compress_end < 8 {
251251
i = compress_end;
252252
} else {
253253
break;
254254
}
255255
}
256-
try!(write!(f, "{:x}", segments[i as usize]));
256+
write!(f, "{:x}", segments[i as usize])?;
257257
if i < 7 {
258-
try!(f.write_str(":"));
258+
f.write_str(":")?;
259259
}
260260
i += 1;
261261
}

src/lib.rs

+19-19
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ impl Url {
781781
/// # use std::io;
782782
///
783783
/// fn connect(url: &Url) -> io::Result<TcpStream> {
784-
/// TcpStream::connect(try!(url.with_default_port(default_port)))
784+
/// TcpStream::connect(url.with_default_port(default_port)?)
785785
/// }
786786
///
787787
/// fn default_port(url: &Url) -> Result<u16, ()> {
@@ -797,13 +797,13 @@ impl Url {
797797
pub fn with_default_port<F>(&self, f: F) -> io::Result<HostAndPort<&str>>
798798
where F: FnOnce(&Url) -> Result<u16, ()> {
799799
Ok(HostAndPort {
800-
host: try!(self.host()
801-
.ok_or(())
802-
.or_else(|()| io_error("URL has no host"))),
803-
port: try!(self.port_or_known_default()
804-
.ok_or(())
805-
.or_else(|()| f(self))
806-
.or_else(|()| io_error("URL has no port number")))
800+
host: self.host()
801+
.ok_or(())
802+
.or_else(|()| io_error("URL has no host"))?,
803+
port: self.port_or_known_default()
804+
.ok_or(())
805+
.or_else(|()| f(self))
806+
.or_else(|()| io_error("URL has no port number"))?
807807
})
808808
}
809809

@@ -1192,7 +1192,7 @@ impl Url {
11921192
if host == "" && SchemeType::from(self.scheme()).is_special() {
11931193
return Err(ParseError::EmptyHost);
11941194
}
1195-
self.set_host_internal(try!(Host::parse(host)), None)
1195+
self.set_host_internal(Host::parse(host)?, None)
11961196
} else if self.has_host() {
11971197
if SchemeType::from(self.scheme()).is_special() {
11981198
return Err(ParseError::EmptyHost)
@@ -1407,7 +1407,7 @@ impl Url {
14071407
/// ```
14081408
pub fn set_scheme(&mut self, scheme: &str) -> Result<(), ()> {
14091409
let mut parser = Parser::for_setter(String::new());
1410-
let remaining = try!(parser.parse_scheme(parser::Input::new(scheme)));
1410+
let remaining = parser.parse_scheme(parser::Input::new(scheme))?;
14111411
if !remaining.is_empty() ||
14121412
(!self.has_host() && SchemeType::from(&parser.serialization).is_special()) {
14131413
return Err(())
@@ -1458,7 +1458,7 @@ impl Url {
14581458
pub fn from_file_path<P: AsRef<Path>>(path: P) -> Result<Url, ()> {
14591459
let mut serialization = "file://".to_owned();
14601460
let path_start = serialization.len() as u32;
1461-
try!(path_to_file_url_segments(path.as_ref(), &mut serialization));
1461+
path_to_file_url_segments(path.as_ref(), &mut serialization)?;
14621462
Ok(Url {
14631463
serialization: serialization,
14641464
scheme_end: "file".len() as u32,
@@ -1491,7 +1491,7 @@ impl Url {
14911491
/// Note that `std::path` does not consider trailing slashes significant
14921492
/// and usually does not include them (e.g. in `Path::parent()`).
14931493
pub fn from_directory_path<P: AsRef<Path>>(path: P) -> Result<Url, ()> {
1494-
let mut url = try!(Url::from_file_path(path));
1494+
let mut url = Url::from_file_path(path)?;
14951495
if !url.serialization.ends_with('/') {
14961496
url.serialization.push('/')
14971497
}
@@ -1532,7 +1532,7 @@ impl Url {
15321532
use serde::{Deserialize, Error};
15331533
let (serialization, scheme_end, username_end,
15341534
host_start, host_end, host, port, path_start,
1535-
query_start, fragment_start) = try!(Deserialize::deserialize(deserializer));
1535+
query_start, fragment_start) = Deserialize::deserialize(deserializer)?;
15361536
let url = Url {
15371537
serialization: serialization,
15381538
scheme_end: scheme_end,
@@ -1546,7 +1546,7 @@ impl Url {
15461546
fragment_start: fragment_start
15471547
};
15481548
if cfg!(debug_assertions) {
1549-
try!(url.check_invariants().map_err(|ref reason| Error::invalid_value(&reason)))
1549+
url.check_invariants().map_err(|ref reason| Error::invalid_value(&reason))?
15501550
}
15511551
Ok(url)
15521552
}
@@ -1598,7 +1598,7 @@ impl ToSocketAddrs for Url {
15981598
type Iter = SocketAddrs;
15991599

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

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

18271827
let mut string = match first.len() {
18281828
2 => {

0 commit comments

Comments
 (0)