use core::marker::PhantomData;
use core::mem::ManuallyDrop;
pub type RawFd = i32;
pub const AT_FDCWD: RawFd = -100;
pub const AT_SYMLINK_NOFOLLOW: i32 = 0x100;
pub const AT_REMOVEDIR: i32 = 0x200;
pub const AT_EMPTY_PATH: i32 = 0x1000;
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(transparent)]
pub struct BorrowedFd<'fd> {
fd: RawFd,
_lt: PhantomData<&'fd ()>,
}
impl<'fd> BorrowedFd<'fd> {
#[must_use]
pub const unsafe fn borrow_raw(fd: RawFd) -> Self {
Self {
fd,
_lt: PhantomData,
}
}
#[must_use]
pub const fn as_raw_fd(self) -> RawFd {
self.fd
}
}
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(transparent)]
pub struct OwnedFd {
fd: RawFd,
}
impl OwnedFd {
#[must_use]
pub const unsafe fn from_raw_fd(fd: RawFd) -> Self {
Self { fd }
}
#[must_use]
pub const fn as_raw_fd(&self) -> RawFd {
self.fd
}
#[must_use]
pub const fn into_raw_fd(self) -> RawFd {
let fd = self.fd;
let _ = ManuallyDrop::new(self);
fd
}
#[must_use]
pub const fn as_fd(&self) -> BorrowedFd<'_> {
BorrowedFd {
fd: self.fd,
_lt: PhantomData,
}
}
}
impl Drop for OwnedFd {
fn drop(&mut self) {
let _ = crate::io::close(self.fd);
}
}