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

Commit 123a5ab

Browse files
authored
Merge pull request #493 from GuillaumeGomez/move-trampolines
Move trampolines
2 parents 94bbc9f + eee0019 commit 123a5ab

File tree

6 files changed

+68
-75
lines changed

6 files changed

+68
-75
lines changed

src/byte_array.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,22 @@ impl ByteArray {
105105
}
106106

107107
pub fn sort<F: FnMut(&u8, &u8) -> Ordering>(&self, compare_func: F) {
108+
unsafe extern "C" fn compare_func_trampoline(
109+
a: glib_sys::gconstpointer,
110+
b: glib_sys::gconstpointer,
111+
func: glib_sys::gpointer,
112+
) -> i32 {
113+
let func = func as *mut &mut (dyn FnMut(&u8, &u8) -> Ordering);
114+
115+
let a = &*(a as *const u8);
116+
let b = &*(b as *const u8);
117+
118+
match (*func)(&a, &b) {
119+
Ordering::Less => -1,
120+
Ordering::Equal => 0,
121+
Ordering::Greater => 1,
122+
}
123+
}
108124
unsafe {
109125
let mut func = compare_func;
110126
let func_obj: &mut (dyn FnMut(&u8, &u8) -> Ordering) = &mut func;
@@ -120,23 +136,6 @@ impl ByteArray {
120136
}
121137
}
122138

123-
unsafe extern "C" fn compare_func_trampoline(
124-
a: glib_sys::gconstpointer,
125-
b: glib_sys::gconstpointer,
126-
func: glib_sys::gpointer,
127-
) -> i32 {
128-
let func = func as *mut &mut (dyn FnMut(&u8, &u8) -> Ordering);
129-
130-
let a = &*(a as *const u8);
131-
let b = &*(b as *const u8);
132-
133-
match (*func)(&a, &b) {
134-
Ordering::Less => -1,
135-
Ordering::Equal => 0,
136-
Ordering::Greater => 1,
137-
}
138-
}
139-
140139
impl AsRef<[u8]> for ByteArray {
141140
fn as_ref(&self) -> &[u8] {
142141
&*self

src/main_context.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,17 @@ impl MainContext {
8686
where
8787
F: FnOnce() + 'static,
8888
{
89+
unsafe extern "C" fn trampoline<F: FnOnce() + 'static>(func: gpointer) -> gboolean {
90+
let func: &mut Option<F> = &mut *(func as *mut Option<F>);
91+
let func = func
92+
.take()
93+
.expect("MainContext::invoke() closure called multiple times");
94+
func();
95+
glib_sys::G_SOURCE_REMOVE
96+
}
97+
unsafe extern "C" fn destroy_closure<F: FnOnce() + 'static>(ptr: gpointer) {
98+
Box::<Option<F>>::from_raw(ptr as *mut _);
99+
}
89100
let func = Box::into_raw(Box::new(Some(func)));
90101
glib_sys::g_main_context_invoke_full(
91102
self.to_glib_none().0,
@@ -114,19 +125,6 @@ impl MainContext {
114125
}
115126
}
116127

117-
unsafe extern "C" fn trampoline<F: FnOnce() + 'static>(func: gpointer) -> gboolean {
118-
let func: &mut Option<F> = &mut *(func as *mut Option<F>);
119-
let func = func
120-
.take()
121-
.expect("MainContext::invoke() closure called multiple times");
122-
func();
123-
glib_sys::G_SOURCE_REMOVE
124-
}
125-
126-
unsafe extern "C" fn destroy_closure<F: FnOnce() + 'static>(ptr: gpointer) {
127-
Box::<Option<F>>::from_raw(ptr as *mut _);
128-
}
129-
130128
struct ThreadDefaultContext<'a>(&'a MainContext);
131129

