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 fn set_fd_nonblocking
<T
: AsRawFd
+ ?Sized
>(fd
: &T
, on
: bool
) -> nix
::Result
<libc
::c_int
> {
10 let fd
= fd
.as_raw_fd();
11 let mut flags
= fcntl
::OFlag
::from_bits(fcntl
::fcntl(fd
, fcntl
::FcntlArg
::F_GETFL
)?
).unwrap();
12 flags
.set(fcntl
::OFlag
::O_NONBLOCK
, on
);
13 fcntl
::fcntl(fd
, fcntl
::FcntlArg
::F_SETFL(flags
))
16 /// Guard a raw file descriptor with a drop handler. This is mostly useful when access to an owned
17 /// `RawFd` is required without the corresponding handler object (such as when only the file
18 /// descriptor number is required in a closure which may be dropped instead of being executed).
20 pub struct Fd(pub RawFd
);
22 file_descriptor_impl
!(Fd
);
24 impl FromRawFd
for Fd
{
25 unsafe fn from_raw_fd(fd
: RawFd
) -> Self {
31 pub fn set_nonblocking(&mut self, nb
: bool
) -> nix
::Result
<libc
::c_int
> {
32 set_fd_nonblocking(self, nb
)
36 impl AsRef
<RawFd
> for Fd
{
38 fn as_ref(&self) -> &RawFd
{
43 /// Byte vector utilities.
45 /// Create an uninitialized byte vector of a specific size.
47 /// This is just a shortcut for:
49 /// # let len = 64usize;
50 /// let mut v = Vec::<u8>::with_capacity(len);
58 /// This is generally safe to call, but the contents of the vector are undefined.
60 pub unsafe fn uninitialized(len
: usize) -> Vec
<u8> {
62 let data
= std
::alloc
::alloc(std
::alloc
::Layout
::array
::<u8>(len
).unwrap());
63 Vec
::from_raw_parts(data
as *mut u8, len
, len
)
69 fn from_fd
<T
: IntoRawFd
>(fd
: T
) -> Self;
72 impl<T
: FromRawFd
> FromFd
for T
{
73 fn from_fd
<F
: IntoRawFd
>(fd
: F
) -> Self {
74 unsafe { Self::from_raw_fd(fd.into_raw_fd()) }
78 /// This is totally unsafe. Only use this when you know what you're doing.
79 #[derive(Debug, Clone)]
81 pub struct AssertSendSync
<T
>(pub T
);
82 unsafe impl<T
> Send
for AssertSendSync
<T
> {}
83 unsafe impl<T
> Sync
for AssertSendSync
<T
> {}