From b09f7e860f4facb17e93e16323f1717c0ee74580 Mon Sep 17 00:00:00 2001 From: Marcin Mielniczuk Date: Sat, 7 Sep 2019 19:39:10 +0200 Subject: [PATCH] Refactor try_from and try_into error handing. --- .vscode/settings.json | 7 +++++++ src/helpers.rs | 2 +- src/hostcalls_impl/misc.rs | 4 ++-- src/sys/unix/host_impl.rs | 4 ++-- src/sys/unix/hostcalls_impl/fs.rs | 33 +++++++++++-------------------- 5 files changed, 24 insertions(+), 26 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000000..90e722fc0c --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "editor.formatOnSave": true, + "rust-client.disableRustup": false, + "rust.target": "x86_64-pc-windows-gnu", + //"rust.sysroot": "/home/marcin/.rustup/toolchains/stable-x86_64-pc-windows-gnu/", + "rust.all_targets": false +} \ No newline at end of file diff --git a/src/helpers.rs b/src/helpers.rs index 3e8d7f8e46..dc770f7e71 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -6,5 +6,5 @@ pub(crate) fn systemtime_to_timestamp(st: SystemTime) -> Result { .map_err(|_| Error::EINVAL)? // date earlier than UNIX_EPOCH .as_nanos() .try_into() - .map_err(|_| Error::EOVERFLOW) // u128 doesn't fit into u64 + .map_err(Into::into) // u128 doesn't fit into u64 } diff --git a/src/hostcalls_impl/misc.rs b/src/hostcalls_impl/misc.rs index 7a08ac175e..04f8a836bd 100644 --- a/src/hostcalls_impl/misc.rs +++ b/src/hostcalls_impl/misc.rs @@ -29,7 +29,7 @@ pub(crate) fn args_get( argv.push(arg_ptr); - let len = wasm32::uintptr_t::try_from(arg_bytes.len()).map_err(|_| Error::EOVERFLOW)?; + let len = wasm32::uintptr_t::try_from(arg_bytes.len())?; argv_buf_offset = argv_buf_offset.checked_add(len).ok_or(Error::EOVERFLOW)?; } @@ -87,7 +87,7 @@ pub(crate) fn environ_get( environ.push(env_ptr); - let len = wasm32::uintptr_t::try_from(env_bytes.len()).map_err(|_| Error::EOVERFLOW)?; + let len = wasm32::uintptr_t::try_from(env_bytes.len())?; environ_buf_offset = environ_buf_offset .checked_add(len) .ok_or(Error::EOVERFLOW)?; diff --git a/src/sys/unix/host_impl.rs b/src/sys/unix/host_impl.rs index 1294c49455..468d98c3d0 100644 --- a/src/sys/unix/host_impl.rs +++ b/src/sys/unix/host_impl.rs @@ -187,8 +187,8 @@ pub(crate) fn filestat_from_nix( } let filetype = nix::sys::stat::SFlag::from_bits_truncate(filestat.st_mode); - let dev = host::__wasi_device_t::try_from(filestat.st_dev).map_err(|_| Error::EOVERFLOW)?; - let ino = host::__wasi_inode_t::try_from(filestat.st_ino).map_err(|_| Error::EOVERFLOW)?; + let dev = host::__wasi_device_t::try_from(filestat.st_dev)?; + let ino = host::__wasi_inode_t::try_from(filestat.st_ino)?; let st_atim = filestat_to_timestamp(filestat.st_atime as u64, filestat.st_atime_nsec as u64)?; let st_ctim = filestat_to_timestamp(filestat.st_ctime as u64, filestat.st_ctime_nsec as u64)?; let st_mtim = filestat_to_timestamp(filestat.st_mtime as u64, filestat.st_mtime_nsec as u64)?; diff --git a/src/sys/unix/hostcalls_impl/fs.rs b/src/sys/unix/hostcalls_impl/fs.rs index 0771910749..04dde6277b 100644 --- a/src/sys/unix/hostcalls_impl/fs.rs +++ b/src/sys/unix/hostcalls_impl/fs.rs @@ -4,7 +4,7 @@ use super::fs_helpers::*; use crate::helpers::systemtime_to_timestamp; use crate::hostcalls_impl::PathGet; use crate::sys::host_impl::{self, errno_from_nix}; -use crate::{host, Result, Error}; +use crate::{host, Error, Result}; use nix::libc::{self, c_long, c_void}; use std::convert::TryInto; use std::ffi::CString; @@ -51,8 +51,8 @@ pub(crate) fn fd_advise( { use nix::fcntl::{posix_fadvise, PosixFadviseAdvice}; - let offset = offset.try_into().map_err(|_| Error::EOVERFLOW)?; - let len = len.try_into().map_err(|_| Error::EOVERFLOW)?; + let offset = offset.try_into()?; + let len = len.try_into()?; let host_advice = match advice { host::__WASI_ADVICE_DONTNEED => PosixFadviseAdvice::POSIX_FADV_DONTNEED, host::__WASI_ADVICE_SEQUENTIAL => PosixFadviseAdvice::POSIX_FADV_SEQUENTIAL, @@ -102,10 +102,8 @@ pub(crate) fn path_create_directory(resolved: PathGet) -> Result<()> { pub(crate) fn path_link(resolved_old: PathGet, resolved_new: PathGet) -> Result<()> { use nix::libc::linkat; - let old_path_cstr = - CString::new(resolved_old.path().as_bytes()).map_err(|_| Error::EILSEQ)?; - let new_path_cstr = - CString::new(resolved_new.path().as_bytes()).map_err(|_| Error::EILSEQ)?; + let old_path_cstr = CString::new(resolved_old.path().as_bytes()).map_err(|_| Error::EILSEQ)?; + let new_path_cstr = CString::new(resolved_new.path().as_bytes()).map_err(|_| Error::EILSEQ)?; // Not setting AT_SYMLINK_FOLLOW fails on most filesystems let atflags = libc::AT_SYMLINK_FOLLOW; @@ -305,10 +303,8 @@ pub(crate) fn path_readlink(resolved: PathGet, buf: &mut [u8]) -> Result pub(crate) fn path_rename(resolved_old: PathGet, resolved_new: PathGet) -> Result<()> { use nix::libc::renameat; - let old_path_cstr = - CString::new(resolved_old.path().as_bytes()).map_err(|_| Error::EILSEQ)?; - let new_path_cstr = - CString::new(resolved_new.path().as_bytes()).map_err(|_| Error::EILSEQ)?; + let old_path_cstr = CString::new(resolved_old.path().as_bytes()).map_err(|_| Error::EILSEQ)?; + let new_path_cstr = CString::new(resolved_new.path().as_bytes()).map_err(|_| Error::EILSEQ)?; let res = unsafe { renameat( @@ -332,14 +328,10 @@ pub(crate) fn fd_filestat_get_impl(file: &std::fs::File) -> Result Result<()> { use nix::libc::symlinkat; let old_path_cstr = CString::new(old_path.as_bytes()).map_err(|_| Error::EILSEQ)?; - let new_path_cstr = - CString::new(resolved.path().as_bytes()).map_err(|_| Error::EILSEQ)?; + let new_path_cstr = CString::new(resolved.path().as_bytes()).map_err(|_| Error::EILSEQ)?; let res = unsafe { symlinkat(