132130
impl<'a> ThreadDefaultContext<'a> {

src/signal.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ pub unsafe fn connect_raw<F>(
5454
trampoline: GCallback,
5555
closure: *mut F,
5656
) -> SignalHandlerId {
57+
unsafe extern "C" fn destroy_closure<F>(ptr: *mut c_void, _: *mut gobject_sys::GClosure) {
58+
// destroy
59+
Box::<F>::from_raw(ptr as *mut _);
60+
}
5761
assert_eq!(mem::size_of::<*mut F>(), mem::size_of::<gpointer>());
5862
assert!(trampoline.is_some());
5963
let handle = gobject_sys::g_signal_connect_data(
@@ -104,8 +108,3 @@ pub fn signal_stop_emission_by_name<T: ObjectType>(instance: &T, signal_name: &s
104108
);
105109
}
106110
}
107-
108-
unsafe extern "C" fn destroy_closure<F>(ptr: *mut c_void, _: *mut gobject_sys::GClosure) {
109-
// destroy
110-
Box::<F>::from_raw(ptr as *mut _);
111-
}

src/subclass/boxed.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ pub trait BoxedType: Clone + Sized + 'static {
4141
///
4242
/// [`glib_boxed_type!`]: ../../macro.glib_boxed_type.html
4343
pub fn register_boxed_type<T: BoxedType>() -> ::Type {
44+
unsafe extern "C" fn boxed_copy<T: BoxedType>(v: glib_sys::gpointer) -> glib_sys::gpointer {
45+
let v = &*(v as *mut T);
46+
let copy = Box::new(v.clone());
47+
48+
Box::into_raw(copy) as glib_sys::gpointer
49+
}
50+
unsafe extern "C" fn boxed_free<T: BoxedType>(v: glib_sys::gpointer) {
51+
let v = v as *mut T;
52+
let _ = Box::from_raw(v);
53+
}
4454
unsafe {
4555
use std::ffi::CString;
4656

@@ -60,18 +70,6 @@ pub fn register_boxed_type<T: BoxedType>() -> ::Type {
6070
}
6171
}
6272

63-
unsafe extern "C" fn boxed_copy<T: BoxedType>(v: glib_sys::gpointer) -> glib_sys::gpointer {
64-
let v = &*(v as *mut T);
65-
let copy = Box::new(v.clone());
66-
67-
Box::into_raw(copy) as glib_sys::gpointer
68-
}
69-
70-
unsafe extern "C" fn boxed_free<T: BoxedType>(v: glib_sys::gpointer) {
71-
let v = v as *mut T;
72-
let _ = Box::from_raw(v);
73-
}
74-
7573
#[macro_export]
7674
/// Macro for defining a `get_type` function.
7775
///

src/translate.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,17 +1829,6 @@ where
18291829
}
18301830
}
18311831

1832-
unsafe extern "C" fn read_string_hash_table(
1833-
key: glib_sys::gpointer,
1834-
value: glib_sys::gpointer,
1835-
hash_map: glib_sys::gpointer,
1836-
) {
1837-
let key: String = from_glib_none(key as *const c_char);
1838-
let value: String = from_glib_none(value as *const c_char);
1839-
let hash_map: &mut HashMap<String, String> = &mut *(hash_map as *mut HashMap<String, String>);
1840-
hash_map.insert(key, value);
1841-
}
1842-
18431832
#[allow(clippy::implicit_hasher)]
18441833
impl FromGlibContainer<*const c_char, *mut glib_sys::GHashTable> for HashMap<String, String> {
18451834
unsafe fn from_glib_none_num(ptr: *mut glib_sys::GHashTable, _: usize) -> Self {
@@ -1858,6 +1847,17 @@ impl FromGlibContainer<*const c_char, *mut glib_sys::GHashTable> for HashMap<Str
18581847
#[allow(clippy::implicit_hasher)]
18591848
impl FromGlibPtrContainer<*const c_char, *mut glib_sys::GHashTable> for HashMap<String, String> {
18601849
unsafe fn from_glib_none(ptr: *mut glib_sys::GHashTable) -> Self {
1850+
unsafe extern "C" fn read_string_hash_table(
1851+
key: glib_sys::gpointer,
1852+
value: glib_sys::gpointer,
1853+
hash_map: glib_sys::gpointer,
1854+
) {
1855+
let key: String = from_glib_none(key as *const c_char);
1856+
let value: String = from_glib_none(value as *const c_char);
1857+
let hash_map: &mut HashMap<String, String> =
1858+
&mut *(hash_map as *mut HashMap<String, String>);
1859+
hash_map.insert(key, value);
1860+
}
18611861
let mut map = HashMap::new();
18621862
glib_sys::g_hash_table_foreach(
18631863
ptr,

src/value_array.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,22 @@ impl ValueArray {
6363
}
6464

6565
pub fn sort_with_data<F: FnMut(&Value, &Value) -> Ordering>(&mut self, compare_func: F) {
66+
unsafe extern "C" fn compare_func_trampoline(
67+
a: glib_sys::gconstpointer,
68+
b: glib_sys::gconstpointer,
69+
func: glib_sys::gpointer,
70+
) -> i32 {
71+
let func = func as *mut &mut (dyn FnMut(&Value, &Value) -> Ordering);
72+
73+
let a = &*(a as *const Value);
74+
let b = &*(b as *const Value);
75+
76+
match (*func)(&a, &b) {
77+
Ordering::Less => -1,
78+
Ordering::Equal => 0,
79+
Ordering::Greater => 1,
80+
}
81+
}
6682
unsafe {
6783
let mut func = compare_func;
6884
let func_obj: &mut (dyn FnMut(&Value, &Value) -> Ordering) = &mut func;
@@ -101,20 +117,3 @@ impl ops::DerefMut for ValueArray {
101117
}
102118
}
103119
}
104-
105-
unsafe extern "C" fn compare_func_trampoline(
106-
a: glib_sys::gconstpointer,
107-
b: glib_sys::gconstpointer,
108-
func: glib_sys::gpointer,
109-
) -> i32 {
110-
let func = func as *mut &mut (dyn FnMut(&Value, &Value) -> Ordering);
111-
112-
let a = &*(a as *const Value);
113-
let b = &*(b as *const Value);
114-
115-
match (*func)(&a, &b) {
116-
Ordering::Less => -1,
117-
Ordering::Equal => 0,
118-
Ordering::Greater => 1,
119-
}
120-
}

0 commit comments

Comments
 (0)