Files
tokio/tokio-fs/src/lib.rs
T

112 lines
3.5 KiB
Rust
Raw Normal View History

#![doc(html_root_url = "https://docs.rs/tokio-fs/0.2.0-alpha.1")]
#![warn(
missing_debug_implementations,
missing_docs,
rust_2018_idioms,
unreachable_pub
)]
2019-05-14 10:27:36 -07:00
#![doc(test(no_crate_inject, attr(deny(rust_2018_idioms))))]
#![feature(async_await)]
2018-10-23 22:00:49 -07:00
//! Asynchronous file and standard stream adaptation.
2018-05-02 11:19:58 -07:00
//!
//! This module contains utility methods and adapter types for input/output to
//! files or standard streams (`Stdin`, `Stdout`, `Stderr`), and
//! filesystem manipulation, for use within (and only within) a Tokio runtime.
2018-05-02 11:19:58 -07:00
//!
//! Tasks run by *worker* threads should not block, as this could delay
//! servicing reactor events. Portable filesystem operations are blocking,
//! however. This module offers adapters which use a [`blocking`] annotation
//! to inform the runtime that a blocking operation is required. When
//! necessary, this allows the runtime to convert the current thread from a
//! *worker* to a *backup* thread, where blocking is acceptable.
2018-05-02 11:19:58 -07:00
//!
//! ## Usage
//!
//! Where possible, users should prefer the provided asynchronous-specific
//! traits such as [`AsyncRead`], or methods returning a `Future` or `Poll`
//! type. Adaptions also extend to traits like `std::io::Read` where methods
//! return `std::io::Result`. Be warned that these adapted methods may return
//! `std::io::ErrorKind::WouldBlock` if a *worker* thread can not be converted
//! to a *backup* thread immediately. See [tokio-threadpool] for more details
//! of the threading model and [`blocking`].
//!
//! [`blocking`]: https://docs.rs/tokio-threadpool/0.1/tokio_threadpool/fn.blocking.html
//! [`AsyncRead`]: https://docs.rs/tokio-io/0.1/tokio_io/trait.AsyncRead.html
//! [tokio-threadpool]: https://docs.rs/tokio-threadpool/0.1/tokio_threadpool
2018-05-02 11:19:58 -07:00
mod create_dir;
mod create_dir_all;
mod file;
mod hard_link;
2018-06-21 18:41:39 +02:00
mod metadata;
mod open_options;
pub mod os;
2019-02-21 11:56:15 -08:00
mod read;
mod read_dir;
mod read_link;
mod remove_dir;
mod remove_dir_all;
mod remove_file;
mod rename;
mod set_permissions;
2019-02-21 11:56:15 -08:00
mod stderr;
2018-05-02 11:19:58 -07:00
mod stdin;
mod stdout;
mod symlink_metadata;
2019-02-20 23:38:49 +01:00
mod write;
2018-05-02 11:19:58 -07:00
pub use crate::create_dir::create_dir;
pub use crate::create_dir_all::create_dir_all;
2019-05-14 10:27:36 -07:00
pub use crate::file::File;
pub use crate::hard_link::hard_link;
pub use crate::metadata::metadata;
pub use crate::open_options::OpenOptions;
pub use crate::read::read;
pub use crate::read_dir::{read_dir, DirEntry, ReadDir};
pub use crate::read_link::read_link;
pub use crate::remove_dir::remove_dir;
pub use crate::remove_dir_all::remove_dir_all;
pub use crate::remove_file::remove_file;
pub use crate::rename::rename;
pub use crate::set_permissions::set_permissions;
2019-05-14 10:27:36 -07:00
pub use crate::stderr::{stderr, Stderr};
pub use crate::stdin::{stdin, Stdin};
pub use crate::stdout::{stdout, Stdout};
pub use crate::symlink_metadata::symlink_metadata;
pub use crate::write::write;
2018-05-02 11:19:58 -07:00
use std::io;
use std::io::ErrorKind::Other;
2019-07-11 11:05:49 -05:00
use std::task::Poll;
use std::task::Poll::*;
2018-05-02 11:19:58 -07:00
2019-07-11 11:05:49 -05:00
fn blocking_io<F, T>(f: F) -> Poll<io::Result<T>>
2019-02-21 11:56:15 -08:00
where
F: FnOnce() -> io::Result<T>,
2018-05-02 11:19:58 -07:00
{
match tokio_threadpool::blocking(f) {
2019-07-11 11:05:49 -05:00
Ready(Ok(v)) => Ready(v),
Ready(Err(_)) => Ready(Err(blocking_err())),
Pending => Pending,
2018-05-02 11:19:58 -07:00
}
}
async fn asyncify<F, T>(f: F) -> io::Result<T>
2019-02-21 11:56:15 -08:00
where
F: FnOnce() -> io::Result<T>,
2018-05-02 11:19:58 -07:00
{
use futures_util::future::poll_fn;
let mut f = Some(f);
poll_fn(move |_| blocking_io(|| f.take().unwrap()())).await
2018-05-02 11:19:58 -07:00
}
fn blocking_err() -> io::Error {
2019-02-21 11:56:15 -08:00
io::Error::new(
Other,
"`blocking` annotated I/O must be called \
from the context of the Tokio runtime.",
)
2018-05-02 11:19:58 -07:00
}