Skip to content

Commit

Permalink
Add Send to trait objects returned from APIs
Browse files Browse the repository at this point in the history
open_file/create_file/append_file

Previously, these APIs would return a trait object not marked Send,
which made it difficult to use in an async context.

Adding 'Send' requires very little modification and greatly improves
usability for async code.
  • Loading branch information
Property404 authored and manuel-woelker committed Dec 1, 2022
1 parent 0ed7811 commit a406210
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 21 deletions.
6 changes: 3 additions & 3 deletions src/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ pub trait FileSystem: Debug + Sync + Send + 'static {
/// Note that the parent directory must already exist.
fn create_dir(&self, path: &str) -> VfsResult<()>;
/// Opens the file at this path for reading
fn open_file(&self, path: &str) -> VfsResult<Box<dyn SeekAndRead>>;
fn open_file(&self, path: &str) -> VfsResult<Box<dyn SeekAndRead + Send>>;
/// Creates a file at this path for writing
fn create_file(&self, path: &str) -> VfsResult<Box<dyn Write>>;
fn create_file(&self, path: &str) -> VfsResult<Box<dyn Write + Send>>;
/// Opens the file at this path for appending
fn append_file(&self, path: &str) -> VfsResult<Box<dyn Write>>;
fn append_file(&self, path: &str) -> VfsResult<Box<dyn Write + Send>>;
/// Returns the file metadata for the file at this path
fn metadata(&self, path: &str) -> VfsResult<VfsMetadata>;
/// Returns true if a file or directory at path exists, false otherwise
Expand Down
6 changes: 3 additions & 3 deletions src/impls/altroot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ impl FileSystem for AltrootFS {
self.path(path)?.create_dir()
}

