From 6e947a9b2f4ebeee098a68c6adccc086c4e87a41 Mon Sep 17 00:00:00 2001 From: 4lDO2 <4lDO2@protonmail.com> Date: Sat, 12 Apr 2025 17:59:51 +0200 Subject: [PATCH] Simplify FdGuard. --- redox-rt/src/proc.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/redox-rt/src/proc.rs b/redox-rt/src/proc.rs index 173d6b0aed..af8117bf7a 100644 --- a/redox-rt/src/proc.rs +++ b/redox-rt/src/proc.rs @@ -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 {