Skip to content

Commit

Permalink
replace try! with ?
Browse files Browse the repository at this point in the history
  • Loading branch information
euclio committed Dec 28, 2019
1 parent 9fcfb11 commit fd42fb7
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 37 deletions.
46 changes: 23 additions & 23 deletions src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ pub fn set_opt<T: Copy>(sock: Socket, opt: c_int, val: c_int,
let payload = &payload as *const T as *const c_void;
#[cfg(target_os = "redox")]
let sock = sock as c_int;
try!(::cvt(setsockopt(sock, opt, val, payload as *const _,
mem::size_of::<T>() as socklen_t)));
::cvt(setsockopt(sock, opt, val, payload as *const _,
mem::size_of::<T>() as socklen_t))?;
}
Ok(())
}
Expand All @@ -90,9 +90,9 @@ pub fn get_opt<T: Copy>(sock: Socket, opt: c_int, val: c_int) -> io::Result<T> {
let mut len = mem::size_of::<T>() as socklen_t;
#[cfg(target_os = "redox")]
let sock = sock as c_int;
try!(::cvt(getsockopt(sock, opt, val,
::cvt(getsockopt(sock, opt, val,
&mut slot as *mut _ as *mut _,
&mut len)));
&mut len))?;
assert_eq!(len as usize, mem::size_of::<T>());
Ok(slot)
}
Expand Down Expand Up @@ -715,47 +715,47 @@ impl TcpStreamExt for TcpStream {

#[cfg(target_os = "redox")]
fn set_keepalive_ms(&self, keepalive: Option<u32>) -> io::Result<()> {
try!(set_opt(self.as_sock(), SOL_SOCKET, SO_KEEPALIVE,
keepalive.is_some() as c_int));
set_opt(self.as_sock(), SOL_SOCKET, SO_KEEPALIVE,
keepalive.is_some() as c_int)?;
if let Some(dur) = keepalive {
try!(set_opt(self.as_sock(), v(IPPROTO_TCP), KEEPALIVE_OPTION,
(dur / 1000) as c_int));
set_opt(self.as_sock(), v(IPPROTO_TCP), KEEPALIVE_OPTION,
(dur / 1000) as c_int)?;
}
Ok(())
}

#[cfg(target_os = "redox")]
fn keepalive_ms(&self) -> io::Result<Option<u32>> {
let keepalive = try!(get_opt::<c_int>(self.as_sock(), SOL_SOCKET,
SO_KEEPALIVE));
let keepalive = get_opt::<c_int>(self.as_sock(), SOL_SOCKET,
SO_KEEPALIVE)?;
if keepalive == 0 {
return Ok(None)
}
let secs = try!(get_opt::<c_int>(self.as_sock(), v(IPPROTO_TCP),
KEEPALIVE_OPTION));
let secs = get_opt::<c_int>(self.as_sock(), v(IPPROTO_TCP),
KEEPALIVE_OPTION)?;
Ok(Some((secs as u32) * 1000))
}

#[cfg(unix)]
fn set_keepalive_ms(&self, keepalive: Option<u32>) -> io::Result<()> {
try!(set_opt(self.as_sock(), SOL_SOCKET, SO_KEEPALIVE,
keepalive.is_some() as c_int));
set_opt(self.as_sock(), SOL_SOCKET, SO_KEEPALIVE,
keepalive.is_some() as c_int)?;
if let Some(dur) = keepalive {
try!(set_opt(self.as_sock(), v(IPPROTO_TCP), KEEPALIVE_OPTION,
(dur / 1000) as c_int));
set_opt(self.as_sock(), v(IPPROTO_TCP), KEEPALIVE_OPTION,
(dur / 1000) as c_int)?;
}
Ok(())
}

#[cfg(unix)]
fn keepalive_ms(&self) -> io::Result<Option<u32>> {
let keepalive = try!(get_opt::<c_int>(self.as_sock(), SOL_SOCKET,
SO_KEEPALIVE));
let keepalive = get_opt::<c_int>(self.as_sock(), SOL_SOCKET,
SO_KEEPALIVE)?;
if keepalive == 0 {
return Ok(None)
}
let secs = try!(get_opt::<c_int>(self.as_sock(), v(IPPROTO_TCP),
KEEPALIVE_OPTION));
let secs = get_opt::<c_int>(self.as_sock(), v(IPPROTO_TCP),
KEEPALIVE_OPTION)?;
Ok(Some((secs as u32) * 1000))
}

Expand Down Expand Up @@ -798,15 +798,15 @@ impl TcpStreamExt for TcpStream {
keepaliveinterval: 0,
};
unsafe {
try!(::cvt_win(WSAIoctl(self.as_sock(),
::cvt_win(WSAIoctl(self.as_sock(),
SIO_KEEPALIVE_VALS,
0 as *mut _,
0,
&mut ka as *mut _ as *mut _,
mem::size_of_val(&ka) as DWORD,
0 as *mut _,
0 as *mut _,
None)));
None))?;
}
Ok({
if ka.onoff == 0 {
Expand Down Expand Up @@ -1290,7 +1290,7 @@ impl UdpSocketExt for UdpSocket {
fn do_connect<A: ToSocketAddrs>(sock: Socket, addr: A) -> io::Result<()> {
let err = io::Error::new(io::ErrorKind::Other,
"no socket addresses resolved");
let addrs = try!(addr.to_socket_addrs());
let addrs = addr.to_socket_addrs()?;
let sys = sys::Socket::from_inner(sock);
let sock = socket::Socket::from_inner(sys);
let ret = addrs.fold(Err(err), |prev, addr| {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub use udp::UdpBuilder;
pub use ext::{TcpStreamExt, TcpListenerExt, UdpSocketExt};

fn one_addr<T: ToSocketAddrs>(tsa: T) -> io::Result<SocketAddr> {
let mut addrs = try!(tsa.to_socket_addrs());
let mut addrs = tsa.to_socket_addrs()?;
let addr = match addrs.next() {
Some(addr) => addr,
None => return Err(io::Error::new(io::ErrorKind::Other,
Expand Down
6 changes: 3 additions & 3 deletions src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct Socket {

impl Socket {
pub fn new(family: c_int, ty: c_int) -> io::Result<Socket> {
Ok(Socket { inner: try!(sys::Socket::new(family, ty)) })
Ok(Socket { inner: sys::Socket::new(family, ty)? })
}

pub fn bind(&self, addr: &SocketAddr) -> io::Result<()> {
Expand All @@ -53,9 +53,9 @@ impl Socket {
unsafe {
let mut storage: c::sockaddr_storage = mem::zeroed();
let mut len = mem::size_of_val(&storage) as c::socklen_t;
try!(::cvt(c::getsockname(self.inner.raw(),
::cvt(c::getsockname(self.inner.raw(),
&mut storage as *mut _ as *mut _,
&mut len)));
&mut len))?;
raw2addr(&storage, len)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/sys/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl Socket {
Err(e) => return Err(e),
}

let fd = try!(::cvt(libc::socket(family, ty, 0)));
let fd = ::cvt(libc::socket(family, ty, 0))?;
ioctl(fd, FIOCLEX);
Ok(Socket { fd: fd })
}
Expand All @@ -61,7 +61,7 @@ impl Socket {
#[cfg(any(target_os = "solaris", target_os = "emscripten"))]
pub fn new(family: c_int, ty: c_int) -> io::Result<Socket> {
unsafe {
let fd = try!(::cvt(libc::socket(family, ty, 0)));
let fd = ::cvt(libc::socket(family, ty, 0))?;
libc::fcntl(fd, libc::FD_CLOEXEC);
Ok(Socket { fd: fd })
}
Expand Down
6 changes: 3 additions & 3 deletions src/sys/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ pub struct Socket {
impl Socket {
pub fn new(family: c_int, ty: c_int) -> io::Result<Socket> {
init();
let socket = try!(unsafe {
let socket = unsafe {
match WSASocketW(family, ty, 0, 0 as *mut _, 0,
WSA_FLAG_OVERLAPPED) {
INVALID_SOCKET => Err(io::Error::last_os_error()),
n => Ok(Socket { socket: n }),
}
});
try!(socket.set_no_inherit());
}?;
socket.set_no_inherit()?;
Ok(socket)
}

Expand Down
4 changes: 2 additions & 2 deletions src/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl TcpBuilder {
where T: ToSocketAddrs
{
self.with_socket(|sock| {
let addr = try!(::one_addr(addr));
let addr = ::one_addr(addr)?;
sock.bind(&addr)
}).map(|()| self)
}
Expand Down Expand Up @@ -85,7 +85,7 @@ impl TcpBuilder {
self.with_socket(|sock| {
let err = io::Error::new(io::ErrorKind::Other,
"no socket addresses resolved");
try!(addr.to_socket_addrs()).fold(Err(err), |prev, addr| {
addr.to_socket_addrs()?.fold(Err(err), |prev, addr| {
prev.or_else(|_| sock.connect(&addr))
})
}).and_then(|()| {
Expand Down
6 changes: 3 additions & 3 deletions src/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ impl UdpBuilder {
pub fn bind<T>(&self, addr: T) -> io::Result<UdpSocket>
where T: ToSocketAddrs
{
try!(self.with_socket(|sock| {
let addr = try!(::one_addr(addr));
self.with_socket(|sock| {
let addr = ::one_addr(addr)?;
sock.bind(&addr)
}));
})?;
Ok(self.socket.borrow_mut().take().unwrap().into_inner().into_udp_socket())
}

Expand Down

0 comments on commit fd42fb7

Please sign in to comment.