3 //! Note that this should stay small, otherwise we should introduce a dependency on our `proxmox`
4 //! crate as that's where we have all this stuff usually...
6 use std
::os
::unix
::io
::{AsRawFd, FromRawFd, IntoRawFd, RawFd}
;
8 pub use io_uring
::iovec
::{IoVec, IoVecMut}
;
10 /// Guard a raw file descriptor with a drop handler. This is mostly useful when access to an owned
11 /// `RawFd` is required without the corresponding handler object (such as when only the file
12 /// descriptor number is required in a closure which may be dropped instead of being executed).
14 pub struct Fd(pub RawFd
);
16 file_descriptor_impl
!(Fd
);
18 impl FromRawFd
for Fd
{
19 unsafe fn from_raw_fd(fd
: RawFd
) -> Self {
25 pub fn set_nonblocking(&self, nb
: bool
) -> std
::io
::Result
<()> {
26 let fd
= self.as_raw_fd();
27 let flags
= c_try
!(unsafe { libc::fcntl(fd, libc::F_GETFL) }
);
29 flags
| libc
::O_NONBLOCK
31 flags
& !libc
::O_NONBLOCK
33 c_try
!(unsafe { libc::fcntl(fd, libc::F_SETFL, flags) }
);
38 /// Byte vector utilities.
40 /// Create an uninitialized byte vector of a specific size.
42 /// This is just a shortcut for:
44 /// # let len = 64usize;
45 /// let mut v = Vec::<u8>::with_capacity(len);
53 /// This is generally safe to call, but the contents of the vector are undefined.
55 pub unsafe fn uninitialized(len
: usize) -> Vec
<u8> {
56 let mut out
= Vec
::with_capacity(len
);
63 fn from_fd(fd
: Fd
) -> Self;
66 impl<T
: FromRawFd
> FromFd
for T
{
67 fn from_fd(fd
: Fd
) -> Self {
68 unsafe { Self::from_raw_fd(fd.into_raw_fd()) }