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, R, F>(fd: SharedFd<T>, f: F) -> io::Result<R>
67where
68 T: Sync + 'static,
69 R: Send + 'static,
70 F: FnOnce(&T) -> io::Result<R> + Send + 'static,
71{
72 let op = AsyncifyFd::new(fd, move |fd: &T| match f(fd) {
73 Ok(res) => BufResult(Ok(0), Some(res)),
74 Err(e) => BufResult(Err(e), None),
75 });
76 let BufResult(res, meta) = compio_runtime::submit(op).await;
77 res?;
78 Ok(meta.into_inner().expect("result should be present"))
79}
80
81#[cfg(all(windows, dirfd))]
82pub(crate) async fn spawn_blocking_with2<T1, T2, R, F>(
83 fd1: SharedFd<T1>,
84 fd2: SharedFd<T2>,
85 f: F,
86) -> io::Result<R>
87where
88 T1: Sync + 'static,
89 T2: Sync + 'static,
90 R: Send + 'static,
91 F: FnOnce(&T1, &T2) -> io::Result<R> + Send + 'static,
92{
93 use compio_driver::op::AsyncifyFd2;
94
95 let op = AsyncifyFd2::new(fd1, fd2, move |fd1: &T1, fd2: &T2| match f(fd1, fd2) {
96 Ok(res) => BufResult(Ok(0), Some(res)),
97 Err(e) => BufResult(Err(e), None),
98 });
99 let BufResult(res, meta) = compio_runtime::submit(op).await;
100 res?;
101 Ok(meta.into_inner().expect("result should be present"))
102}