1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
use crate::io::{Read, Seek, Write};
use std::{
    fs::{Metadata, Permissions},
    io::{Result, SeekFrom},
    path::{Path, PathBuf},
};

pub struct File(async_std::fs::File);

impl File {
    #[inline]
    pub async fn create(path: impl AsRef<Path>) -> Result<File> {
        async_std::fs::File::create(path.as_ref()).await.map(File)
    }

    #[inline]
    pub async fn open(path: impl AsRef<Path>) -> Result<File> {
        async_std::fs::File::open(path.as_ref()).await.map(File)
    }

    #[inline]
    pub async fn metadata(&self) -> Result<Metadata> {
        self.0.metadata().await
    }

    #[inline]
    pub async fn set_len(&self, size: u64) -> Result<()> {
        self.0.set_len(size).await
    }

    #[inline]
    pub async fn set_permissions(&self, perm: Permissions) -> Result<()> {
        self.0.set_permissions(perm).await
    }

    #[inline]
    pub async fn sync_all(&self) -> Result<()> {
        self.0.sync_all().await
    }

    #[inline]
    pub async fn sync_data(&self) -> Result<()> {
        self.0.sync_data().await
    }

    #[inline]
    pub async fn try_clone(&self) -> Result<File> {
        Ok(File(self.0.clone()))
    }
}

impl Read for File {
    #[inline]
    async fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
        async_std::io::ReadExt::read(&mut self.0, buf).await
    }
}

impl Write for File {
    #[inline]
    async fn write(&mut self, buf: &[u8]) -> Result<usize> {
        async_std::io::WriteExt::write(&mut self.0, buf).await
    }

    #[inline]
    async fn flush(&mut self) -> Result<()> {
        async_std::io::WriteExt::flush(&mut self.0).await
    }
}

impl Seek for File {
    #[inline]
    async fn seek(&mut self, pos: SeekFrom) -> Result<u64> {
        async_std::io::prelude::SeekExt::seek(&mut self.0, pos).await
    }
}

#[derive(Default)]
pub struct OpenOptions(async_std::fs::OpenOptions);

impl OpenOptions {
    #[inline]
    pub fn append(&mut self, append: bool) -> &mut OpenOptions {
        self.0.append(append);
        self
    }

    #[inline]
    pub fn create(&mut self, create: bool) -> &mut OpenOptions {
        self.0.create(create);
        self
    }

    #[inline]
    pub fn create_new(&mut self, create_new: bool) -> &mut OpenOptions {
        self.0.create_new(create_new);
        self
    }

    #[inline]
    pub fn new() -> OpenOptions {
        OpenOptions(async_std::fs::OpenOptions::new())
    }

    #[inline]
    pub async fn open(&self, path: impl AsRef<Path>) -> Result<File> {
        self.0.open(path.as_ref()).await.map(File)
    }

    #[inline]
    pub fn read(&mut self, read: bool) -> &mut OpenOptions {
        self.0.read(read);
        self
    }

    #[inline]
    pub fn truncate(&mut self, truncate: bool) -> &mut OpenOptions {
        self.0.truncate(truncate);
        self
    }

    #[inline]
    pub fn write(&mut self, write: bool) -> &mut OpenOptions {
        self.0.write(write);
        self
    }
}

#[inline]
pub async fn canonicalize(path: impl AsRef<Path>) -> Result<PathBuf> {
    async_std::fs::canonicalize(path.as_ref())
        .await
        .map(Into::into)
}

#[inline]
pub async fn copy(from: impl AsRef<Path>, to: impl AsRef<Path>) -> Result<u64> {
    async_std::fs::copy(from.as_ref(), to.as_ref())
        .await
        .map(Into::into)
}

#[inline]
pub async fn create_dir(path: impl AsRef<Path>) -> Result<()> {
    async_std::fs::create_dir(path.as_ref())
        .await
        .map(Into::into)
}

#[inline]
pub async fn create_dir_all(path: impl AsRef<Path>) -> Result<()> {
    async_std::fs::create_dir_all(path.as_ref())
        .await
        .map(Into::into)
}

#[inline]
pub async fn hard_link(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> Result<()> {
    async_std::fs::hard_link(src.as_ref(), dst.as_ref())
        .await
        .map(Into::into)
}

#[inline]
pub async fn metadata(path: impl AsRef<Path>) -> Result<Metadata> {
    async_std::fs::metadata(path.as_ref()).await.map(Into::into)
}

#[inline]
pub async fn read(path: impl AsRef<Path>) -> Result<Vec<u8>> {
    async_std::fs::read(path.as_ref()).await.map(Into::into)
}

#[inline]
pub async fn read_link(path: impl AsRef<Path>) -> Result<PathBuf> {
    async_std::fs::read_link(path.as_ref())
        .await
        .map(Into::into)
}

#[inline]
pub async fn read_to_string(path: impl AsRef<Path>) -> Result<String> {
    async_std::fs::read_to_string(path.as_ref())
        .await
        .map(Into::into)
}

#[inline]
pub async fn remove_dir(path: impl AsRef<Path>) -> Result<()> {
    async_std::fs::remove_dir(path.as_ref())
        .await
        .map(Into::into)
}

#[inline]
pub async fn remove_dir_all(path: impl AsRef<Path>) -> Result<()> {
    async_std::fs::remove_dir_all(path.as_ref())
        .await
        .map(Into::into)
}

#[inline]
pub async fn remove_file(path: impl AsRef<Path>) -> Result<()> {
    async_std::fs::remove_file(path.as_ref())
        .await
        .map(Into::into)
}

#[inline]
pub async fn rename(from: impl AsRef<Path>, to: impl AsRef<Path>) -> Result<()> {
    async_std::fs::rename(from.as_ref(), to.as_ref())
        .await
        .map(Into::into)
}

#[inline]
pub async fn set_permissions(path: impl AsRef<Path>, perm: Permissions) -> Result<()> {
    async_std::fs::set_permissions(path.as_ref(), perm)
        .await
        .map(Into::into)
}

#[inline]
pub async fn symlink_metadata(path: impl AsRef<Path>) -> Result<Metadata> {
    async_std::fs::symlink_metadata(path.as_ref())
        .await
        .map(Into::into)
}

#[inline]
pub async fn write(path: impl AsRef<Path>, contents: impl AsRef<[u8]>) -> Result<()> {
    async_std::fs::write(path.as_ref(), contents)
        .await
        .map(Into::into)
}