aral_runtime_async_std/fs/
mod.rs

1use crate::io::{Read, Seek, Write};
2use std::{
3    fs::{Metadata, Permissions},
4    io::{Result, SeekFrom},
5    path::{Path, PathBuf},
6};
7
8pub struct File(async_std::fs::File);
9
10impl File {
11    #[inline]
12    pub async fn create(path: impl AsRef<Path>) -> Result<File> {
13        async_std::fs::File::create(path.as_ref()).await.map(File)
14    }
15
16    #[inline]
17    pub async fn open(path: impl AsRef<Path>) -> Result<File> {
18        async_std::fs::File::open(path.as_ref()).await.map(File)
19    }
20
21    #[inline]
22    pub async fn metadata(&self) -> Result<Metadata> {
23        self.0.metadata().await
24    }
25
26    #[inline]
27    pub async fn set_len(&self, size: u64) -> Result<()> {
28        self.0.set_len(size).await
29    }
30
31    #[inline]
32    pub async fn set_permissions(&self, perm: Permissions) -> Result<()> {
33        self.0.set_permissions(perm).await
34    }
35
36    #[inline]
37    pub async fn sync_all(&self) -> Result<()> {
38        self.0.sync_all().await
39    }
40
41    #[inline]
42    pub async fn sync_data(&self) -> Result<()> {
43        self.0.sync_data().await
44    }
45
46    #[inline]
47    pub async fn try_clone(&self) -> Result<File> {
48        Ok(File(self.0.clone()))
49    }
50}
51
52impl Read for File {
53    #[inline]
54    async fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
55        async_std::io::ReadExt::read(&mut self.0, buf).await
56    }
57}
58
59impl Write for File {
60    #[inline]
61    async fn write(&mut self, buf: &[u8]) -> Result<usize> {
62        async_std::io::WriteExt::write(&mut self.0, buf).await
63    }
64
65    #[inline]
66    async fn flush(&mut self) -> Result<()> {
67        async_std::io::WriteExt::flush(&mut self.0).await
68    }
69}
70
71impl Seek for File {
72    #[inline]
73    async fn seek(&mut self, pos: SeekFrom) -> Result<u64> {
74        async_std::io::prelude::SeekExt::seek(&mut self.0, pos).await
75    }
76}
77
78#[derive(Default)]
79pub struct OpenOptions(async_std::fs::OpenOptions);
80
81impl OpenOptions {
82    #[inline]
83    pub fn append(&mut self, append: bool) -> &mut OpenOptions {
84        self.0.append(append);
85        self
86    }
87
88    #[inline]
89    pub fn create(&mut self, create: bool) -> &mut OpenOptions {
90        self.0.create(create);
91        self
92    }
93
94    #[inline]
95    pub fn create_new(&mut self, create_new: bool) -> &mut OpenOptions {
96        self.0.create_new(create_new);
97        self
98    }
99
100    #[inline]
101    pub fn new() -> OpenOptions {
102        OpenOptions(async_std::fs::OpenOptions::new())
103    }
104
105    #[inline]
106    pub async fn open(&self, path: impl AsRef<Path>) -> Result<File> {
107        self.0.open(path.as_ref()).await.map(File)
108    }
109
110    #[inline]
111    pub fn read(&mut self, read: bool) -> &mut OpenOptions {
112        self.0.read(read);
113        self
114    }
115
116    #[inline]
117    pub fn truncate(&mut self, truncate: bool) -> &mut OpenOptions {
118        self.0.truncate(truncate);
119        self
120    }
121
122    #[inline]
123    pub fn write(&mut self, write: bool) -> &mut OpenOptions {
124        self.0.write(write);
125        self
126    }
127}
128
129#[inline]
130pub async fn canonicalize(path: impl AsRef<Path>) -> Result<PathBuf> {
131    async_std::fs::canonicalize(path.as_ref())
132        .await
133        .map(Into::into)
134}
135
136#[inline]
137pub async fn copy(from: impl AsRef<Path>, to: impl AsRef<Path>) -> Result<u64> {
138    async_std::fs::copy(from.as_ref(), to.as_ref())
139        .await
140        .map(Into::into)
141}
142
143#[inline]
144pub async fn create_dir(path: impl AsRef<Path>) -> Result<()> {
145    async_std::fs::create_dir(path.as_ref())
146        .await
147        .map(Into::into)
148}
149
150#[inline]
151pub async fn create_dir_all(path: impl AsRef<Path>) -> Result<()> {
152    async_std::fs::create_dir_all(path.as_ref())
153        .await
154        .map(Into::into)
155}
156
157#[inline]
158pub async fn hard_link(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> Result<()> {
159    async_std::fs::hard_link(src.as_ref(), dst.as_ref())
160        .await
161        .map(Into::into)
162}
163
164#[inline]
165pub async fn metadata(path: impl AsRef<Path>) -> Result<Metadata> {
166    async_std::fs::metadata(path.as_ref()).await.map(Into::into)
167}
168
169#[inline]
170pub async fn read(path: impl AsRef<Path>) -> Result<Vec<u8>> {
171    async_std::fs::read(path.as_ref()).await.map(Into::into)
172}
173
174#[inline]
175pub async fn read_link(path: impl AsRef<Path>) -> Result<PathBuf> {
176    async_std::fs::read_link(path.as_ref())
177        .await
178        .map(Into::into)
179}
180
181#[inline]
182pub async fn read_to_string(path: impl AsRef<Path>) -> Result<String> {
183    async_std::fs::read_to_string(path.as_ref())
184        .await
185        .map(Into::into)
186}
187
188#[inline]
189pub async fn remove_dir(path: impl AsRef<Path>) -> Result<()> {
190    async_std::fs::remove_dir(path.as_ref())
191        .await
192        .map(Into::into)
193}
194
195#[inline]
196pub async fn remove_dir_all(path: impl AsRef<Path>) -> Result<()> {
197    async_std::fs::remove_dir_all(path.as_ref())
198        .await
199        .map(Into::into)
200}
201
202#[inline]
203pub async fn remove_file(path: impl AsRef<Path>) -> Result<()> {
204    async_std::fs::remove_file(path.as_ref())
205        .await
206        .map(Into::into)
207}
208
209#[inline]
210pub async fn rename(from: impl AsRef<Path>, to: impl AsRef<Path>) -> Result<()> {
211    async_std::fs::rename(from.as_ref(), to.as_ref())
212        .await
213        .map(Into::into)
214}
215
216#[inline]
217pub async fn set_permissions(path: impl AsRef<Path>, perm: Permissions) -> Result<()> {
218    async_std::fs::set_permissions(path.as_ref(), perm)
219        .await
220        .map(Into::into)
221}
222
223#[inline]
224pub async fn symlink_metadata(path: impl AsRef<Path>) -> Result<Metadata> {
225    async_std::fs::symlink_metadata(path.as_ref())
226        .await
227        .map(Into::into)
228}
229
230#[inline]
231pub async fn write(path: impl AsRef<Path>, contents: impl AsRef<[u8]>) -> Result<()> {
232    async_std::fs::write(path.as_ref(), contents)
233        .await
234        .map(Into::into)
235}