fut_compat/fs/
async_std.rs

1use super::*;
2
3use ::async_std::fs;
4
5
6
7/// [`async_std`](https://docs.rs/async-std)'s abstraction of a [`Filesystem`].
8#[cfg(feature = "async-std-rt")]
9#[cfg_attr(docsrs, doc(cfg(feature = "async-std-rt")))]
10#[derive(Default, Clone, Copy, Debug, PartialEq, Eq, Hash)]
11pub struct AsyncStdFs {}
12
13
14#[async_trait]
15impl Filesystem for AsyncStdFs {
16    type ReadDir = fs::ReadDir;
17    type DirEntry = fs::DirEntry;
18
19    async fn canonicalize<P: AsRef<Path> + Send>(path: P) -> std::io::Result<PathBuf> {
20        let path = path.as_ref();
21        let path: &Path = path.into();
22
23        let path_buf = fs::canonicalize(path).await?;
24        let path_buf: PathBuf = path_buf.into();
25
26        Ok(path_buf)
27    }
28
29    async fn copy<S: AsRef<Path> + Send, D: AsRef<Path> + Send>(
30        from: S,
31        to: D,
32    ) -> std::io::Result<u64> {
33        let from = from.as_ref();
34        let from: &Path = from.into();
35
36        let to = to.as_ref();
37        let to: &Path = to.into();
38
39        fs::copy(from, to).await
40    }
41
42    async fn create_dir<P: AsRef<Path> + Send>(path: P) -> std::io::Result<()> {
43        let path = path.as_ref();
44        let path: &Path = path.into();
45
46        fs::create_dir(path).await
47    }
48
49    async fn create_dir_all<P: AsRef<Path> + Send>(path: P) -> std::io::Result<()> {
50        let path = path.as_ref();
51        let path: &Path = path.into();
52
53        fs::create_dir_all(path).await
54    }
55
56    async fn hard_link<S: AsRef<Path> + Send, D: AsRef<Path> + Send>(
57        from: S,
58        to: D,
59    ) -> std::io::Result<()> {
60        let from = from.as_ref();
61        let from: &Path = from.into();
62
63        let to = to.as_ref();
64        let to: &Path = to.into();
65
66        fs::hard_link(from, to).await
67    }
68
69    async fn metadata<P: AsRef<Path> + Send>(path: P) -> std::io::Result<Metadata> {
70        let path = path.as_ref();
71        let path: &Path = path.into();
72
73        fs::metadata(path).await
74    }
75
76    async fn read<P: AsRef<Path> + Send>(path: P) -> std::io::Result<Vec<u8>> {
77        let path = path.as_ref();
78        let path: &Path = path.into();
79
80        fs::read(path).await
81    }
82
83    async fn read_dir<P: AsRef<Path> + Send>(path: P) -> std::io::Result<Self::ReadDir> {
84        let path = path.as_ref();
85        let path: &Path = path.into();
86
87        fs::read_dir(path).await
88    }
89
90    async fn read_link<P: AsRef<Path> + Send>(path: P) -> std::io::Result<PathBuf> {
91        let path = path.as_ref();
92        let path: &Path = path.into();
93
94        let path_buf = fs::read_link(path).await?;
95        let path_buf: PathBuf = path_buf.into();
96
97        Ok(path_buf)
98    }
99
100    async fn read_to_string<P: AsRef<Path> + Send>(path: P) -> std::io::Result<String> {
101        let path = path.as_ref();
102        let path: &Path = path.into();
103
104        fs::read_to_string(path).await
105    }
106
107    async fn remove_dir<P: AsRef<Path> + Send>(path: P) -> std::io::Result<()> {
108        let path = path.as_ref();
109        let path: &Path = path.into();
110
111        fs::remove_dir(path).await
112    }
113
114    async fn remove_dir_all<P: AsRef<Path> + Send>(path: P) -> std::io::Result<()> {
115        let path = path.as_ref();
116        let path: &Path = path.into();
117
118        fs::remove_dir_all(path).await
119    }
120
121    async fn remove_file<P: AsRef<Path> + Send>(path: P) -> std::io::Result<()> {
122        let path = path.as_ref();
123        let path: &Path = path.into();
124
125        fs::remove_file(path).await
126    }
127
128    async fn rename<O: AsRef<Path> + Send, N: AsRef<Path> + Send>(
129        from: O,
130        to: N,
131    ) -> std::io::Result<()> {
132        let from = from.as_ref();
133        let from: &Path = from.into();
134
135        let to = to.as_ref();
136        let to: &Path = to.into();
137
138        fs::rename(from, to).await
139    }
140
141    async fn set_permissions<P: AsRef<Path> + Send>(
142        path: P,
143        perm: Permissions,
144    ) -> std::io::Result<()> {
145        let path = path.as_ref();
146        let path: &Path = path.into();
147
148        fs::set_permissions(path, perm).await
149    }
150
151    async fn symlink_metadata<P: AsRef<Path> + Send>(path: P) -> std::io::Result<Metadata> {
152        let path = path.as_ref();
153        let path: &Path = path.into();
154
155        fs::symlink_metadata(path).await
156    }
157
158    async fn write<P: AsRef<Path> + Send, C: AsRef<[u8]> + Send>(
159        path: P,
160        contents: C
161    ) -> std::io::Result<()> {
162        let path = path.as_ref();
163        let path: &Path = path.into();
164
165        fs::write(path, contents).await
166    }
167}
168
169
170
171#[async_trait]
172impl DirEntry for fs::DirEntry {
173    fn path(&self) -> PathBuf {
174        self.path().into()
175    }
176
177    fn file_name(&self) -> OsString {
178        self.file_name()
179    }
180
181    async fn metadata(&self) -> std::io::Result<Metadata> {
182        self.metadata().await
183    }
184
185    async fn file_type(&self) -> std::io::Result<FileType> {
186        self.file_type().await
187    }
188}
189
190#[async_trait]
191impl File for fs::File {
192    async fn open<P: AsRef<Path> + Send>(path: P) -> std::io::Result<Self> {
193        let path = path.as_ref();
194        let path: &Path = path.into();
195
196        Self::open(path).await
197    }
198
199    async fn create<P: AsRef<Path> + Send>(path: P) -> std::io::Result<Self> {
200        let path = path.as_ref();
201        let path: &Path = path.into();
202
203        Self::create(path).await
204    }
205
206    async fn sync_all(&self) -> std::io::Result<()> {
207        self.sync_all().await
208    }
209
210    async fn sync_data(&self) -> std::io::Result<()> {
211        self.sync_data().await
212    }
213
214    async fn set_len(&self, size: u64) -> std::io::Result<()> {
215        self.set_len(size).await
216    }
217
218    async fn metadata(&self) -> std::io::Result<Metadata> {
219        self.metadata().await
220    }
221
222    async fn set_permissions(&self, perm: Permissions) -> std::io::Result<()> {
223        self.set_permissions(perm).await
224    }
225}
226
227#[async_trait]
228impl OpenOptions for fs::OpenOptions {
229    type File = fs::File;
230
231    fn new() -> Self {
232        Self::new()
233    }
234
235    fn read(&mut self, read: bool) -> &mut Self {
236        self.read(read)
237    }
238
239    fn write(&mut self, write: bool) -> &mut Self {
240        self.write(write)
241    }
242
243    fn append(&mut self, append: bool) -> &mut Self {
244        self.append(append)
245    }
246
247    fn truncate(&mut self, truncate: bool) -> &mut Self {
248        self.truncate(truncate)
249    }
250
251    fn create(&mut self, create: bool) -> &mut Self {
252        self.create(create)
253    }
254
255    fn create_new(&mut self, create_new: bool) -> &mut Self {
256        self.create_new(create_new)
257    }
258
259    async fn open<P: AsRef<Path> + Send>(&self, path: P) -> std::io::Result<Self::File> {
260        let path = path.as_ref();
261        let path: &::async_std::path::Path = path.into();
262
263        self.open(path).await
264    }
265}
266
267#[async_trait]
268impl DirBuilder for fs::DirBuilder {
269    fn new() -> Self {
270        Self::new()
271    }
272
273    fn recursive(&mut self, recursive: bool) -> &mut Self {
274        self.recursive(recursive)
275    }
276
277    async fn create<P: AsRef<Path> + Send>(&self, path: P) -> std::io::Result<()> {
278        let path = path.as_ref();
279        let path: &::async_std::path::Path = path.into();
280
281        self.create(path).await
282    }
283}