@@ -15,7 +15,6 @@ use ffi::{gboolean, gpointer};
15
15
#[ cfg( any( unix, feature = "dox" ) ) ]
16
16
use IOCondition ;
17
17
use translate:: { from_glib, from_glib_full, FromGlib , ToGlib , ToGlibPtr } ;
18
- use libc;
19
18
20
19
use Source ;
21
20
use MainContext ;
@@ -100,54 +99,51 @@ impl Drop for CallbackGuard {
100
99
}
101
100
102
101
#[ cfg_attr( feature = "cargo-clippy" , allow( transmute_ptr_to_ref) ) ]
103
- unsafe extern "C" fn trampoline ( func : gpointer ) -> gboolean {
104
- let func: & RefCell < Box < FnMut ( ) -> Continue + ' static > > = transmute ( func) ;
102
+ unsafe extern "C" fn trampoline < F : FnMut ( ) -> Continue + ' static > ( func : gpointer ) -> gboolean {
103
+ let func: & RefCell < F > = transmute ( func) ;
105
104
( & mut * func. borrow_mut ( ) ) ( ) . to_glib ( )
106
105
}
107
106
108
- unsafe extern "C" fn destroy_closure ( ptr : gpointer ) {
109
- Box :: < RefCell < Box < FnMut ( ) -> Continue + ' static > > > :: from_raw ( ptr as * mut _ ) ;
107
+ unsafe extern "C" fn destroy_closure < F : FnMut ( ) -> Continue + ' static > ( ptr : gpointer ) {
108
+ Box :: < RefCell < F > > :: from_raw ( ptr as * mut _ ) ;
110
109
}
111
110
112
111
fn into_raw < F : FnMut ( ) -> Continue + ' static > ( func : F ) -> gpointer {
113
- let func: Box < RefCell < Box < FnMut ( ) -> Continue + ' static > > > =
114
- Box :: new ( RefCell :: new ( Box :: new ( func) ) ) ;
112
+ let func: Box < RefCell < F > > = Box :: new ( RefCell :: new ( func) ) ;
115
113
Box :: into_raw ( func) as gpointer
116
114
}
117
115
118
116
#[ cfg_attr( feature = "cargo-clippy" , allow( transmute_ptr_to_ref) ) ]
119
- unsafe extern "C" fn trampoline_child_watch ( pid : glib_ffi:: GPid , status : i32 , func : gpointer ) {
120
- let func: & RefCell < Box < FnMut ( Pid , i32 ) + ' static > > = transmute ( func) ;
117
+ unsafe extern "C" fn trampoline_child_watch < F : FnMut ( Pid , i32 ) + ' static > ( pid : glib_ffi:: GPid , status : i32 , func : gpointer ) {
118
+ let func: & RefCell < F > = transmute ( func) ;
121
119
( & mut * func. borrow_mut ( ) ) ( Pid ( pid) , status)
122
120
}
123
121
124
- unsafe extern "C" fn destroy_closure_child_watch ( ptr : gpointer ) {
125
- Box :: < RefCell < Box < FnMut ( Pid , i32 ) + ' static > > > :: from_raw ( ptr as * mut _ ) ;
122
+ unsafe extern "C" fn destroy_closure_child_watch < F : FnMut ( Pid , i32 ) + ' static > ( ptr : gpointer ) {
123
+ Box :: < RefCell < F > > :: from_raw ( ptr as * mut _ ) ;
126
124
}
127
125
128
126
#[ cfg_attr( feature = "cargo-clippy" , allow( type_complexity) ) ]
129
127
fn into_raw_child_watch < F : FnMut ( Pid , i32 ) + ' static > ( func : F ) -> gpointer {
130
- let func: Box < RefCell < Box < FnMut ( Pid , i32 ) + ' static > > > =
131
- Box :: new ( RefCell :: new ( Box :: new ( func) ) ) ;
128
+ let func: Box < RefCell < F > > = Box :: new ( RefCell :: new ( func) ) ;
132
129
Box :: into_raw ( func) as gpointer
133
130
}
134
131
135
132
#[ cfg( any( unix, feature = "dox" ) ) ]
136
133
#[ cfg_attr( feature = "cargo-clippy" , allow( transmute_ptr_to_ref) ) ]
137
- unsafe extern "C" fn trampoline_unix_fd ( fd : i32 , condition : glib_ffi:: GIOCondition , func : gpointer ) -> gboolean {
138
- let func: & RefCell < Box < FnMut ( RawFd , IOCondition ) -> Continue + ' static > > = transmute ( func) ;
134
+ unsafe extern "C" fn trampoline_unix_fd < F : FnMut ( RawFd , IOCondition ) -> Continue + ' static > ( fd : i32 , condition : glib_ffi:: GIOCondition , func : gpointer ) -> gboolean {
135
+ let func: & RefCell < F > = transmute ( func) ;
139
136
( & mut * func. borrow_mut ( ) ) ( fd, from_glib ( condition) ) . to_glib ( )
140
137
}
141
138
142
139
#[ cfg( any( unix, feature = "dox" ) ) ]
143
- unsafe extern "C" fn destroy_closure_unix_fd ( ptr : gpointer ) {
144
- Box :: < RefCell < Box < FnMut ( RawFd , IOCondition ) + ' static > > > :: from_raw ( ptr as * mut _ ) ;
140
+ unsafe extern "C" fn destroy_closure_unix_fd < F : FnMut ( RawFd , IOCondition ) -> Continue + ' static > ( ptr : gpointer ) {
141
+ Box :: < RefCell < F > > :: from_raw ( ptr as * mut _ ) ;
145
142
}
146
143
147
144
#[ cfg( any( unix, feature = "dox" ) ) ]
148
145
fn into_raw_unix_fd < F : FnMut ( RawFd , IOCondition ) -> Continue + ' static > ( func : F ) -> gpointer {
149
- let func: Box < RefCell < Box < FnMut ( RawFd , IOCondition ) -> Continue + ' static > > > =
150
- Box :: new ( RefCell :: new ( Box :: new ( func) ) ) ;
146
+ let func: Box < RefCell < F > > = Box :: new ( RefCell :: new ( func) ) ;
151
147
Box :: into_raw ( func) as gpointer
152
148
}
153
149
@@ -160,8 +156,8 @@ fn into_raw_unix_fd<F: FnMut(RawFd, IOCondition) -> Continue + 'static>(func: F)
160
156
pub fn idle_add < F > ( func : F ) -> SourceId
161
157
where F : FnMut ( ) -> Continue + Send + ' static {
162
158
unsafe {
163
- from_glib ( glib_ffi:: g_idle_add_full ( glib_ffi:: G_PRIORITY_DEFAULT_IDLE , Some ( trampoline) ,
164
- into_raw ( func) , Some ( destroy_closure) ) )
159
+ from_glib ( glib_ffi:: g_idle_add_full ( glib_ffi:: G_PRIORITY_DEFAULT_IDLE , Some ( trampoline :: < F > ) ,
160
+ into_raw ( func) , Some ( destroy_closure :: < F > ) ) )
165
161
}
166
162
}
167
163
@@ -181,8 +177,8 @@ pub fn idle_add_local<F>(func: F) -> SourceId
181
177
where F : FnMut ( ) -> Continue + ' static {
182
178
unsafe {
183
179
assert ! ( MainContext :: default ( ) . is_owner( ) ) ;
184
- from_glib ( glib_ffi:: g_idle_add_full ( glib_ffi:: G_PRIORITY_DEFAULT_IDLE , Some ( trampoline) ,
185
- into_raw ( func) , Some ( destroy_closure) ) )
180
+ from_glib ( glib_ffi:: g_idle_add_full ( glib_ffi:: G_PRIORITY_DEFAULT_IDLE , Some ( trampoline :: < F > ) ,
181
+ into_raw ( func) , Some ( destroy_closure :: < F > ) ) )
186
182
}
187
183
}
188
184
@@ -200,7 +196,7 @@ pub fn timeout_add<F>(interval: u32, func: F) -> SourceId
200
196
where F : FnMut ( ) -> Continue + Send + ' static {
201
197
unsafe {
202
198
from_glib ( glib_ffi:: g_timeout_add_full ( glib_ffi:: G_PRIORITY_DEFAULT , interval,
203
- Some ( trampoline) , into_raw ( func) , Some ( destroy_closure) ) )
199
+ Some ( trampoline :: < F > ) , into_raw ( func) , Some ( destroy_closure :: < F > ) ) )
204
200
}
205
201
}
206
202
@@ -225,7 +221,7 @@ where F: FnMut() -> Continue + 'static {
225
221
unsafe {
226
222
assert ! ( MainContext :: default ( ) . is_owner( ) ) ;
227
223
from_glib ( glib_ffi:: g_timeout_add_full ( glib_ffi:: G_PRIORITY_DEFAULT , interval,
228
- Some ( trampoline) , into_raw ( func) , Some ( destroy_closure) ) )
224
+ Some ( trampoline :: < F > ) , into_raw ( func) , Some ( destroy_closure :: < F > ) ) )
229
225
}
230
226
}
231
227
@@ -242,7 +238,7 @@ pub fn timeout_add_seconds<F>(interval: u32, func: F) -> SourceId
242
238
where F : FnMut ( ) -> Continue + Send + ' static {
243
239
unsafe {
244
240
from_glib ( glib_ffi:: g_timeout_add_seconds_full ( glib_ffi:: G_PRIORITY_DEFAULT , interval,
245
- Some ( trampoline) , into_raw ( func) , Some ( destroy_closure) ) )
241
+ Some ( trampoline :: < F > ) , into_raw ( func) , Some ( destroy_closure :: < F > ) ) )
246
242
}
247
243
}
248
244
@@ -266,7 +262,7 @@ where F: FnMut() -> Continue + 'static {
266
262
unsafe {
267
263
assert ! ( MainContext :: default ( ) . is_owner( ) ) ;
268
264
from_glib ( glib_ffi:: g_timeout_add_seconds_full ( glib_ffi:: G_PRIORITY_DEFAULT , interval,
269
- Some ( trampoline) , into_raw ( func) , Some ( destroy_closure) ) )
265
+ Some ( trampoline :: < F > ) , into_raw ( func) , Some ( destroy_closure :: < F > ) ) )
270
266
}
271
267
}
272
268
@@ -277,9 +273,8 @@ where F: FnMut() -> Continue + 'static {
277
273
pub fn child_watch_add < ' a , N : Into < Option < & ' a str > > , F > ( pid : Pid , func : F ) -> SourceId
278
274
where F : FnMut ( Pid , i32 ) + Send + ' static {
279
275
unsafe {
280
- let trampoline = trampoline_child_watch as * mut libc:: c_void ;
281
276
from_glib ( glib_ffi:: g_child_watch_add_full ( glib_ffi:: G_PRIORITY_DEFAULT , pid. 0 ,
282
- Some ( transmute ( trampoline ) ) , into_raw_child_watch ( func) , Some ( destroy_closure_child_watch) ) )
277
+ Some ( transmute ( trampoline_child_watch :: < F > as usize ) ) , into_raw_child_watch ( func) , Some ( destroy_closure_child_watch :: < F > ) ) )
283
278
}
284
279
}
285
280
@@ -297,9 +292,8 @@ pub fn child_watch_add_local<'a, N: Into<Option<&'a str>>, F>(pid: Pid, func: F)
297
292
where F : FnMut ( Pid , i32 ) + ' static {
298
293
unsafe {
299
294
assert ! ( MainContext :: default ( ) . is_owner( ) ) ;
300
- let trampoline = trampoline_child_watch as * mut libc:: c_void ;
301
295
from_glib ( glib_ffi:: g_child_watch_add_full ( glib_ffi:: G_PRIORITY_DEFAULT , pid. 0 ,
302
- Some ( transmute ( trampoline ) ) , into_raw_child_watch ( func) , Some ( destroy_closure_child_watch) ) )
296
+ Some ( transmute ( trampoline_child_watch :: < F > as usize ) ) , into_raw_child_watch ( func) , Some ( destroy_closure_child_watch :: < F > ) ) )
303
297
}
304
298
}
305
299
@@ -315,7 +309,7 @@ pub fn unix_signal_add<F>(signum: i32, func: F) -> SourceId
315
309
where F : FnMut ( ) -> Continue + Send + ' static {
316
310
unsafe {
317
311
from_glib ( glib_ffi:: g_unix_signal_add_full ( glib_ffi:: G_PRIORITY_DEFAULT , signum,
318
- Some ( trampoline) , into_raw ( func) , Some ( destroy_closure) ) )
312
+ Some ( trampoline :: < F > ) , into_raw ( func) , Some ( destroy_closure :: < F > ) ) )
319
313
}
320
314
}
321
315
@@ -338,7 +332,7 @@ where F: FnMut() -> Continue + 'static {
338
332
unsafe {
339
333
assert ! ( MainContext :: default ( ) . is_owner( ) ) ;
340
334
from_glib ( glib_ffi:: g_unix_signal_add_full ( glib_ffi:: G_PRIORITY_DEFAULT , signum,
341
- Some ( trampoline) , into_raw ( func) , Some ( destroy_closure) ) )
335
+ Some ( trampoline :: < F > ) , into_raw ( func) , Some ( destroy_closure :: < F > ) ) )
342
336
}
343
337
}
344
338
@@ -354,9 +348,8 @@ where F: FnMut() -> Continue + 'static {
354
348
pub fn unix_fd_add < F > ( fd : RawFd , condition : IOCondition , func : F ) -> SourceId
355
349
where F : FnMut ( RawFd , IOCondition ) -> Continue + Send + ' static {
356
350
unsafe {
357
- let trampoline = trampoline_unix_fd as * mut libc:: c_void ;
358
351
from_glib ( glib_ffi:: g_unix_fd_add_full ( glib_ffi:: G_PRIORITY_DEFAULT , fd, condition. to_glib ( ) ,
359
- Some ( transmute ( trampoline ) ) , into_raw_unix_fd ( func) , Some ( destroy_closure_unix_fd) ) )
352
+ Some ( transmute ( trampoline_unix_fd :: < F > as usize ) ) , into_raw_unix_fd ( func) , Some ( destroy_closure_unix_fd :: < F > ) ) )
360
353
}
361
354
}
362
355
@@ -379,9 +372,8 @@ pub fn unix_fd_add_local<F>(fd: RawFd, condition: IOCondition, func: F) -> Sourc
379
372
where F : FnMut ( RawFd , IOCondition ) -> Continue + ' static {
380
373
unsafe {
381
374
assert ! ( MainContext :: default ( ) . is_owner( ) ) ;
382
- let trampoline = trampoline_unix_fd as * mut libc:: c_void ;
383
375
from_glib ( glib_ffi:: g_unix_fd_add_full ( glib_ffi:: G_PRIORITY_DEFAULT , fd, condition. to_glib ( ) ,
384
- Some ( transmute ( trampoline ) ) , into_raw_unix_fd ( func) , Some ( destroy_closure_unix_fd) ) )
376
+ Some ( transmute ( trampoline_unix_fd :: < F > as usize ) ) , into_raw_unix_fd ( func) , Some ( destroy_closure_unix_fd :: < F > ) ) )
385
377
}
386
378
}
387
379
@@ -439,7 +431,7 @@ pub fn idle_source_new<'a, N: Into<Option<&'a str>>, F>(name: N, priority: Prior
439
431
where F : FnMut ( ) -> Continue + Send + ' static {
440
432
unsafe {
441
433
let source = glib_ffi:: g_idle_source_new ( ) ;
442
- glib_ffi:: g_source_set_callback ( source, Some ( trampoline) , into_raw ( func) , Some ( destroy_closure) ) ;
434
+ glib_ffi:: g_source_set_callback ( source, Some ( trampoline :: < F > ) , into_raw ( func) , Some ( destroy_closure :: < F > ) ) ;
443
435
glib_ffi:: g_source_set_priority ( source, priority. to_glib ( ) ) ;
444
436
445
437
let name = name. into ( ) ;
@@ -462,7 +454,7 @@ pub fn timeout_source_new<'a, N: Into<Option<&'a str>>, F>(interval: u32, name:
462
454
where F : FnMut ( ) -> Continue + Send + ' static {
463
455
unsafe {
464
456
let source = glib_ffi:: g_timeout_source_new ( interval) ;
465
- glib_ffi:: g_source_set_callback ( source, Some ( trampoline) , into_raw ( func) , Some ( destroy_closure) ) ;
457
+ glib_ffi:: g_source_set_callback ( source, Some ( trampoline :: < F > ) , into_raw ( func) , Some ( destroy_closure :: < F > ) ) ;
466
458
glib_ffi:: g_source_set_priority ( source, priority. to_glib ( ) ) ;
467
459
468
460
let name = name. into ( ) ;
@@ -484,7 +476,7 @@ pub fn timeout_source_new_seconds<'a, N: Into<Option<&'a str>>, F>(interval: u32
484
476
where F : FnMut ( ) -> Continue + Send + ' static {
485
477
unsafe {
486
478
let source = glib_ffi:: g_timeout_source_new_seconds ( interval) ;
487
- glib_ffi:: g_source_set_callback ( source, Some ( trampoline) , into_raw ( func) , Some ( destroy_closure) ) ;
479
+ glib_ffi:: g_source_set_callback ( source, Some ( trampoline :: < F > ) , into_raw ( func) , Some ( destroy_closure :: < F > ) ) ;
488
480
glib_ffi:: g_source_set_priority ( source, priority. to_glib ( ) ) ;
489
481
490
482
let name = name. into ( ) ;
@@ -504,8 +496,7 @@ pub fn child_watch_source_new<'a, N: Into<Option<&'a str>>, F>(pid: Pid, name: N
504
496
where F : FnMut ( Pid , i32 ) + Send + ' static {
505
497
unsafe {
506
498
let source = glib_ffi:: g_child_watch_source_new ( pid. 0 ) ;
507
- let trampoline = trampoline_child_watch as * mut libc:: c_void ;
508
- glib_ffi:: g_source_set_callback ( source, Some ( transmute ( trampoline) ) , into_raw_child_watch ( func) , Some ( destroy_closure_child_watch) ) ;
499
+ glib_ffi:: g_source_set_callback ( source, Some ( transmute ( trampoline_child_watch :: < F > as usize ) ) , into_raw_child_watch ( func) , Some ( destroy_closure_child_watch :: < F > ) ) ;
509
500
glib_ffi:: g_source_set_priority ( source, priority. to_glib ( ) ) ;
510
501
511
502
let name = name. into ( ) ;
@@ -527,7 +518,7 @@ pub fn unix_signal_source_new<'a, N: Into<Option<&'a str>>, F>(signum: i32, name
527
518
where F : FnMut ( ) -> Continue + Send + ' static {
528
519
unsafe {
529
520
let source = glib_ffi:: g_unix_signal_source_new ( signum) ;
530
- glib_ffi:: g_source_set_callback ( source, Some ( trampoline) , into_raw ( func) , Some ( destroy_closure) ) ;
521
+ glib_ffi:: g_source_set_callback ( source, Some ( trampoline :: < F > ) , into_raw ( func) , Some ( destroy_closure :: < F > ) ) ;
531
522
glib_ffi:: g_source_set_priority ( source, priority. to_glib ( ) ) ;
532
523
533
524
let name = name. into ( ) ;
@@ -549,8 +540,7 @@ pub fn unix_fd_source_new<'a, N: Into<Option<&'a str>>, F>(fd: RawFd, condition:
549
540
where F : FnMut ( RawFd , IOCondition ) -> Continue + Send + ' static {
550
541
unsafe {
551
542
let source = glib_ffi:: g_unix_fd_source_new ( fd, condition. to_glib ( ) ) ;
552
- let trampoline = trampoline_unix_fd as * mut libc:: c_void ;
553
- glib_ffi:: g_source_set_callback ( source, Some ( transmute ( trampoline) ) , into_raw_unix_fd ( func) , Some ( destroy_closure_unix_fd) ) ;
543
+ glib_ffi:: g_source_set_callback ( source, Some ( transmute ( trampoline_unix_fd :: < F > as usize ) ) , into_raw_unix_fd ( func) , Some ( destroy_closure_unix_fd :: < F > ) ) ;
554
544
glib_ffi:: g_source_set_priority ( source, priority. to_glib ( ) ) ;
555
545
556
546
let name = name. into ( ) ;
0 commit comments