Skip to content

Replace try! with ? #995

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,17 @@ libc_bitflags!(
);

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

Errno::result(fd)
}

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

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

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

wrap_readlink_result(buffer, res)
}


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

wrap_readlink_result(buffer, res)
}
Expand Down
12 changes: 6 additions & 6 deletions src/mount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub fn mount<P1: ?Sized + NixPath, P2: ?Sized + NixPath, P3: ?Sized + NixPath, P
data: Option<&P4>) -> Result<()> {
use libc;

let res = try!(try!(try!(try!(
let res =
source.with_nix_path(|source| {
target.with_nix_path(|target| {
fstype.with_nix_path(|fstype| {
Expand All @@ -78,23 +78,23 @@ pub fn mount<P1: ?Sized + NixPath, P2: ?Sized + NixPath, P3: ?Sized + NixPath, P
})
})
})
})))));
})????;

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

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

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

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

Errno::result(res).map(drop)
}
4 changes: 2 additions & 2 deletions src/mqueue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ pub fn mq_setattr(mqd: mqd_t, newattr: &MqAttr) -> Result<MqAttr> {
/// Sets the `O_NONBLOCK` attribute for a given message queue descriptor
/// Returns the old attributes
pub fn mq_set_nonblock(mqd: mqd_t) -> Result<(MqAttr)> {
let oldattr = try!(mq_getattr(mqd));
let oldattr = mq_getattr(mqd)?;
let newattr = MqAttr::new(MQ_OFlag::O_NONBLOCK.bits() as c_long,
oldattr.mq_attr.mq_maxmsg,
oldattr.mq_attr.mq_msgsize,
Expand All @@ -164,7 +164,7 @@ pub fn mq_set_nonblock(mqd: mqd_t) -> Result<(MqAttr)> {
/// Removes `O_NONBLOCK` attribute for a given message queue descriptor
/// Returns the old attributes
pub fn mq_remove_nonblock(mqd: mqd_t) -> Result<(MqAttr)> {
let oldattr = try!(mq_getattr(mqd));
let oldattr = mq_getattr(mqd)?;
let newattr = MqAttr::new(0,
oldattr.mq_attr.mq_maxmsg,
oldattr.mq_attr.mq_msgsize,
Expand Down
2 changes: 1 addition & 1 deletion src/net/if_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use {Result, Error, NixPath};

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

if if_index == 0 {
Err(Error::last())
Expand Down
8 changes: 4 additions & 4 deletions src/sys/mman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ pub unsafe fn msync(addr: *mut c_void, length: size_t, flags: MsFlags) -> Result

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

Errno::result(ret)
}

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

Errno::result(ret).map(drop)
}
14 changes: 6 additions & 8 deletions src/sys/quota.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,24 +231,22 @@ impl Dqblk {
fn quotactl<P: ?Sized + NixPath>(cmd: QuotaCmd, special: Option<&P>, id: c_int, addr: *mut c_char) -> Result<()> {
unsafe {
Errno::clear();
let res = try!(
match special {
Some(dev) => dev.with_nix_path(|path| libc::quotactl(cmd.as_int(), path.as_ptr(), id, addr)),
None => Ok(libc::quotactl(cmd.as_int(), ptr::null(), id, addr)),
}
);
let res = match special {
Some(dev) => dev.with_nix_path(|path| libc::quotactl(cmd.as_int(), path.as_ptr(), id, addr)),
None => Ok(libc::quotactl(cmd.as_int(), ptr::null(), id, addr)),
}?;

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

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

/// Disable disk quotas for a block device.
Expand Down
4 changes: 2 additions & 2 deletions src/sys/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ impl SigSet {
/// Gets the currently blocked (masked) set of signals for the calling thread.
pub fn thread_get_mask() -> Result<SigSet> {
let mut oldmask: SigSet = unsafe { mem::uninitialized() };
try!(pthread_sigmask(SigmaskHow::SIG_SETMASK, None, Some(&mut oldmask)));
pthread_sigmask(SigmaskHow::SIG_SETMASK, None, Some(&mut oldmask))?;
Ok(oldmask)
}

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

Expand Down
2 changes: 1 addition & 1 deletion src/sys/signalfd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl SignalFd {
}

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

Ok(SignalFd(fd))
}
Expand Down
8 changes: 4 additions & 4 deletions src/sys/socket/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ pub struct UnixAddr(pub libc::sockaddr_un, pub usize);
impl UnixAddr {
/// Create a new sockaddr_un representing a filesystem path.
pub fn new<P: ?Sized + NixPath>(path: &P) -> Result<UnixAddr> {
try!(path.with_nix_path(|cstr| {
path.with_nix_path(|cstr| {
unsafe {
let mut ret = libc::sockaddr_un {
sun_family: AddressFamily::Unix as sa_family_t,
Expand All @@ -619,7 +619,7 @@ impl UnixAddr {

Ok(UnixAddr(ret, bytes.len()))
}
}))
})?
}

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

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

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

unsafe { try!(ctl_info(sockfd, &mut info)); }
unsafe { ctl_info(sockfd, &mut info)?; }

Ok(SysControlAddr::new(info.ctl_id, unit))
}
Expand Down
10 changes: 5 additions & 5 deletions src/sys/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ pub fn recvmsg<'a, T>(fd: RawFd, iov: &[IoVec<&mut [u8]>], cmsg_buffer: Option<&
};

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

let ret = try!(Errno::result(libc::recvfrom(
let ret = Errno::result(libc::recvfrom(
sockfd,
buf.as_ptr() as *mut c_void,
buf.len() as size_t,
0,
mem::transmute(&addr),
&mut len as *mut socklen_t)));
&mut len as *mut socklen_t))?;

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

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

try!(Errno::result(ret));
Errno::result(ret)?;

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

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

try!(Errno::result(ret));
Errno::result(ret)?;

sockaddr_storage_to_addr(&addr, len as usize)
}
Expand Down
2 changes: 1 addition & 1 deletion src/sys/socket/sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ macro_rules! getsockopt_impl {
let res = libc::getsockopt(fd, $level, $flag,
getter.ffi_ptr(),
getter.ffi_len());
try!(Errno::result(res));
Errno::result(res)?;

Ok(getter.unwrap())
}
Expand Down
24 changes: 12 additions & 12 deletions src/sys/stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ libc_bitflags! {
}

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

Errno::result(res).map(drop)
}
Expand Down Expand Up @@ -79,26 +79,26 @@ pub fn umask(mode: Mode) -> Mode {

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

try!(Errno::result(res));
Errno::result(res)?;

Ok(dst)
}

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

try!(Errno::result(res));
Errno::result(res)?;

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

try!(Errno::result(res));
Errno::result(res)?;

Ok(dst)
}

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

try!(Errno::result(res));
Errno::result(res)?;

Ok(dst)
}
Expand Down
4 changes: 1 addition & 3 deletions src/sys/statfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ use libc;
pub fn statfs<P: ?Sized + NixPath>(path: &P, stat: &mut libc::statfs) -> Result<()> {
unsafe {
Errno::clear();
let res = try!(
path.with_nix_path(|path| libc::statfs(path.as_ptr(), stat))
);
let res = path.with_nix_path(|path| libc::statfs(path.as_ptr(), stat))?;

Errno::result(res).map(drop)
}
Expand Down
6 changes: 3 additions & 3 deletions src/sys/statvfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ pub fn statvfs<P: ?Sized + NixPath>(path: &P) -> Result<Statvfs> {
unsafe {
Errno::clear();
let mut stat: Statvfs = mem::uninitialized();
let res = try!(
path.with_nix_path(|path| libc::statvfs(path.as_ptr(), &mut stat.0))
);
let res = path.with_nix_path(|path|
libc::statvfs(path.as_ptr(), &mut stat.0)
)?;

Errno::result(res).map(|_| stat)
}
Expand Down
2 changes: 1 addition & 1 deletion src/sys/termios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ pub fn tcgetattr(fd: RawFd) -> Result<Termios> {

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

try!(Errno::result(res));
Errno::result(res)?;

Ok(termios.into())
}
Expand Down
Loading