1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use std::{
ops::Deref,
os::unix::io::{
AsRawFd,
FromRawFd,
IntoRawFd,
RawFd,
},
sync::atomic::{
AtomicBool,
Ordering,
},
};
use inotify_sys as ffi;
#[derive(Debug)]
pub struct FdGuard {
pub(crate) fd : RawFd,
pub(crate) close_on_drop: AtomicBool,
}
impl FdGuard {
#[inline]
pub fn should_not_close(&self) {
self.close_on_drop.store(false, Ordering::Release);
}
}
impl Deref for FdGuard {
type Target = RawFd;
#[inline]
fn deref(&self) -> &Self::Target {
&self.fd
}
}
impl Drop for FdGuard {
fn drop(&mut self) {
if self.close_on_drop.load(Ordering::Acquire) {
unsafe { ffi::close(self.fd); }
}
}
}
impl FromRawFd for FdGuard {
unsafe fn from_raw_fd(fd: RawFd) -> Self {
FdGuard {
fd,
close_on_drop: AtomicBool::new(true),
}
}
}
impl IntoRawFd for FdGuard {
fn into_raw_fd(self) -> RawFd {
self.should_not_close();
self.fd
}
}
impl AsRawFd for FdGuard {
fn as_raw_fd(&self) -> RawFd {
self.fd
}
}
impl PartialEq for FdGuard {
fn eq(&self, other: &FdGuard) -> bool {
self.fd == other.fd
}
}