1 // Copyright (c) 2023 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 //! Asynchronous file IO
15
16 mod async_dir;
17 mod async_file;
18 mod file_buf;
19 mod open_options;
20
21 use std::io;
22
23 pub use async_dir::{
24 canonicalize, copy, create_dir, create_dir_all, hard_link, metadata, read, read_dir, read_link,
25 read_to_string, remove_dir, remove_dir_all, remove_file, rename, set_permissions,
26 symlink_metadata, write,
27 };
28 pub use async_file::File;
29 pub use open_options::OpenOptions;
30
31 use crate::spawn::spawn_blocking;
32 use crate::task::TaskBuilder;
33
async_op<T, R>(task: T) -> io::Result<R> where T: FnOnce() -> io::Result<R>, T: Send + 'static, R: Send + 'static,34 pub(crate) async fn async_op<T, R>(task: T) -> io::Result<R>
35 where
36 T: FnOnce() -> io::Result<R>,
37 T: Send + 'static,
38 R: Send + 'static,
39 {
40 spawn_blocking(&TaskBuilder::new(), task)
41 .await
42 .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?
43 }
44
45 macro_rules! poll_ready {
46 ($e:expr) => {
47 match $e {
48 std::task::Poll::Ready(t) => t,
49 std::task::Poll::Pending => return Poll::Pending,
50 }
51 };
52 }
53
54 pub(crate) use poll_ready;
55