1#![cfg_attr(docsrs, feature(doc_cfg))]
4#![allow(unused_features)]
5#![warn(missing_docs)]
6#![deny(rustdoc::broken_intra_doc_links)]
7#![doc(
8 html_logo_url = "https://github.com/compio-rs/compio-logo/raw/refs/heads/master/generated/colored-bold.svg"
9)]
10#![doc(
11 html_favicon_url = "https://github.com/compio-rs/compio-logo/raw/refs/heads/master/generated/colored-bold.svg"
12)]
13#![cfg_attr(feature = "read_buf", feature(read_buf, core_io_borrowed_buf))]
14#![cfg_attr(
15 all(windows, feature = "windows_by_handle"),
16 feature(windows_by_handle)
17)]
18
19mod file;
20pub use file::*;
21
22mod open_options;
23pub use open_options::*;
24
25mod metadata;
26pub use metadata::*;
27
28mod stdio;
29pub use stdio::*;
30
31mod utils;
32pub use utils::*;
33
34#[cfg(dirfd)]
35mod dirfd;
36#[cfg(dirfd)]
37pub use dirfd::*;
38
39#[cfg(windows)]
40pub mod named_pipe;
41
42#[cfg(unix)]
43pub mod pipe;
44
45#[deprecated(since = "0.12.0", note = "Use `compio::runtime::fd::AsyncFd` instead")]
47pub type AsyncFd<T> = compio_runtime::fd::AsyncFd<T>;
48
49use std::io;
50
51#[cfg(unix)]
52pub(crate) fn path_string(path: impl AsRef<std::path::Path>) -> io::Result<std::ffi::CString> {
53 use std::os::unix::ffi::OsStrExt;
54
55 std::ffi::CString::new(path.as_ref().as_os_str().as_bytes().to_vec()).map_err(|_| {
56 io::Error::new(
57 io::ErrorKind::InvalidInput,
58 "file name contained an unexpected NUL byte",
59 )
60 })
61}
62
63use compio_buf::{BufResult, IntoInner};
64use compio_driver::{SharedFd, op::AsyncifyFd};
65
66pub(crate) async fn spawn_blocking_with<T: 'static, R: Send + 'static>(
67 fd: SharedFd<T>,
68 f: impl FnOnce(&T) -> io::Result<R> + Send + 'static,
69) -> io::Result<R> {
70 let op = AsyncifyFd::new(fd, move |fd: &T| match f(fd) {
71 Ok(res) => BufResult(Ok(0), Some(res)),
72 Err(e) => BufResult(Err(e), None),
73 });
74 let BufResult(res, meta) = compio_runtime::submit(op).await;
75 res?;
76 Ok(meta.into_inner().expect("result should be present"))
77}
78
79#[cfg(all(windows, dirfd))]
80pub(crate) async fn spawn_blocking_with2<T1: 'static, T2: 'static, R: Send + 'static>(
81 fd1: SharedFd<T1>,
82 fd2: SharedFd<T2>,
83 f: impl FnOnce(&T1, &T2) -> io::Result<R> + Send + 'static,
84) -> io::Result<R> {
85 use compio_driver::op::AsyncifyFd2;
86
87 let op = AsyncifyFd2::new(fd1, fd2, move |fd1: &T1, fd2: &T2| match f(fd1, fd2) {
88 Ok(res) => BufResult(Ok(0), Some(res)),
89 Err(e) => BufResult(Err(e), None),
90 });
91 let BufResult(res, meta) = compio_runtime::submit(op).await;
92 res?;
93 Ok(meta.into_inner().expect("result should be present"))
94}