Skip to main content

compio_driver/sys/pal/unix/
mod.rs

1use std::io;
2pub use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
3
4pub use libc::cmsghdr as CmsgHeader;
5use rustix::{
6    fs::{AtFlags, CWD, Stat},
7    io::Errno,
8};
9use smallvec::SmallVec;
10
11use crate::sys::prelude::*;
12
13mod_use![stat, pipe, socket];
14
15pub mod reexport {}
16
17/// One item in local or more items on heap.
18pub type Multi<T> = SmallVec<[T; 1]>;
19
20/// The interest to poll a file descriptor.
21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
22pub enum Interest {
23    /// Represents a read operation.
24    Readable,
25    /// Represents a write operation.
26    Writable,
27}
28
29/// A special file descriptor that always refers to the current working
30/// directory. It represents [`AT_FDCWD`](libc::AT_FDCWD) in libc.
31pub struct CurrentDir;
32
33impl AsRawFd for CurrentDir {
34    fn as_raw_fd(&self) -> RawFd {
35        CWD.as_raw_fd()
36    }
37}
38
39impl AsFd for CurrentDir {
40    fn as_fd(&self) -> BorrowedFd<'_> {
41        unsafe { BorrowedFd::borrow_raw(libc::AT_FDCWD) }
42    }
43}
44
45/// Execute the function, retry on interruption, return [`Poll::Pending`] if
46/// it's not finished yet, or return the result otherwise.
47pub fn poll_io<R, E, F>(mut f: F) -> Poll<io::Result<R>>
48where
49    F: FnMut() -> std::result::Result<R, E>,
50    E: Into<io::Error>,
51{
52    loop {
53        match f().map_err(Into::into) {
54            Ok(res) => break Poll::Ready(Ok(res)),
55            Err(e) => match Errno::from_io_error(&e) {
56                Some(Errno::WOULDBLOCK | Errno::INPROGRESS) => return Poll::Pending,
57                Some(Errno::INTR) => continue,
58                _ => return Poll::Ready(Err(e)),
59            },
60        }
61    }
62}