Skip to content
This repository was archived by the owner on Jun 8, 2021. It is now read-only.

Commit 0807596

Browse files
committed
Don't box GSource closures twice
1 parent 5bbdaad commit 0807596

File tree

1 file changed

+35
-45
lines changed

1 file changed

+35
-45
lines changed

src/source.rs

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use ffi::{gboolean, gpointer};
1515
#[cfg(any(unix, feature = "dox"))]
1616
use IOCondition;
1717
use translate::{from_glib, from_glib_full, FromGlib, ToGlib, ToGlibPtr};
18-
use libc;
1918

2019
use Source;
2120
use MainContext;
@@ -100,54 +99,51 @@ impl Drop for CallbackGuard {
10099
}
101100

102101
#[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);
105104
(&mut *func.borrow_mut())().to_glib()
106105
}
107106

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 _);
110109
}
111110

112111
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));
115113
Box::into_raw(func) as gpointer
116114
}
117115

118116
#[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);
121119
(&mut *func.borrow_mut())(Pid(pid), status)
122120
}
123121

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 _);
126124
}
127125

128126
#[cfg_attr(feature = "cargo-clippy", allow(type_complexity))]
129127
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));
132129
Box::into_raw(func) as gpointer
133130
}
134131

135132
#[cfg(any(unix, feature = "dox"))]
136133
#[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);
139136
(&mut *func.borrow_mut())(fd, from_glib(condition)).to_glib()
140137
}
141138

142139
#[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 _);
145142
}
146143

147144
#[cfg(any(unix, feature = "dox"))]
148145
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));
151147
Box::into_raw(func) as gpointer
152148
}
153149

@@ -160,8 +156,8 @@ fn into_raw_unix_fd<F: FnMut(RawFd, IOCondition) -> Continue + 'static>(func: F)
160156
pub fn idle_add<F>(func: F) -> SourceId
161157
where F: FnMut() -> Continue + Send + 'static {
162158
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>)))
165161
}
166162
}
167163

@@ -181,8 +177,8 @@ pub fn idle_add_local<F>(func: F) -> SourceId
181177
where F: FnMut() -> Continue + 'static {
182178
unsafe {
183179
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>)))
186182
}
187183
}
188184

@@ -200,7 +196,7 @@ pub fn timeout_add<F>(interval: u32, func: F) -> SourceId
200196
where F: FnMut() -> Continue + Send + 'static {
201197
unsafe {
202198
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>)))
204200
}
205201
}
206202

@@ -225,7 +221,7 @@ where F: FnMut() -> Continue + 'static {
225221
unsafe {
226222
assert!(MainContext::default().is_owner());
227223
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>)))
229225
}
230226
}
231227

@@ -242,7 +238,7 @@ pub fn timeout_add_seconds<F>(interval: u32, func: F) -> SourceId
242238
where F: FnMut() -> Continue + Send + 'static {
243239
unsafe {
244240
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>)))
246242
}
247243
}
248244

@@ -266,7 +262,7 @@ where F: FnMut() -> Continue + 'static {
266262
unsafe {
267263
assert!(MainContext::default().is_owner());
268264
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>)))
270266
}
271267
}
272268

@@ -277,9 +273,8 @@ where F: FnMut() -> Continue + 'static {
277273
pub fn child_watch_add<'a, N: Into<Option<&'a str>>, F>(pid: Pid, func: F) -> SourceId
278274
where F: FnMut(Pid, i32) + Send + 'static {
279275
unsafe {
280-
let trampoline = trampoline_child_watch as *mut libc::c_void;
281276
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>)))
283278
}
284279
}
285280

@@ -297,9 +292,8 @@ pub fn child_watch_add_local<'a, N: Into<Option<&'a str>>, F>(pid: Pid, func: F)
297292
where F: FnMut(Pid, i32) + 'static {
298293
unsafe {
299294
assert!(MainContext::default().is_owner());
300-
let trampoline = trampoline_child_watch as *mut libc::c_void;
301295
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>)))
303297
}
304298
}
305299

@@ -315,7 +309,7 @@ pub fn unix_signal_add<F>(signum: i32, func: F) -> SourceId
315309
where F: FnMut() -> Continue + Send + 'static {
316310
unsafe {
317311
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>)))
319313
}
320314
}
321315

@@ -338,7 +332,7 @@ where F: FnMut() -> Continue + 'static {
338332
unsafe {
339333
assert!(MainContext::default().is_owner());
340334
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>)))
342336
}
343337
}
344338

@@ -354,9 +348,8 @@ where F: FnMut() -> Continue + 'static {
354348
pub fn unix_fd_add<F>(fd: RawFd, condition: IOCondition, func: F) -> SourceId
355349
where F: FnMut(RawFd, IOCondition) -> Continue + Send + 'static {
356350
unsafe {
357-
let trampoline = trampoline_unix_fd as *mut libc::c_void;
358351
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>)))
360353
}
361354
}
362355

@@ -379,9 +372,8 @@ pub fn unix_fd_add_local<F>(fd: RawFd, condition: IOCondition, func: F) -> Sourc
379372
where F: FnMut(RawFd, IOCondition) -> Continue + 'static {
380373
unsafe {
381374
assert!(MainContext::default().is_owner());
382-
let trampoline = trampoline_unix_fd as *mut libc::c_void;
383375
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>)))
385377
}
386378
}
387379

@@ -439,7 +431,7 @@ pub fn idle_source_new<'a, N: Into<Option<&'a str>>, F>(name: N, priority: Prior
439431
where F: FnMut() -> Continue + Send + 'static {
440432
unsafe {
441433
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>));
443435
glib_ffi::g_source_set_priority(source, priority.to_glib());
444436

445437
let name = name.into();
@@ -462,7 +454,7 @@ pub fn timeout_source_new<'a, N: Into<Option<&'a str>>, F>(interval: u32, name:
462454
where F: FnMut() -> Continue + Send + 'static {
463455
unsafe {
464456
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>));
466458
glib_ffi::g_source_set_priority(source, priority.to_glib());
467459

468460
let name = name.into();
@@ -484,7 +476,7 @@ pub fn timeout_source_new_seconds<'a, N: Into<Option<&'a str>>, F>(interval: u32
484476
where F: FnMut() -> Continue + Send + 'static {
485477
unsafe {
486478
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>));
488480
glib_ffi::g_source_set_priority(source, priority.to_glib());
489481

490482
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
504496
where F: FnMut(Pid, i32) + Send + 'static {
505497
unsafe {
506498
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>));
509500
glib_ffi::g_source_set_priority(source, priority.to_glib());
510501

511502
let name = name.into();
@@ -527,7 +518,7 @@ pub fn unix_signal_source_new<'a, N: Into<Option<&'a str>>, F>(signum: i32, name
527518
where F: FnMut() -> Continue + Send + 'static {
528519
unsafe {
529520
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>));
531522
glib_ffi::g_source_set_priority(source, priority.to_glib());
532523

533524
let name = name.into();
@@ -549,8 +540,7 @@ pub fn unix_fd_source_new<'a, N: Into<Option<&'a str>>, F>(fd: RawFd, condition:
549540
where F: FnMut(RawFd, IOCondition) -> Continue + Send + 'static {
550541
unsafe {
551542
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>));
554544
glib_ffi::g_source_set_priority(source, priority.to_glib());
555545

556546
let name = name.into();

0 commit comments

Comments
 (0)