Simplify FdGuard.

This commit is contained in:
4lDO2
2025-04-12 17:59:51 +02:00
parent 3a8d63d2b8
commit 6e947a9b2f
+11 -8
View File
@@ -646,32 +646,35 @@ impl Drop for MmapGuard {
}
}
#[repr(transparent)]
pub struct FdGuard {
fd: usize,
taken: bool,
}
impl FdGuard {
#[inline]
pub const fn new(fd: usize) -> Self {
Self { fd, taken: false }
Self { fd }
}
pub fn take(&mut self) -> usize {
self.taken = true;
self.fd
#[inline]
pub fn take(self) -> usize {
let fd = self.fd;
core::mem::forget(self);
fd
}
}
impl core::ops::Deref for FdGuard {
type Target = usize;
#[inline]
fn deref(&self) -> &Self::Target {
&self.fd
}
}
impl Drop for FdGuard {
#[inline]
fn drop(&mut self) {
if !self.taken {
let _ = syscall::close(self.fd);
}
let _ = syscall::close(self.fd);
}
}
impl Debug for FdGuard {