1use core::marker::PhantomData;
4use core::mem::ManuallyDrop;
5
6pub type RawFd = i32;
8
9pub const AT_FDCWD: RawFd = -100;
11pub const AT_SYMLINK_NOFOLLOW: i32 = 0x100;
13pub const AT_REMOVEDIR: i32 = 0x200;
15pub const AT_EMPTY_PATH: i32 = 0x1000;
17
18#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
20#[repr(transparent)]
21pub struct BorrowedFd<'fd> {
22 fd: RawFd,
23 _lt: PhantomData<&'fd ()>,
24}
25
26impl<'fd> BorrowedFd<'fd> {
27 #[must_use]
33 pub const unsafe fn borrow_raw(fd: RawFd) -> Self {
34 Self {
35 fd,
36 _lt: PhantomData,
37 }
38 }
39
40 #[must_use]
42 pub const fn as_raw_fd(self) -> RawFd {
43 self.fd
44 }
45}
46
47#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
49#[repr(transparent)]
50pub struct OwnedFd {
51 fd: RawFd,
52}
53
54impl OwnedFd {
55 #[must_use]
61 pub const unsafe fn from_raw_fd(fd: RawFd) -> Self {
62 Self { fd }
63 }
64
65 #[must_use]
67 pub const fn as_raw_fd(&self) -> RawFd {
68 self.fd
69 }
70
71 #[must_use]
73 pub const fn into_raw_fd(self) -> RawFd {
74 let fd = self.fd;
75 let _ = ManuallyDrop::new(self);
76 fd
77 }
78
79 #[must_use]
81 pub const fn as_fd(&self) -> BorrowedFd<'_> {
82 BorrowedFd {
83 fd: self.fd,
84 _lt: PhantomData,
85 }
86 }
87}
88
89impl Drop for OwnedFd {
90 fn drop(&mut self) {
91 let _ = crate::io::close(self.fd);
92 }
93}