Skip to content

Commit e524d41

Browse files
committed
examples: make extra_text FFI safe
1 parent 28f3031 commit e524d41

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

examples/virtual_methods/base_button/imp.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub type PinnedFuture = Pin<Box<dyn Future<Output = Result<(), Error>> + 'static
1515
pub struct BaseButtonClass {
1616
pub parent_class: gtk::ffi::GtkButtonClass,
1717
pub sync_method:
18-
Option<unsafe extern "C" fn(*mut BaseButtonInstance, extra_text: Box<Option<String>>)>,
18+
Option<unsafe extern "C" fn(*mut BaseButtonInstance, extra_text: *mut Option<String>)>,
1919
pub async_method: Option<unsafe extern "C" fn(*mut BaseButtonInstance) -> PinnedFuture>,
2020
}
2121

@@ -43,10 +43,10 @@ pub struct BaseButton {}
4343
// Virtual method default implementation trampolines
4444
unsafe extern "C" fn sync_method_default_trampoline(
4545
this: *mut BaseButtonInstance,
46-
extra_text: Box<Option<String>>,
46+
extra_text: *mut Option<String>,
4747
) {
4848
let imp = (*this).impl_();
49-
imp.sync_method(&from_glib_borrow(this), extra_text)
49+
imp.sync_method(&from_glib_borrow(this), Box::from_raw(extra_text))
5050
}
5151

5252
unsafe extern "C" fn async_method_default_trampoline(
@@ -58,7 +58,7 @@ unsafe extern "C" fn async_method_default_trampoline(
5858

5959
pub unsafe extern "C" fn base_button_sync_method(
6060
this: *mut BaseButtonInstance,
61-
extra_text: Box<Option<String>>,
61+
extra_text: *mut Option<String>,
6262
) {
6363
let klass = glib::subclass::types::InstanceStruct::class(&*this);
6464

examples/virtual_methods/base_button/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ pub trait BaseButtonExt {
2525
/// and call the correct implementation of the function.
2626
impl<O: IsA<BaseButton>> BaseButtonExt for O {
2727
fn sync_method(&self, extra_text: Box<Option<String>>) {
28-
unsafe { imp::base_button_sync_method(self.as_ref().to_glib_none().0, extra_text) }
28+
unsafe {
29+
imp::base_button_sync_method(self.as_ref().to_glib_none().0, Box::into_raw(extra_text))
30+
}
2931
}
3032

3133
fn async_method(&self) -> PinnedFuture {
@@ -49,7 +51,7 @@ pub trait BaseButtonImpl: ButtonImpl + ObjectImpl + 'static {
4951
let data = Self::type_data();
5052
let parent_class = data.as_ref().parent_class() as *mut imp::BaseButtonClass;
5153
if let Some(ref f) = (*parent_class).sync_method {
52-
f(obj.to_glib_none().0, extra_text)
54+
f(obj.to_glib_none().0, Box::into_raw(extra_text))
5355
} else {
5456
unimplemented!()
5557
}
@@ -87,13 +89,13 @@ unsafe impl<T: BaseButtonImpl> IsSubclassable<T> for BaseButton {
8789
// Virtual method default implementation trampolines
8890
unsafe extern "C" fn sync_method_trampoline<T: ObjectSubclass>(
8991
this: *mut imp::BaseButtonInstance,
90-
extra_text: Box<Option<String>>,
92+
extra_text: *mut Option<String>,
9193
) where
9294
T: BaseButtonImpl,
9395
{
9496
let instance = &*(this as *const T::Instance);
9597
let imp = instance.impl_();
96-
imp.sync_method(&from_glib_borrow(this), extra_text)
98+
imp.sync_method(&from_glib_borrow(this), Box::from_raw(extra_text))
9799
}
98100

99101
unsafe extern "C" fn async_method_trampoline<T: ObjectSubclass>(

0 commit comments

Comments
 (0)