broker_tokio/fs/
mod.rs

1#![cfg(not(loom))]
2
3//! Asynchronous file and standard stream adaptation.
4//!
5//! This module contains utility methods and adapter types for input/output to
6//! files or standard streams (`Stdin`, `Stdout`, `Stderr`), and
7//! filesystem manipulation, for use within (and only within) a Tokio runtime.
8//!
9//! Tasks run by *worker* threads should not block, as this could delay
10//! servicing reactor events. Portable filesystem operations are blocking,
11//! however. This module offers adapters which use a `blocking` annotation
12//! to inform the runtime that a blocking operation is required. When
13//! necessary, this allows the runtime to convert the current thread from a
14//! *worker* to a *backup* thread, where blocking is acceptable.
15//!
16//! ## Usage
17//!
18//! Where possible, users should prefer the provided asynchronous-specific
19//! traits such as [`AsyncRead`], or methods returning a `Future` or `Poll`
20//! type. Adaptions also extend to traits like `std::io::Read` where methods
21//! return `std::io::Result`. Be warned that these adapted methods may return
22//! `std::io::ErrorKind::WouldBlock` if a *worker* thread can not be converted
23//! to a *backup* thread immediately.
24//!
25//! [`AsyncRead`]: https://docs.rs/tokio-io/0.1/tokio_io/trait.AsyncRead.html
26
27mod canonicalize;
28pub use self::canonicalize::canonicalize;
29
30mod create_dir;
31pub use self::create_dir::create_dir;
32
33mod create_dir_all;
34pub use self::create_dir_all::create_dir_all;
35
36mod file;
37pub use self::file::File;
38
39mod hard_link;
40pub use self::hard_link::hard_link;
41
42mod metadata;
43pub use self::metadata::metadata;
44
45mod open_options;
46pub use self::open_options::OpenOptions;
47
48pub mod os;
49
50mod read;
51pub use self::read::read;
52
53mod read_dir;
54pub use self::read_dir::{read_dir, DirEntry, ReadDir};
55
56mod read_link;
57pub use self::read_link::read_link;
58
59mod read_to_string;
60pub use self::read_to_string::read_to_string;
61
62mod remove_dir;
63pub use self::remove_dir::remove_dir;
64
65mod remove_dir_all;
66pub use self::remove_dir_all::remove_dir_all;
67
68mod remove_file;
69pub use self::remove_file::remove_file;
70
71mod rename;
72pub use self::rename::rename;
73
74mod set_permissions;
75pub use self::set_permissions::set_permissions;
76
77mod symlink_metadata;
78pub use self::symlink_metadata::symlink_metadata;
79
80mod write;
81pub use self::write::write;
82
83use std::io;
84
85pub(crate) async fn asyncify<F, T>(f: F) -> io::Result<T>
86where
87    F: FnOnce() -> io::Result<T> + Send + 'static,
88    T: Send + 'static,
89{
90    match sys::run(f).await {
91        Ok(res) => res,
92        Err(_) => Err(io::Error::new(
93            io::ErrorKind::Other,
94            "background task failed",
95        )),
96    }
97}
98
99/// Types in this module can be mocked out in tests.
100mod sys {
101    pub(crate) use std::fs::File;
102
103    // TODO: don't rename
104    pub(crate) use crate::runtime::spawn_blocking as run;
105    pub(crate) use crate::task::JoinHandle as Blocking;
106}