Skip to content

Commit ca03573

Browse files
committed
Replace try! with ?
try! is not available in Rust 2018
1 parent 5a3ac8d commit ca03573

File tree

18 files changed

+101
-104
lines changed

18 files changed

+101
-104
lines changed

src/fcntl.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,17 +139,17 @@ libc_bitflags!(
139139
);
140140

141141
pub fn open<P: ?Sized + NixPath>(path: &P, oflag: OFlag, mode: Mode) -> Result<RawFd> {
142-
let fd = try!(path.with_nix_path(|cstr| {
142+
let fd = path.with_nix_path(|cstr| {
143143
unsafe { libc::open(cstr.as_ptr(), oflag.bits(), mode.bits() as c_uint) }
144-
}));
144+
})?;
145145

146146
Errno::result(fd)
147147
}
148148

149149
pub fn openat<P: ?Sized + NixPath>(dirfd: RawFd, path: &P, oflag: OFlag, mode: Mode) -> Result<RawFd> {
150-
let fd = try!(path.with_nix_path(|cstr| {
150+
let fd = path.with_nix_path(|cstr| {
151151
unsafe { libc::openat(dirfd, cstr.as_ptr(), oflag.bits(), mode.bits() as c_uint) }
152-
}));
152+
})?;
153153
Errno::result(fd)
154154
}
155155

@@ -167,18 +167,18 @@ fn wrap_readlink_result(buffer: &mut[u8], res: ssize_t) -> Result<&OsStr> {
167167
}
168168

169169
pub fn readlink<'a, P: ?Sized + NixPath>(path: &P, buffer: &'a mut [u8]) -> Result<&'a OsStr> {
170-
let res = try!(path.with_nix_path(|cstr| {
170+
let res = path.with_nix_path(|cstr| {
171171
unsafe { libc::readlink(cstr.as_ptr(), buffer.as_mut_ptr() as *mut c_char, buffer.len() as size_t) }
172-
}));
172+
})?;
173173

174174
wrap_readlink_result(buffer, res)
175175
}
176176

177177

178178
pub fn readlinkat<'a, P: ?Sized + NixPath>(dirfd: RawFd, path: &P, buffer: &'a mut [u8]) -> Result<&'a OsStr> {
179-
let res = try!(path.with_nix_path(|cstr| {
179+
let res = path.with_nix_path(|cstr| {
180180
unsafe { libc::readlinkat(dirfd, cstr.as_ptr(), buffer.as_mut_ptr() as *mut c_char, buffer.len() as size_t) }
181-
}));
181+
})?;
182182

183183
wrap_readlink_result(buffer, res)
184184
}

src/mount.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub fn mount<P1: ?Sized + NixPath, P2: ?Sized + NixPath, P3: ?Sized + NixPath, P
6363
data: Option<&P4>) -> Result<()> {
6464
use libc;
6565

66-
let res = try!(try!(try!(try!(
66+
let res =
6767
source.with_nix_path(|source| {
6868
target.with_nix_path(|target| {
6969
fstype.with_nix_path(|fstype| {
@@ -78,23 +78,23 @@ pub fn mount<P1: ?Sized + NixPath, P2: ?Sized + NixPath, P3: ?Sized + NixPath, P
7878
})
7979
})
8080
})
81-
})))));
81+
})????;
8282

8383
Errno::result(res).map(drop)
8484
}
8585

8686
pub fn umount<P: ?Sized + NixPath>(target: &P) -> Result<()> {
87-
let res = try!(target.with_nix_path(|cstr| {
87+
let res = target.with_nix_path(|cstr| {
8888
unsafe { libc::umount(cstr.as_ptr()) }
89-
}));
89+
})?;
9090

9191
Errno::result(res).map(drop)
9292
}
9393

