virtual_fs/
passthru_fs.rs1use std::{path::Path, sync::Arc};
6
7use crate::*;
8
9#[derive(Debug)]
10pub struct PassthruFileSystem {
11 fs: Arc<dyn FileSystem + Send + Sync + 'static>,
12}
13
14impl PassthruFileSystem {
15 pub fn new(inner: Box<dyn FileSystem + Send + Sync + 'static>) -> Self {
19 Self { fs: inner.into() }
20 }
21
22 pub fn new_arc(inner: Arc<dyn FileSystem + Send + Sync + 'static>) -> Self {
23 Self { fs: inner }
24 }
25}
26
27impl FileSystem for PassthruFileSystem {
28 fn readlink(&self, path: &Path) -> Result<PathBuf> {
29 self.fs.readlink(path)
30 }
31
32 fn read_dir(&self, path: &Path) -> Result<ReadDir> {
33 self.fs.read_dir(path)
34 }
35
36 fn create_dir(&self, path: &Path) -> Result<()> {
37 self.fs.create_dir(path)
38 }
39
40 fn remove_dir(&self, path: &Path) -> Result<()> {
41 self.fs.remove_dir(path)
42 }
43
44 fn rename<'a>(&'a self, from: &'a Path, to: &'a Path) -> BoxFuture<'a, Result<()>> {
45 Box::pin(async { self.fs.rename(from, to).await })
46 }
47
48 fn metadata(&self, path: &Path) -> Result<Metadata> {
49 self.fs.metadata(path)
50 }
51
52 fn symlink_metadata(&self, path: &Path) -> Result<Metadata> {
53 self.fs.symlink_metadata(path)
54 }
55
56 fn remove_file(&self, path: &Path) -> Result<()> {
57 self.fs.remove_file(path)
58 }
59
60 fn new_open_options(&self) -> OpenOptions<'_> {
61 self.fs.new_open_options()
62 }
63
64 fn mount(
65 &self,
66 _name: String,
67 _path: &Path,
68 _fs: Box<dyn FileSystem + Send + Sync>,
69 ) -> Result<()> {
70 Err(FsError::Unsupported)
71 }
72}
73
74#[cfg(test)]
75mod test_builder {
76 use std::sync::Arc;
77
78 use tokio::io::{AsyncReadExt, AsyncWriteExt};
79
80 use crate::{FileSystem, PassthruFileSystem};
81
82 #[tokio::test]
83 async fn test_passthru_fs_2() {
84 let mem_fs = crate::mem_fs::FileSystem::default();
85
86 mem_fs
87 .new_open_options()
88 .read(true)
89 .write(true)
90 .create(true)
91 .open("/foo.txt")
92 .unwrap()
93 .write_all(b"hello")
94 .await
95 .unwrap();
96
97 let mut buf = Vec::new();
98 mem_fs
99 .new_open_options()
100 .read(true)
101 .open("/foo.txt")
102 .unwrap()
103 .read_to_end(&mut buf)
104 .await
105 .unwrap();
106 assert_eq!(buf, b"hello");
107
108 let passthru_fs = PassthruFileSystem::new_arc(Arc::new(mem_fs.clone()));
109 let mut buf = Vec::new();
110 passthru_fs
111 .new_open_options()
112 .read(true)
113 .open("/foo.txt")
114 .unwrap()
115 .read_to_end(&mut buf)
116 .await
117 .unwrap();
118 assert_eq!(buf, b"hello");
119 }
120}