Remove unwrap()s in file abstraction code

This commit is contained in:
Matthias Beyer 2016-10-13 14:28:26 +02:00
parent c0eb329abf
commit 52367edc83

View file

@ -58,7 +58,7 @@ mod fs {
debug!("Getting lazy file: {:?}", self); debug!("Getting lazy file: {:?}", self);
match *self { match *self {
FileAbstraction::Absent(ref f) => { FileAbstraction::Absent(ref f) => {
let map = MAP.lock().unwrap(); let map = try!(MAP.lock().map_err_into(SEK::LockPoisoned));
return map.get(f).cloned().ok_or(SEK::FileNotFound.into_error()); return map.get(f).cloned().ok_or(SEK::FileNotFound.into_error());
}, },
}; };
@ -67,7 +67,7 @@ mod fs {
pub fn write_file_content(&mut self, buf: &[u8]) -> Result<(), SE> { pub fn write_file_content(&mut self, buf: &[u8]) -> Result<(), SE> {
match *self { match *self {
FileAbstraction::Absent(ref f) => { FileAbstraction::Absent(ref f) => {
let mut map = MAP.lock().unwrap(); let mut map = try!(MAP.lock().map_err_into(SEK::LockPoisoned));
if let Some(ref mut cur) = map.get_mut(f) { if let Some(ref mut cur) = map.get_mut(f) {
let mut vec = cur.get_mut(); let mut vec = cur.get_mut();
vec.clear(); vec.clear();
@ -82,19 +82,21 @@ mod fs {
} }
pub fn remove_file(path: &PathBuf) -> Result<(), SE> { pub fn remove_file(path: &PathBuf) -> Result<(), SE> {
MAP.lock().unwrap().remove(path); try!(MAP.lock().map_err_into(SEK::LockPoisoned))
Ok(()) .remove(path)
.map(|_| ())
.ok_or(SEK::FileNotFound.into_error())
} }
pub fn copy(from: &PathBuf, to: &PathBuf) -> Result<(), SE> { pub fn copy(from: &PathBuf, to: &PathBuf) -> Result<(), SE> {
let mut map = MAP.lock().unwrap(); let mut map = try!(MAP.lock().map_err_into(SEK::LockPoisoned));
let a = try!(map.get(from).cloned().ok_or(SEK::FileNotFound.into_error())); let a = try!(map.get(from).cloned().ok_or(SEK::FileNotFound.into_error()));
map.insert(to.clone(), a); map.insert(to.clone(), a);
Ok(()) Ok(())
} }
pub fn rename(from: &PathBuf, to: &PathBuf) -> Result<(), SE> { pub fn rename(from: &PathBuf, to: &PathBuf) -> Result<(), SE> {
let mut map = MAP.lock().unwrap(); let mut map = try!(MAP.lock().map_err_into(SEK::LockPoisoned));
let a = try!(map.get(from).cloned().ok_or(SEK::FileNotFound.into_error())); let a = try!(map.get(from).cloned().ok_or(SEK::FileNotFound.into_error()));
map.insert(to.clone(), a); map.insert(to.clone(), a);
Ok(()) Ok(())