@@ -33,24 +33,28 @@ pub use crate::sys::time::timer::{Expiration, TimerSetTimeFlags};
33
33
use crate :: unistd:: read;
34
34
use crate :: { errno:: Errno , Result } ;
35
35
use libc:: c_int;
36
- use std:: os:: unix:: io:: { AsRawFd , FromRawFd , RawFd } ;
36
+ use std:: os:: unix:: io:: { AsFd , AsRawFd , BorrowedFd , FromRawFd , OwnedFd , RawFd } ;
37
37
38
38
/// A timerfd instance. This is also a file descriptor, you can feed it to
39
- /// other interfaces consuming file descriptors, epoll for example.
39
+ /// other interfaces taking file descriptors as arguments, [`epoll`] for example.
40
+ ///
41
+ /// [`epoll`]: crate::sys::epoll
40
42
#[ derive( Debug ) ]
41
43
pub struct TimerFd {
42
- fd : RawFd ,
44
+ fd : OwnedFd ,
43
45
}
44
46
45
- impl AsRawFd for TimerFd {
46
- fn as_raw_fd ( & self ) -> RawFd {
47
- self . fd
47
+ impl AsFd for TimerFd {
48
+ fn as_fd ( & self ) -> BorrowedFd < ' _ > {
49
+ self . fd . as_fd ( )
48
50
}
49
51
}
50
52
51
53
impl FromRawFd for TimerFd {
52
54
unsafe fn from_raw_fd ( fd : RawFd ) -> Self {
53
- TimerFd { fd }
55
+ TimerFd {
56
+ fd : OwnedFd :: from_raw_fd ( fd) ,
57
+ }
54
58
}
55
59
}
56
60
@@ -97,7 +101,9 @@ impl TimerFd {
97
101
Errno :: result ( unsafe {
98
102
libc:: timerfd_create ( clockid as i32 , flags. bits ( ) )
99
103
} )
100
- . map ( |fd| Self { fd } )
104
+ . map ( |fd| Self {
105
+ fd : unsafe { OwnedFd :: from_raw_fd ( fd) } ,
106
+ } )
101
107
}
102
108
103
109
/// Sets a new alarm on the timer.
@@ -145,7 +151,7 @@ impl TimerFd {
145
151
let timerspec: TimerSpec = expiration. into ( ) ;
146
152
Errno :: result ( unsafe {
147
153
libc:: timerfd_settime (
148
- self . fd ,
154
+ self . fd . as_fd ( ) . as_raw_fd ( ) ,
149
155
flags. bits ( ) ,
150
156
timerspec. as_ref ( ) ,
151
157
std:: ptr:: null_mut ( ) ,
@@ -159,7 +165,10 @@ impl TimerFd {
159
165
pub fn get ( & self ) -> Result < Option < Expiration > > {
160
166
let mut timerspec = TimerSpec :: none ( ) ;
161
167
Errno :: result ( unsafe {
162
- libc:: timerfd_gettime ( self . fd , timerspec. as_mut ( ) )
168
+ libc:: timerfd_gettime (
169
+ self . fd . as_fd ( ) . as_raw_fd ( ) ,
170
+ timerspec. as_mut ( ) ,
171
+ )
163
172
} )
164
173
. map ( |_| {
165
174
if timerspec. as_ref ( ) . it_interval . tv_sec == 0
@@ -179,7 +188,7 @@ impl TimerFd {
179
188
pub fn unset ( & self ) -> Result < ( ) > {
180
189
Errno :: result ( unsafe {
181
190
libc:: timerfd_settime (
182
- self . fd ,
191
+ self . fd . as_fd ( ) . as_raw_fd ( ) ,
183
192
TimerSetTimeFlags :: empty ( ) . bits ( ) ,
184
193
TimerSpec :: none ( ) . as_ref ( ) ,
185
194
std:: ptr:: null_mut ( ) ,
@@ -192,7 +201,7 @@ impl TimerFd {
192
201
///
193
202
/// Note: If the alarm is unset, then you will wait forever.
194
203
pub fn wait ( & self ) -> Result < ( ) > {
195
- while let Err ( e) = read ( self . fd , & mut [ 0u8 ; 8 ] ) {
204
+ while let Err ( e) = read ( self . fd . as_fd ( ) . as_raw_fd ( ) , & mut [ 0u8 ; 8 ] ) {
196
205
if e != Errno :: EINTR {
197
206
return Err ( e) ;
198
207
}
@@ -201,14 +210,3 @@ impl TimerFd {
201
210
Ok ( ( ) )
202
211
}
203
212
}
204
-
205
- impl Drop for TimerFd {
206
- fn drop ( & mut self ) {
207
- if !std:: thread:: panicking ( ) {
208
- let result = Errno :: result ( unsafe { libc:: close ( self . fd ) } ) ;
209
- if let Err ( Errno :: EBADF ) = result {
210
- panic ! ( "close of TimerFd encountered EBADF" ) ;
211
- }
212
- }
213
- }
214
- }
0 commit comments