@@ -54,7 +54,7 @@ use rt::io::IoError;
54
54
pub use self :: file:: { FsRequest , FsCallback } ;
55
55
pub use self :: net:: { StreamWatcher , TcpWatcher } ;
56
56
pub use self :: net:: { ReadCallback , AllocCallback , ConnectionCallback , ConnectCallback } ;
57
-
57
+ pub use self :: idle :: { IdleWatcher , IdleCallback } ;
58
58
59
59
/// The implementation of `rtio` for libuv
60
60
pub mod uvio;
@@ -64,6 +64,7 @@ pub mod uvll;
64
64
65
65
pub mod file;
66
66
pub mod net;
67
+ pub mod idle;
67
68
68
69
/// A trait for callbacks to implement. Provides a little extra type safety
69
70
/// for generic, unsafe interop functions like `set_watcher_callback`.
@@ -120,74 +121,6 @@ impl NativeHandle<*uvll::uv_loop_t> for Loop {
120
121
}
121
122
}
122
123
123
- pub struct IdleWatcher ( * uvll:: uv_idle_t ) ;
124
- impl Watcher for IdleWatcher { }
125
-
126
- pub type IdleCallback = ~fn ( IdleWatcher , Option < UvError > ) ;
127
- impl Callback for IdleCallback { }
128
-
129
- pub impl IdleWatcher {
130
- fn new ( loop_ : & mut Loop ) -> IdleWatcher {
131
- unsafe {
132
- let handle = uvll:: idle_new ( ) ;
133
- assert ! ( handle. is_not_null( ) ) ;
134
- assert ! ( 0 == uvll:: idle_init( loop_. native_handle( ) , handle) ) ;
135
- let mut watcher: IdleWatcher = NativeHandle :: from_native_handle ( handle) ;
136
- watcher. install_watcher_data ( ) ;
137
- return watcher
138
- }
139
- }
140
-
141
- fn start ( & mut self , cb : IdleCallback ) {
142
- {
143
- let data = self . get_watcher_data ( ) ;
144
- data. idle_cb = Some ( cb) ;
145
- }
146
-
147
- unsafe {
148
- assert ! ( 0 == uvll:: idle_start( self . native_handle( ) , idle_cb) )
149
- } ;
150
-
151
- extern fn idle_cb ( handle : * uvll:: uv_idle_t , status : c_int ) {
152
- let mut idle_watcher: IdleWatcher = NativeHandle :: from_native_handle ( handle) ;
153
- let data = idle_watcher. get_watcher_data ( ) ;
154
- let cb: & IdleCallback = data. idle_cb . get_ref ( ) ;
155
- let status = status_to_maybe_uv_error ( handle, status) ;
156
- ( * cb) ( idle_watcher, status) ;
157
- }
158
- }
159
-
160
- fn stop ( & mut self ) {
161
- // NB: Not resetting the Rust idl_cb to None here because `stop` is likely
162
- // called from *within* the idle callback, which would cause a use after free
163
-
164
- unsafe {
165
- assert ! ( 0 == uvll:: idle_stop( self . native_handle( ) ) ) ;
166
- }
167
- }
168
-
169
- fn close ( self ) {
170
- unsafe { uvll:: close ( self . native_handle ( ) , close_cb) } ;
171
-
172
- extern fn close_cb ( handle : * uvll:: uv_idle_t ) {
173
- unsafe {
174
- let mut idle_watcher: IdleWatcher = NativeHandle :: from_native_handle ( handle) ;
175
- idle_watcher. drop_watcher_data ( ) ;
176
- uvll:: idle_delete ( handle) ;
177
- }
178
- }
179
- }
180
- }
181
-
182
- impl NativeHandle < * uvll:: uv_idle_t > for IdleWatcher {
183
- fn from_native_handle ( handle : * uvll:: uv_idle_t ) -> IdleWatcher {
184
- IdleWatcher ( handle)
185
- }
186
- fn native_handle ( & self ) -> * uvll:: uv_idle_t {
187
- match self { & IdleWatcher ( ptr) => ptr }
188
- }
189
- }
190
-
191
124
/// Callbacks used by StreamWatchers, set as custom data on the foreign handle
192
125
struct WatcherData {
193
126
read_cb : Option < ReadCallback > ,
0 commit comments