|
7 | 7 | use libc::{c_char, c_void, c_ulong};
|
8 | 8 |
|
9 | 9 | use gobject_ffi::{self, GCallback};
|
10 |
| -use ffi::gboolean; |
| 10 | +use ffi::{gboolean, gpointer}; |
11 | 11 | use object::ObjectType;
|
12 | 12 | use translate::{from_glib, FromGlib, ToGlib, ToGlibPtr};
|
| 13 | +use std::mem; |
13 | 14 |
|
14 | 15 | /// The id of a signal that is returned by `connect`.
|
15 | 16 | #[derive(Debug, Eq, PartialEq)]
|
@@ -48,15 +49,12 @@ impl ToGlib for Inhibit {
|
48 | 49 | }
|
49 | 50 | }
|
50 | 51 |
|
51 |
| -pub unsafe fn connect(receiver: *mut gobject_ffi::GObject, signal_name: &str, trampoline: GCallback, |
52 |
| - closure: *mut Box<Fn() + 'static>) -> SignalHandlerId { |
53 |
| - connect_raw(receiver, signal_name.to_glib_none().0, trampoline, closure) |
54 |
| -} |
55 |
| - |
56 |
| -pub unsafe fn connect_raw(receiver: *mut gobject_ffi::GObject, signal_name: *const c_char, trampoline: GCallback, |
57 |
| - closure: *mut Box<Fn() + 'static>) -> SignalHandlerId { |
| 52 | +pub unsafe fn connect_raw<F>(receiver: *mut gobject_ffi::GObject, signal_name: *const c_char, trampoline: GCallback, |
| 53 | + closure: *mut F) -> SignalHandlerId { |
| 54 | + assert_eq!(mem::size_of::<*mut F>(), mem::size_of::<gpointer>()); |
| 55 | + assert!(trampoline.is_some()); |
58 | 56 | let handle = gobject_ffi::g_signal_connect_data(receiver, signal_name,
|
59 |
| - trampoline, closure as *mut _, Some(destroy_closure), 0); |
| 57 | + trampoline, closure as *mut _, Some(destroy_closure::<F>), 0); |
60 | 58 | assert!(handle > 0);
|
61 | 59 | from_glib(handle)
|
62 | 60 | }
|
@@ -86,7 +84,7 @@ pub fn signal_stop_emission_by_name<T: ObjectType>(instance: &T, signal_name: &s
|
86 | 84 | }
|
87 | 85 | }
|
88 | 86 |
|
89 |
| -unsafe extern "C" fn destroy_closure(ptr: *mut c_void, _: *mut gobject_ffi::GClosure) { |
| 87 | +unsafe extern "C" fn destroy_closure<F>(ptr: *mut c_void, _: *mut gobject_ffi::GClosure) { |
90 | 88 | // destroy
|
91 |
| - Box::<Box<Fn()>>::from_raw(ptr as *mut _); |
| 89 | + Box::<F>::from_raw(ptr as *mut _); |
92 | 90 | }
|
0 commit comments