aral_runtime_tokio/fs/
mod.rs

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