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 create_symlink(&self, source: &Path, target: &Path) -> Result<()> {
41 self.fs.create_symlink(source, target)
42 }
43
44 fn remove_dir(&self, path: &Path) -> Result<()> {
45 self.fs.remove_dir(path)
46 }
47
48 fn rename<'a>(&'a self, from: &'a Path, to: &'a Path) -> BoxFuture<'a, Result<()>> {
49 Box::pin(async { self.fs.rename(from, to).await })
50 }
51
52 fn metadata(&self, path: &Path) -> Result<Metadata> {
53 self.fs.metadata(path)
54 }
55
56 fn symlink_metadata(&self, path: &Path) -> Result<Metadata> {
57 self.fs.symlink_metadata(path)
58 }
59
60 fn remove_file(&self, path: &Path) -> Result<()> {
61 self.fs.remove_file(path)
62 }
63
64 fn new_open_options(&self) -> OpenOptions<'_> {
65 self.fs.new_open_options()
66 }
67}
68
69#[cfg(test)]
70mod test_builder {
71 use std::sync::Arc;
72
73 use tokio::io::{AsyncReadExt, AsyncWriteExt};
74
75 use crate::{FileSystem, PassthruFileSystem};
76
77 #[tokio::test]
78 async fn test_passthru_fs_2() {
79 let mem_fs = crate::mem_fs::FileSystem::default();
80
81 mem_fs
82 .new_open_options()
83 .read(true)
84 .write(true)
85 .create(true)
86 .open("/foo.txt")
87 .unwrap()
88 .write_all(b"hello")
89 .await
90 .unwrap();
91
92 let mut buf = Vec::new();
93 mem_fs
94 .new_open_options()
95 .read(true)
96 .open("/foo.txt")
97 .unwrap()
98 .read_to_end(&mut buf)
99 .await
100 .unwrap();
101 assert_eq!(buf, b"hello");
102
103 let passthru_fs = PassthruFileSystem::new_arc(Arc::new(mem_fs.clone()));
104 let mut buf = Vec::new();
105 passthru_fs
106 .new_open_options()
107 .read(true)
108 .open("/foo.txt")
109 .unwrap()
110 .read_to_end(&mut buf)
111 .await
112 .unwrap();
113 assert_eq!(buf, b"hello");
114 }
115}