fn open_file(&self, path: &str) -> VfsResult<Box<dyn SeekAndRead>> {
fn open_file(&self, path: &str) -> VfsResult<Box<dyn SeekAndRead + Send>> {
self.path(path)?.open_file()
}

fn create_file(&self, path: &str) -> VfsResult<Box<dyn Write>> {
fn create_file(&self, path: &str) -> VfsResult<Box<dyn Write + Send>> {
self.path(path)?.create_file()
}

fn append_file(&self, path: &str) -> VfsResult<Box<dyn Write>> {
fn append_file(&self, path: &str) -> VfsResult<Box<dyn Write + Send>> {
self.path(path)?.append_file()
}

Expand Down
6 changes: 3 additions & 3 deletions src/impls/embedded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,18 @@ where
Err(VfsErrorKind::NotSupported.into())
}

fn open_file(&self, path: &str) -> VfsResult<Box<dyn SeekAndRead>> {
fn open_file(&self, path: &str) -> VfsResult<Box<dyn SeekAndRead + Send>> {
match T::get(path.split_at(1).1) {
None => Err(VfsErrorKind::FileNotFound.into()),
Some(file) => Ok(Box::new(Cursor::new(file.data))),
}
}

fn create_file(&self, _path: &str) -> VfsResult<Box<dyn Write>> {
fn create_file(&self, _path: &str) -> VfsResult<Box<dyn Write + Send>> {
Err(VfsErrorKind::NotSupported.into())
}

fn append_file(&self, _path: &str) -> VfsResult<Box<dyn Write>> {
fn append_file(&self, _path: &str) -> VfsResult<Box<dyn Write + Send>> {
Err(VfsErrorKind::NotSupported.into())
}

Expand Down
6 changes: 3 additions & 3 deletions src/impls/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl FileSystem for MemoryFS {
Ok(())
}

fn open_file(&self, path: &str) -> VfsResult<Box<dyn SeekAndRead>> {
fn open_file(&self, path: &str) -> VfsResult<Box<dyn SeekAndRead + Send>> {
let handle = self.handle.read().unwrap();
let file = handle.files.get(path).ok_or(VfsErrorKind::FileNotFound)?;
ensure_file(file)?;
Expand All @@ -169,7 +169,7 @@ impl FileSystem for MemoryFS {
}))
}

fn create_file(&self, path: &str) -> VfsResult<Box<dyn Write>> {
fn create_file(&self, path: &str) -> VfsResult<Box<dyn Write + Send>> {
self.ensure_has_parent(path)?;
let content = Arc::new(Vec::<u8>::new());
self.handle.write().unwrap().files.insert(
Expand All @@ -187,7 +187,7 @@ impl FileSystem for MemoryFS {
Ok(Box::new(writer))
}

fn append_file(&self, path: &str) -> VfsResult<Box<dyn Write>> {
fn append_file(&self, path: &str) -> VfsResult<Box<dyn Write + Send>> {
let handle = self.handle.write().unwrap();
let file = handle.files.get(path).ok_or(VfsErrorKind::FileNotFound)?;
let mut content = Cursor::new(file.content.as_ref().clone());
Expand Down
6 changes: 3 additions & 3 deletions src/impls/overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ impl FileSystem for OverlayFS {
Ok(())
}

fn open_file(&self, path: &str) -> VfsResult<Box<dyn SeekAndRead>> {
fn open_file(&self, path: &str) -> VfsResult<Box<dyn SeekAndRead + Send>> {
self.read_path(path)?.open_file()
}

fn create_file(&self, path: &str) -> VfsResult<Box<dyn Write>> {
fn create_file(&self, path: &str) -> VfsResult<Box<dyn Write + Send>> {
self.ensure_has_parent(path)?;
let result = self.write_path(path)?.create_file()?;
let whiteout_path = self.whiteout_path(path)?;
Expand All @@ -131,7 +131,7 @@ impl FileSystem for OverlayFS {
Ok(result)
}

fn append_file(&self, path: &str) -> VfsResult<Box<dyn Write>> {
fn append_file(&self, path: &str) -> VfsResult<Box<dyn Write + Send>> {
let write_path = self.write_path(path)?;
if !write_path.exists()? {
self.ensure_has_parent(path)?;
Expand Down
6 changes: 3 additions & 3 deletions src/impls/physical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ impl FileSystem for PhysicalFS {
Ok(())
}

fn open_file(&self, path: &str) -> VfsResult<Box<dyn SeekAndRead>> {
fn open_file(&self, path: &str) -> VfsResult<Box<dyn SeekAndRead + Send>> {
Ok(Box::new(File::open(self.get_path(path))?))
}

fn create_file(&self, path: &str) -> VfsResult<Box<dyn Write>> {
fn create_file(&self, path: &str) -> VfsResult<Box<dyn Write + Send>> {
Ok(Box::new(File::create(self.get_path(path))?))
}

fn append_file(&self, path: &str) -> VfsResult<Box<dyn Write>> {
fn append_file(&self, path: &str) -> VfsResult<Box<dyn Write + Send>> {
Ok(Box::new(
OpenOptions::new()
.write(true)
Expand Down
6 changes: 3 additions & 3 deletions src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ impl VfsPath {
/// assert_eq!(&result, "Hello, world!");
/// # Ok::<(), VfsError>(())
/// ```
pub fn create_file(&self) -> VfsResult<Box<dyn Write>> {
pub fn create_file(&self) -> VfsResult<Box<dyn Write + Send>> {
self.get_parent("create file")?;
self.fs.fs.create_file(&self.path).map_err(|err| {
err.with_path(&self.path)
Expand All @@ -289,7 +289,7 @@ impl VfsPath {
/// assert_eq!(&result, "Hello, world!");
/// # Ok::<(), VfsError>(())
/// ```
pub fn open_file(&self) -> VfsResult<Box<dyn SeekAndRead>> {
pub fn open_file(&self) -> VfsResult<Box<dyn SeekAndRead + Send>> {
self.fs.fs.open_file(&self.path).map_err(|err| {
err.with_path(&self.path)
.with_context(|| "Could not open file")
Expand Down Expand Up @@ -342,7 +342,7 @@ impl VfsPath {
/// assert_eq!(&result, "Hello, world!");
/// # Ok::<(), VfsError>(())
/// ```
pub fn append_file(&self) -> VfsResult<Box<dyn Write>> {
pub fn append_file(&self) -> VfsResult<Box<dyn Write + Send>> {
self.fs.fs.append_file(&self.path).map_err(|err| {
err.with_path(&self.path)
.with_context(|| "Could not open file for appending")
Expand Down

0 comments on commit a406210

Please sign in to comment.