9494
pub fn umount2<P: ?Sized + NixPath>(target: &P, flags: MntFlags) -> Result<()> {
95-
let res = try!(target.with_nix_path(|cstr| {
95+
let res = target.with_nix_path(|cstr| {
9696
unsafe { libc::umount2(cstr.as_ptr(), flags.bits) }
97-
}));
97+
})?;
9898

9999
Errno::result(res).map(drop)
100100
}

src/mqueue.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ pub fn mq_setattr(mqd: mqd_t, newattr: &MqAttr) -> Result<MqAttr> {
152152
/// Sets the `O_NONBLOCK` attribute for a given message queue descriptor
153153
/// Returns the old attributes
154154
pub fn mq_set_nonblock(mqd: mqd_t) -> Result<(MqAttr)> {
155-
let oldattr = try!(mq_getattr(mqd));
155+
let oldattr = mq_getattr(mqd)?;
156156
let newattr = MqAttr::new(MQ_OFlag::O_NONBLOCK.bits() as c_long,
157157
oldattr.mq_attr.mq_maxmsg,
158158
oldattr.mq_attr.mq_msgsize,
@@ -164,7 +164,7 @@ pub fn mq_set_nonblock(mqd: mqd_t) -> Result<(MqAttr)> {
164164
/// Removes `O_NONBLOCK` attribute for a given message queue descriptor
165165
/// Returns the old attributes
166166
pub fn mq_remove_nonblock(mqd: mqd_t) -> Result<(MqAttr)> {
167-
let oldattr = try!(mq_getattr(mqd));
167+
let oldattr = mq_getattr(mqd)?;
168168
let newattr = MqAttr::new(0,
169169
oldattr.mq_attr.mq_maxmsg,
170170
oldattr.mq_attr.mq_msgsize,

src/net/if_.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use {Result, Error, NixPath};
99

1010
/// Resolve an interface into a interface number.
1111
pub fn if_nametoindex<P: ?Sized + NixPath>(name: &P) -> Result<c_uint> {
12-
let if_index = try!(name.with_nix_path(|name| unsafe { libc::if_nametoindex(name.as_ptr()) }));
12+
let if_index = name.with_nix_path(|name| unsafe { libc::if_nametoindex(name.as_ptr()) })?;
1313

1414
if if_index == 0 {
1515
Err(Error::last())

src/sys/mman.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ pub unsafe fn msync(addr: *mut c_void, length: size_t, flags: MsFlags) -> Result
264264

265265
#[cfg(not(target_os = "android"))]
266266
pub fn shm_open<P: ?Sized + NixPath>(name: &P, flag: OFlag, mode: Mode) -> Result<RawFd> {
267-
let ret = try!(name.with_nix_path(|cstr| {
267+
let ret = name.with_nix_path(|cstr| {
268268
#[cfg(any(target_os = "macos", target_os = "ios"))]
269269
unsafe {
270270
libc::shm_open(cstr.as_ptr(), flag.bits(), mode.bits() as libc::c_uint)
@@ -273,16 +273,16 @@ pub fn shm_open<P: ?Sized + NixPath>(name: &P, flag: OFlag, mode: Mode) -> Resul
273273
unsafe {
274274
libc::shm_open(cstr.as_ptr(), flag.bits(), mode.bits() as libc::mode_t)
275275
}
276-
}));
276+
})?;
277277

278278
Errno::result(ret)
279279
}
280280

281281
#[cfg(not(target_os = "android"))]
282282
pub fn shm_unlink<P: ?Sized + NixPath>(name: &P) -> Result<()> {
283-
let ret = try!(name.with_nix_path(|cstr| {
283+
let ret = name.with_nix_path(|cstr| {
284284
unsafe { libc::shm_unlink(cstr.as_ptr()) }
285-
}));
285+
})?;
286286

287287
Errno::result(ret).map(drop)
288288
}

src/sys/quota.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -231,24 +231,22 @@ impl Dqblk {
231231
fn quotactl<P: ?Sized + NixPath>(cmd: QuotaCmd, special: Option<&P>, id: c_int, addr: *mut c_char) -> Result<()> {
232232
unsafe {
233233
Errno::clear();
234-
let res = try!(
235-
match special {
236-
Some(dev) => dev.with_nix_path(|path| libc::quotactl(cmd.as_int(), path.as_ptr(), id, addr)),
237-
None => Ok(libc::quotactl(cmd.as_int(), ptr::null(), id, addr)),
238-
}
239-
);
234+
let res = match special {
235+
Some(dev) => dev.with_nix_path(|path| libc::quotactl(cmd.as_int(), path.as_ptr(), id, addr)),
236+
None => Ok(libc::quotactl(cmd.as_int(), ptr::null(), id, addr)),
237+
}?;
240238

241239
Errno::result(res).map(drop)
242240
}
243241
}
244242

245243
/// Turn on disk quotas for a block device.
246244
pub fn quotactl_on<P: ?Sized + NixPath>(which: QuotaType, special: &P, format: QuotaFmt, quota_file: &P) -> Result<()> {
247-
try!(quota_file.with_nix_path(|path| {
245+
quota_file.with_nix_path(|path| {
248246
let mut path_copy = path.to_bytes_with_nul().to_owned();
249247
let p: *mut c_char = path_copy.as_mut_ptr() as *mut c_char;
250248
quotactl(QuotaCmd(QuotaSubCmd::Q_QUOTAON, which), Some(special), format as c_int, p)
251-
}))
249+
})?
252250
}
253251

254252
/// Disable disk quotas for a block device.

src/sys/signal.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ impl SigSet {
413413
/// Gets the currently blocked (masked) set of signals for the calling thread.
414414
pub fn thread_get_mask() -> Result<SigSet> {
415415
let mut oldmask: SigSet = unsafe { mem::uninitialized() };
416-
try!(pthread_sigmask(SigmaskHow::SIG_SETMASK, None, Some(&mut oldmask)));
416+
pthread_sigmask(SigmaskHow::SIG_SETMASK, None, Some(&mut oldmask))?;
417417
Ok(oldmask)
418418
}
419419

@@ -435,7 +435,7 @@ impl SigSet {
435435
/// Sets the set of signals as the signal mask, and returns the old mask.
436436
pub fn thread_swap_mask(&self, how: SigmaskHow) -> Result<SigSet> {
437437
let mut oldmask: SigSet = unsafe { mem::uninitialized() };
438-
try!(pthread_sigmask(how, Some(self), Some(&mut oldmask)));
438+
pthread_sigmask(how, Some(self), Some(&mut oldmask))?;
439439
Ok(oldmask)
440440
}
441441

src/sys/signalfd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl SignalFd {
8888
}
8989

9090
pub fn with_flags(mask: &SigSet, flags: SfdFlags) -> Result<SignalFd> {
91-
let fd = try!(signalfd(SIGNALFD_NEW, mask, flags));
91+
let fd = signalfd(SIGNALFD_NEW, mask, flags)?;
9292

9393
Ok(SignalFd(fd))
9494
}

src/sys/socket/addr.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ pub struct UnixAddr(pub libc::sockaddr_un, pub usize);
600600
impl UnixAddr {
601601
/// Create a new sockaddr_un representing a filesystem path.
602602
pub fn new<P: ?Sized + NixPath>(path: &P) -> Result<UnixAddr> {
603-
try!(path.with_nix_path(|cstr| {
603+
path.with_nix_path(|cstr| {
604604
unsafe {
605605
let mut ret = libc::sockaddr_un {
606606
sun_family: AddressFamily::Unix as sa_family_t,
@@ -619,7 +619,7 @@ impl UnixAddr {
619619

620620
Ok(UnixAddr(ret, bytes.len()))
621621
}
622-
}))
622+
})?
623623
}
624624

625625
/// Create a new `sockaddr_un` representing an address in the "abstract namespace".
@@ -759,7 +759,7 @@ impl SockAddr {
759759
}
760760

761761
pub fn new_unix<P: ?Sized + NixPath>(path: &P) -> Result<SockAddr> {
762-
Ok(SockAddr::Unix(try!(UnixAddr::new(path))))
762+
Ok(SockAddr::Unix(UnixAddr::new(path)?))
763763
}
764764

765765
#[cfg(any(target_os = "android", target_os = "linux"))]
@@ -1079,7 +1079,7 @@ pub mod sys_control {
10791079
ctl_name[..name.len()].clone_from_slice(name.as_bytes());
10801080
let mut info = ctl_ioc_info { ctl_id: 0, ctl_name: ctl_name };
10811081

1082-
unsafe { try!(ctl_info(sockfd, &mut info)); }
1082+
unsafe { ctl_info(sockfd, &mut info)?; }
10831083

10841084
Ok(SysControlAddr::new(info.ctl_id, unit))
10851085
}

src/sys/socket/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ pub fn recvmsg<'a, T>(fd: RawFd, iov: &[IoVec<&mut [u8]>], cmsg_buffer: Option<&
750750
};
751751

752752
Ok(unsafe { RecvMsg {
753-
bytes: try!(Errno::result(ret)) as usize,
753+
bytes: Errno::result(ret)? as usize,
754754
cmsg_buffer,
755755
address: sockaddr_storage_to_addr(&address,
756756
mhdr.msg_namelen as usize).ok(),
@@ -890,13 +890,13 @@ pub fn recvfrom(sockfd: RawFd, buf: &mut [u8]) -> Result<(usize, SockAddr)> {
890890
let addr: sockaddr_storage = mem::zeroed();
891891
let mut len = mem::size_of::<sockaddr_storage>() as socklen_t;
892892

893-
let ret = try!(Errno::result(libc::recvfrom(
893+
let ret = Errno::result(libc::recvfrom(
894894
sockfd,
895895
buf.as_ptr() as *mut c_void,
896896
buf.len() as size_t,
897897
0,
898898
mem::transmute(&addr),
899-
&mut len as *mut socklen_t)));
899+
&mut len as *mut socklen_t))?;
900900

901901
sockaddr_storage_to_addr(&addr, len as usize)
902902
.map(|addr| (ret as usize, addr))
@@ -1004,7 +1004,7 @@ pub fn getpeername(fd: RawFd) -> Result<SockAddr> {
10041004

10051005
let ret = libc::getpeername(fd, mem::transmute(&addr), &mut len);
10061006

1007-
try!(Errno::result(ret));
1007+
Errno::result(ret)?;
10081008

10091009
sockaddr_storage_to_addr(&addr, len as usize)
10101010
}
@@ -1020,7 +1020,7 @@ pub fn getsockname(fd: RawFd) -> Result<SockAddr> {
10201020

10211021
let ret = libc::getsockname(fd, mem::transmute(&addr), &mut len);
10221022

1023-
try!(Errno::result(ret));
1023+
Errno::result(ret)?;
10241024

10251025
sockaddr_storage_to_addr(&addr, len as usize)
10261026
}

src/sys/socket/sockopt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ macro_rules! getsockopt_impl {
8989
let res = libc::getsockopt(fd, $level, $flag,
9090
getter.ffi_ptr(),
9191
getter.ffi_len());
92-
try!(Errno::result(res));
92+
Errno::result(res)?;
9393

9494
Ok(getter.unwrap())
9595
}

src/sys/stat.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ libc_bitflags! {
4343
}
4444

4545
pub fn mknod<P: ?Sized + NixPath>(path: &P, kind: SFlag, perm: Mode, dev: dev_t) -> Result<()> {
46-
let res = try!(path.with_nix_path(|cstr| {
46+
let res = path.with_nix_path(|cstr| {
4747
unsafe {
4848
libc::mknod(cstr.as_ptr(), kind.bits | perm.bits() as mode_t, dev)
4949
}
50-
}));
50+
})?;
5151

5252
Errno::result(res).map(drop)
5353
}
@@ -79,26 +79,26 @@ pub fn umask(mode: Mode) -> Mode {
7979

8080
pub fn stat<P: ?Sized + NixPath>(path: &P) -> Result<FileStat> {
8181
let mut dst = unsafe { mem::uninitialized() };
82-
let res = try!(path.with_nix_path(|cstr| {
82+
let res = path.with_nix_path(|cstr| {
8383
unsafe {
8484
libc::stat(cstr.as_ptr(), &mut dst as *mut FileStat)
8585
}
86-
}));
86+
})?;
8787

88-
try!(Errno::result(res));
88+
Errno::result(res)?;
8989

9090
Ok(dst)
9191
}
9292

9393
pub fn lstat<P: ?Sized + NixPath>(path: &P) -> Result<FileStat> {
9494
let mut dst = unsafe { mem::uninitialized() };
95-
let res = try!(path.with_nix_path(|cstr| {
95+
let res = path.with_nix_path(|cstr| {
9696
unsafe {
9797
libc::lstat(cstr.as_ptr(), &mut dst as *mut FileStat)
9898
}
99-
}));
99+
})?;
100100

101-
try!(Errno::result(res));
101+
Errno::result(res)?;
102102

103103
Ok(dst)
104104
}
@@ -107,18 +107,18 @@ pub fn fstat(fd: RawFd) -> Result<FileStat> {
107107
let mut dst = unsafe { mem::uninitialized() };
108108
let res = unsafe { libc::fstat(fd, &mut dst as *mut FileStat) };
109109

110-
try!(Errno::result(res));
110+
Errno::result(res)?;
111111

112112
Ok(dst)
113113
}
114114

115115
pub fn fstatat<P: ?Sized + NixPath>(dirfd: RawFd, pathname: &P, f: AtFlags) -> Result<FileStat> {
116116
let mut dst = unsafe { mem::uninitialized() };
117-
let res = try!(pathname.with_nix_path(|cstr| {
117+
let res = pathname.with_nix_path(|cstr| {
118118
unsafe { libc::fstatat(dirfd, cstr.as_ptr(), &mut dst as *mut FileStat, f.bits() as libc::c_int) }
119-
}));
119+
})?;
120120

121-
try!(Errno::result(res));
121+
Errno::result(res)?;
122122

123123
Ok(dst)
124124
}

src/sys/statfs.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ use libc;
66
pub fn statfs<P: ?Sized + NixPath>(path: &P, stat: &mut libc::statfs) -> Result<()> {
77
unsafe {
88
Errno::clear();
9-
let res = try!(
10-
path.with_nix_path(|path| libc::statfs(path.as_ptr(), stat))
11-
);
9+
let res = path.with_nix_path(|path| libc::statfs(path.as_ptr(), stat))?;
1210

1311
Errno::result(res).map(drop)
1412
}

src/sys/statvfs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ pub fn statvfs<P: ?Sized + NixPath>(path: &P) -> Result<Statvfs> {
126126
unsafe {
127127
Errno::clear();
128128
let mut stat: Statvfs = mem::uninitialized();
129-
let res = try!(
130-
path.with_nix_path(|path| libc::statvfs(path.as_ptr(), &mut stat.0))
131-
);
129+
let res = path.with_nix_path(|path|
130+
libc::statvfs(path.as_ptr(), &mut stat.0)
131+
)?;
132132

133133
Errno::result(res).map(|_| stat)
134134
}

src/sys/termios.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1050,7 +1050,7 @@ pub fn tcgetattr(fd: RawFd) -> Result<Termios> {
10501050

10511051
let res = unsafe { libc::tcgetattr(fd, &mut termios) };
10521052

1053-
try!(Errno::result(res));
1053+
Errno::result(res)?;
10541054

10551055
Ok(termios.into())
10561056
}

0 commit comments

Comments
 (0)