diff --git a/vfs/src/lib.rs b/vfs/src/lib.rs index a7be30ee..c4423244 100644 --- a/vfs/src/lib.rs +++ b/vfs/src/lib.rs @@ -17,10 +17,29 @@ mod sealed { impl Sealed for StdBackend {} } +pub trait IoResultExt { + fn with_not_found(self) -> io::Result>; +} + +impl IoResultExt for io::Result { + fn with_not_found(self) -> io::Result> { + match self { + Ok(v) => Ok(Some(v)), + Err(err) => { + if err.kind() == io::ErrorKind::NotFound { + Ok(None) + } else { + Err(err) + } + } + } + } +} + /// Backend that can be used to create a `Vfs`. /// /// This trait is sealed and cannot not be implemented outside this crate. -pub trait VfsBackend: sealed::Sealed { +pub trait VfsBackend: sealed::Sealed + Send + 'static { fn read(&mut self, path: &Path) -> io::Result>; fn write(&mut self, path: &Path, data: &[u8]) -> io::Result<()>; fn read_dir(&mut self, path: &Path) -> io::Result; @@ -146,7 +165,7 @@ impl Vfs { } /// Creates a new `Vfs` with the given backend. - pub fn new(backend: B) -> Self { + pub fn new(backend: B) -> Self { let lock = VfsLock { backend: Box::new(backend), }; @@ -193,7 +212,7 @@ impl Vfs { /// Roughly equivalent to [`std::fs::remove_file`][std::fs::remove_file]. /// /// [std::fs::remove_file]: https://doc.rust-lang.org/stable/std/fs/fn.remove_file.html - pub fn remove_file>(&mut self, path: P) -> io::Result<()> { + pub fn remove_file>(&self, path: P) -> io::Result<()> { let path = path.as_ref(); self.inner.lock().unwrap().remove_file(path) } @@ -203,7 +222,7 @@ impl Vfs { /// Roughly equivalent to [`std::fs::remove_dir_all`][std::fs::remove_dir_all]. /// /// [std::fs::remove_dir_all]: https://doc.rust-lang.org/stable/std/fs/fn.remove_dir_all.html - pub fn remove_dir_all>(&mut self, path: P) -> io::Result<()> { + pub fn remove_dir_all>(&self, path: P) -> io::Result<()> { let path = path.as_ref(); self.inner.lock().unwrap().remove_dir_all(path) } @@ -213,7 +232,7 @@ impl Vfs { /// Roughly equivalent to [`std::fs::metadata`][std::fs::metadata]. /// /// [std::fs::metadata]: https://doc.rust-lang.org/stable/std/fs/fn.metadata.html - pub fn metadata>(&mut self, path: P) -> io::Result { + pub fn metadata>(&self, path: P) -> io::Result { let path = path.as_ref(); self.inner.lock().unwrap().metadata(path) }