Use ? instead of try

This commit is contained in:
Michael Doronin
2020-10-28 15:45:49 +03:00
parent ed3d9e32de
commit 554acf08b5
2 changed files with 18 additions and 18 deletions

View File

@@ -148,7 +148,7 @@ impl<'a> SmbClient<'a> {
}; };
unsafe { unsafe {
let ctx = try!(result_from_ptr_mut(smbc_new_context())); let ctx = result_from_ptr_mut(smbc_new_context())?;
smbc_setOptionUserData(ctx, auth_fn as *const _ as *mut c_void); smbc_setOptionUserData(ctx, auth_fn as *const _ as *mut c_void);
smbc_setFunctionAuthDataWithContext(ctx, Some(Self::auth_wrapper::<F>)); smbc_setFunctionAuthDataWithContext(ctx, Some(Self::auth_wrapper::<F>));
@@ -158,7 +158,7 @@ impl<'a> SmbClient<'a> {
smbc_setOptionDebugToStderr(ctx, SMBC_TRUE); smbc_setOptionDebugToStderr(ctx, SMBC_TRUE);
//smbc_setDebug(ctx, 10); //smbc_setDebug(ctx, 10);
smbc.ctx = try!(result_from_ptr_mut(smbc_init_context(ctx))); smbc.ctx = result_from_ptr_mut(smbc_init_context(ctx))?;
} }
trace!(target: "smbc", "new smbclient"); trace!(target: "smbc", "new smbclient");
@@ -208,14 +208,14 @@ impl<'a> SmbClient<'a> {
let open_fn = try_ufn!(smbc_getFunctionOpen <- self); let open_fn = try_ufn!(smbc_getFunctionOpen <- self);
let path = try!(cstring(path)); let path = cstring(path)?;
trace!(target: "smbc", "opening {:?} with {:?}", path, open_fn); trace!(target: "smbc", "opening {:?} with {:?}", path, open_fn);
unsafe { unsafe {
let fd = try!(result_from_ptr_mut(open_fn(self.ctx, let fd = result_from_ptr_mut(open_fn(self.ctx,
path.as_ptr(), path.as_ptr(),
try!(options.to_flags()), try!(options.to_flags()),
options.mode))); options.mode))?;
if (fd as i64) < 0 { if (fd as i64) < 0 {
trace!(target: "smbc", "neg fd"); trace!(target: "smbc", "neg fd");
} }
@@ -273,7 +273,7 @@ impl<'a> SmbClient<'a> {
/// Get metadata for file at `path` /// Get metadata for file at `path`
pub fn metadata<P: AsRef<str>>(&self, path: P) -> Result<()> { pub fn metadata<P: AsRef<str>>(&self, path: P) -> Result<()> {
let stat_fn = try_ufn!(smbc_getFunctionStat <- self); let stat_fn = try_ufn!(smbc_getFunctionStat <- self);
let path = try!(cstring(path)); let path = cstring(path)?;
unimplemented!(); unimplemented!();
} }
@@ -281,8 +281,8 @@ impl<'a> SmbClient<'a> {
/// Create new directory at SMB `path` /// Create new directory at SMB `path`
pub fn create_dir<P: AsRef<str>>(&self, path: P) -> Result<()> { pub fn create_dir<P: AsRef<str>>(&self, path: P) -> Result<()> {
let mkdir_fn = try_ufn!(smbc_getFunctionMkdir <- self); let mkdir_fn = try_ufn!(smbc_getFunctionMkdir <- self);
let path = try!(cstring(path)); let path = cstring(path)?;
try!(to_result_with_le(unsafe { mkdir_fn(self.ctx, path.as_ptr(), 0o755) })); to_result_with_le(unsafe { mkdir_fn(self.ctx, path.as_ptr(), 0o755) })?;
Ok(()) Ok(())
} }
@@ -295,8 +295,8 @@ impl<'a> SmbClient<'a> {
/// Directory should be empty to delete it. /// Directory should be empty to delete it.
pub fn remove_dir<P: AsRef<str>>(&self, path: P) -> Result<()> { pub fn remove_dir<P: AsRef<str>>(&self, path: P) -> Result<()> {
let rmdir_fn = try_ufn!(smbc_getFunctionRmdir <- self); let rmdir_fn = try_ufn!(smbc_getFunctionRmdir <- self);
let path = try!(cstring(path)); let path = cstring(path)?;
try!(to_result_with_le(unsafe { rmdir_fn(self.ctx, path.as_ptr()) })); to_result_with_le(unsafe { rmdir_fn(self.ctx, path.as_ptr()) })?;
Ok(()) Ok(())
} }
} // 2}}} } // 2}}}
@@ -426,12 +426,12 @@ impl<'a, 'b> Read for SmbFile<'a, 'b> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
trace!(target: "smbc", "reading file to buf [{:?};{}]", buf.as_ptr(), buf.len()); trace!(target: "smbc", "reading file to buf [{:?};{}]", buf.as_ptr(), buf.len());
let read_fn = try_ufn!(smbc_getFunctionRead <- self.smbc); let read_fn = try_ufn!(smbc_getFunctionRead <- self.smbc);
let bytes_read = try!(to_result_with_le(unsafe { let bytes_read = to_result_with_le(unsafe {
read_fn(self.smbc.ctx, read_fn(self.smbc.ctx,
self.fd, self.fd,
buf.as_mut_ptr() as *mut c_void, buf.as_mut_ptr() as *mut c_void,
buf.len() as _) buf.len() as _)
})); })?;
Ok(bytes_read as usize) Ok(bytes_read as usize)
} }
} // }}} } // }}}
@@ -441,12 +441,12 @@ impl<'a, 'b> Write for SmbFile<'a, 'b> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
trace!(target: "smbc", "writing buf [{:?};{}] to file", buf.as_ptr(), buf.len()); trace!(target: "smbc", "writing buf [{:?};{}] to file", buf.as_ptr(), buf.len());
let write_fn = try_ufn!(smbc_getFunctionWrite <- self.smbc); let write_fn = try_ufn!(smbc_getFunctionWrite <- self.smbc);
let bytes_wrote = try!(to_result_with_le(unsafe { let bytes_wrote = to_result_with_le(unsafe {
write_fn(self.smbc.ctx, write_fn(self.smbc.ctx,
self.fd, self.fd,
buf.as_ptr() as *const c_void, buf.as_ptr() as *const c_void,
buf.len() as _) buf.len() as _)
})); })?;
Ok(bytes_wrote as usize) Ok(bytes_wrote as usize)
} }
@@ -466,7 +466,7 @@ impl<'a, 'b> Seek for SmbFile<'a, 'b> {
SeekFrom::End(p) => (libc::SEEK_END, p as off_t), SeekFrom::End(p) => (libc::SEEK_END, p as off_t),
SeekFrom::Current(p) => (libc::SEEK_CUR, p as off_t), SeekFrom::Current(p) => (libc::SEEK_CUR, p as off_t),
}; };
let res = try!(to_result_with_errno(unsafe { lseek_fn(self.smbc.ctx, self.fd, off, whence) }, libc::EINVAL)); let res = to_result_with_errno(unsafe { lseek_fn(self.smbc.ctx, self.fd, off, whence) }, libc::EINVAL)?;
Ok(res as u64) Ok(res as u64)
} }
} // }}} } // }}}

View File

@@ -27,9 +27,9 @@ use result::*;
/// try! get smbc function or return io::Error(EINVAL) /// try! get smbc function or return io::Error(EINVAL)
macro_rules! try_ufn { macro_rules! try_ufn {
($e:ident <- $s:expr) => (try!(unsafe { ($e:ident <- $s:expr) => (unsafe {
$e($s.ctx).ok_or($crate::std::io::Error::from_raw_os_error(libc::EINVAL as i32)) $e($s.ctx).ok_or($crate::std::io::Error::from_raw_os_error(libc::EINVAL as i32))
})) }?)
} }
#[inline(always)] #[inline(always)]
@@ -49,7 +49,7 @@ pub unsafe fn cstr<'a, T>(p: *const T) -> Cow<'a, str> {
} }
pub fn cstring<P: AsRef<str>>(p: P) -> Result<CString> { pub fn cstring<P: AsRef<str>>(p: P) -> Result<CString> {
Ok(try!(CString::new(p.as_ref()))) Ok(CString::new(p.as_ref())?)
} }
pub unsafe fn write_to_cstr(dest: *mut u8, len: usize, src: &str) { pub unsafe fn write_to_cstr(dest: *mut u8, len: usize, src: &str) {