Mercurial > lbo > hg > leveldb-rs
changeset 194:c1ec05394944
error: Remove explicit error conversion functions and just use the From trait.
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Mon, 28 Aug 2017 20:40:08 +0200 |
parents | 7f48d77e61c4 |
children | 03d4b25fd8af |
files | src/disk_env.rs src/env.rs src/error.rs |
diffstat | 3 files changed, 27 insertions(+), 32 deletions(-) [+] |
line wrap: on
line diff
--- a/src/disk_env.rs Mon Aug 28 20:28:54 2017 +0200 +++ b/src/disk_env.rs Mon Aug 28 20:40:08 2017 +0200 @@ -1,6 +1,6 @@ use env::{Env, FileLock, Logger, RandomAccess}; use env_common::{micros, sleep_for}; -use error::{from_io_result, Status, StatusCode, Result}; +use error::{Status, StatusCode, Result}; use std::collections::HashMap; use std::fs; @@ -30,29 +30,33 @@ } } +// Note: We're using Ok(f()?) in several locations below in order to benefit from the automatic +// error conversion using std::convert::From. impl Env for PosixDiskEnv { fn open_sequential_file(&self, p: &Path) -> Result<Box<Read>> { - Ok(Box::new(try!(from_io_result(fs::OpenOptions::new().read(true).open(p))))) + Ok(Box::new(try!(fs::OpenOptions::new().read(true).open(p)))) } fn open_random_access_file(&self, p: &Path) -> Result<Box<RandomAccess>> { - from_io_result(fs::OpenOptions::new().read(true).open(p)).map(|f| { - let b: Box<RandomAccess> = Box::new(f); - b - }) + Ok(fs::OpenOptions::new().read(true) + .open(p) + .map(|f| { + let b: Box<RandomAccess> = Box::new(f); + b + })?) } fn open_writable_file(&self, p: &Path) -> Result<Box<Write>> { - Ok(Box::new(try!(from_io_result(fs::OpenOptions::new() + Ok(Box::new(try!(fs::OpenOptions::new() .create(true) .write(true) .append(false) - .open(p))))) + .open(p)))) } fn open_appendable_file(&self, p: &Path) -> Result<Box<Write>> { - Ok(Box::new(try!(from_io_result(fs::OpenOptions::new() + Ok(Box::new(try!(fs::OpenOptions::new() .create(true) .write(true) .append(true) - .open(p))))) + .open(p)))) } fn exists(&self, p: &Path) -> Result<bool> { @@ -77,16 +81,16 @@ } fn delete(&self, p: &Path) -> Result<()> { - from_io_result(fs::remove_file(p)) + Ok(fs::remove_file(p)?) } fn mkdir(&self, p: &Path) -> Result<()> { - from_io_result(fs::create_dir(p)) + Ok(fs::create_dir(p)?) } fn rmdir(&self, p: &Path) -> Result<()> { - from_io_result(fs::remove_dir_all(p)) + Ok(fs::remove_dir_all(p)?) } fn rename(&self, old: &Path, new: &Path) -> Result<()> { - from_io_result(fs::rename(old, new)) + Ok(fs::rename(old, new)?) } fn lock(&self, p: &Path) -> Result<FileLock> {
--- a/src/env.rs Mon Aug 28 20:28:54 2017 +0200 +++ b/src/env.rs Mon Aug 28 20:40:08 2017 +0200 @@ -1,7 +1,7 @@ //! An `env` is an abstraction layer that allows the database to run both on different platforms as //! well as persisting data on disk or in memory. -use error::{self, Result}; +use error::Result; use std::io::prelude::*; use std::fs::File; @@ -14,7 +14,7 @@ impl RandomAccess for File { fn read_at(&self, off: usize, dst: &mut [u8]) -> Result<usize> { - error::from_io_result((self as &FileExt).read_at(dst, off as u64)) + Ok((self as &FileExt).read_at(dst, off as u64)?) } }
--- a/src/error.rs Mon Aug 28 20:28:54 2017 +0200 +++ b/src/error.rs Mon Aug 28 20:40:08 2017 +0200 @@ -3,6 +3,7 @@ use std::fmt::{self, Display, Formatter}; use std::io; use std::result; +use std::sync; #[derive(Clone, Debug)] #[allow(dead_code)] @@ -63,6 +64,9 @@ } } +/// LevelDB's result type +pub type Result<T> = result::Result<T, Status>; + impl From<io::Error> for Status { fn from(e: io::Error) -> Status { let c = match e.kind() { @@ -77,21 +81,8 @@ } } -/// LevelDB's result type -pub type Result<T> = result::Result<T, Status>; - -pub fn from_io_result<T>(e: io::Result<T>) -> Result<T> { - match e { - Ok(r) => result::Result::Ok(r), - Err(e) => Err(Status::from(e)), +impl<T> From<sync::PoisonError<T>> for Status { + fn from(_: sync::PoisonError<T>) -> Status { + Status::new(StatusCode::LockError, "lock poisoned") } } - -use std::sync; - -pub fn from_lock_result<T>(e: sync::LockResult<T>) -> Result<T> { - match e { - Ok(r) => result::Result::Ok(r), - Err(_) => result::Result::Err(Status::new(StatusCode::LockError, "lock is poisoned")), - } -}