Commit b3975ae6 authored by Rasmus Thomsen's avatar Rasmus Thomsen
Browse files

refactor(import_export_dialog_base|view): cleanup subclassing implementation

parent 8116aa53
Pipeline #339609 passed with stages
in 27 minutes and 2 seconds
......@@ -35,7 +35,7 @@ mod imp {
#[repr(C)]
pub struct ViewClass {
pub parent_class: gtk::ffi::GtkWidgetClass,
pub update: Option<unsafe fn(&ViewInstance) -> PinnedResultFuture>,
pub update: fn(&ViewInstance) -> PinnedResultFuture,
}
unsafe impl ClassStruct for ViewClass {
......@@ -81,10 +81,10 @@ mod imp {
View::from_instance(this).update(this)
}
pub(super) unsafe fn view_update(this: &ViewInstance) -> PinnedResultFuture {
let klass = &*(this.class() as *const _ as *const ViewClass);
pub(super) fn view_update(this: &ViewInstance) -> PinnedResultFuture {
let klass = this.class();
(klass.update.unwrap())(this)
(klass.as_ref().update)(this)
}
impl View {
......@@ -103,7 +103,7 @@ mod imp {
type Class = ViewClass;
fn class_init(klass: &mut Self::Class) {
klass.update = Some(update_default_trampoline);
klass.update = update_default_trampoline;
klass.set_layout_manager_type::<gtk::BinLayout>();
Self::bind_template(klass);
......@@ -240,7 +240,7 @@ pub trait ViewExt {
impl<O: IsA<View>> ViewExt for O {
fn update(&self) -> PinnedResultFuture {
unsafe { imp::view_update(self.upcast_ref()) }
imp::view_update(self.upcast_ref())
}
}
......@@ -258,12 +258,8 @@ impl<T: ViewImpl> ViewImplExt for T {
fn parent_update(&self, obj: &View) -> PinnedResultFuture {
unsafe {
let data = Self::type_data();
let parent_class = data.as_ref().parent_class().cast::<imp::ViewClass>();
if let Some(ref f) = (*parent_class).update {
f(obj)
} else {
unimplemented!()
}
let parent_class = &*(data.as_ref().parent_class() as *mut imp::ViewClass);
(parent_class.update)(obj)
}
}
}
......@@ -273,7 +269,7 @@ unsafe impl<T: ViewImpl> IsSubclassable<T> for View {
<gtk::Widget as IsSubclassable<T>>::class_init(class);
let klass = class.as_mut();
klass.update = Some(update_trampoline::<T>);
klass.update = update_trampoline::<T>;
}
fn instance_init(instance: &mut glib::subclass::InitializingObject<T>) {
......@@ -282,11 +278,10 @@ unsafe impl<T: ViewImpl> IsSubclassable<T> for View {
}
// Virtual method default implementation trampolines
unsafe fn update_trampoline<T: ObjectSubclass>(this: &View) -> PinnedResultFuture
fn update_trampoline<T: ObjectSubclass>(this: &View) -> PinnedResultFuture
where
T: ViewImpl,
{
let instance = &*(this as *const _ as *const T::Instance);
let imp = instance.impl_();
let imp = T::from_instance(this.dynamic_cast_ref::<T::Type>().unwrap());
imp.update(this)
}
......@@ -42,18 +42,10 @@ mod imp {
#[repr(C)]
pub struct ImportExportDialogBaseClass {
pub parent_class: gtk::ffi::GtkDialogClass,
pub on_activities: Option<
unsafe fn(
&ImportExportDialogBaseInstance,
password: Option<String>,
) -> PinnedResultFuture,
>,
pub on_weights: Option<
unsafe fn(
&ImportExportDialogBaseInstance,
password: Option<String>,
) -> PinnedResultFuture,
>,
pub on_activities:
fn(&ImportExportDialogBaseInstance, password: Option<String>) -> PinnedResultFuture,
pub on_weights:
fn(&ImportExportDialogBaseInstance, password: Option<String>) -> PinnedResultFuture,
}
unsafe impl ClassStruct for ImportExportDialogBaseClass {
......@@ -120,22 +112,22 @@ mod imp {
ImportExportDialogBase::from_instance(this).on_weights(this, password)
}
pub(super) unsafe fn import_export_dialog_base_on_activities(
pub(super) fn import_export_dialog_base_on_activities(
this: &ImportExportDialogBaseInstance,
password: Option<String>,
) -> PinnedResultFuture {
let klass = &*(this.class() as *const _ as *const ImportExportDialogBaseClass);
let klass = this.class();
(klass.on_activities.unwrap())(this, password)
(klass.as_ref().on_activities)(this, password)
}
pub(super) unsafe fn import_export_dialog_base_on_weights(
pub(super) fn import_export_dialog_base_on_weights(
this: &ImportExportDialogBaseInstance,
password: Option<String>,
) -> PinnedResultFuture {
let klass = &*(this.class() as *const _ as *const ImportExportDialogBaseClass);
let klass = this.class();
(klass.on_weights.unwrap())(this, password)
(klass.as_ref().on_weights)(this, password)
}
impl ImportExportDialogBase {
......@@ -168,8 +160,8 @@ mod imp {
type Class = ImportExportDialogBaseClass;
fn class_init(klass: &mut Self::Class) {
klass.on_activities = Some(on_activities_default_trampoline);
klass.on_weights = Some(on_weights_default_trampoline);
klass.on_activities = on_activities_default_trampoline;
klass.on_weights = on_weights_default_trampoline;
Self::bind_template(klass);
}
......@@ -354,11 +346,11 @@ pub trait ImportExportDialogBaseExt {
impl<O: IsA<ImportExportDialogBase>> ImportExportDialogBaseExt for O {
fn on_activities(&self, password: Option<String>) -> PinnedResultFuture {
unsafe { imp::import_export_dialog_base_on_activities(self.upcast_ref(), password) }
imp::import_export_dialog_base_on_activities(self.upcast_ref(), password)
}
fn on_weights(&self, password: Option<String>) -> PinnedResultFuture {
unsafe { imp::import_export_dialog_base_on_weights(self.upcast_ref(), password) }
imp::import_export_dialog_base_on_weights(self.upcast_ref(), password)
}
}
......@@ -401,15 +393,9 @@ impl<T: ImportExportDialogBaseImpl> ImportExportDialogBaseImplExt for T {
) -> PinnedResultFuture {
unsafe {
let data = Self::type_data();
let parent_class = data
.as_ref()
.parent_class()
.cast::<imp::ImportExportDialogBaseClass>();
if let Some(ref f) = (*parent_class).on_activities {
f(obj, password)
} else {
unimplemented!()
}
let parent_class =
&*(data.as_ref().parent_class() as *mut imp::ImportExportDialogBaseClass);
(parent_class.on_activities)(obj, password)
}
}
......@@ -420,15 +406,9 @@ impl<T: ImportExportDialogBaseImpl> ImportExportDialogBaseImplExt for T {
) -> PinnedResultFuture {
unsafe {
let data = Self::type_data();
let parent_class = data
.as_ref()
.parent_class()
.cast::<imp::ImportExportDialogBaseClass>();
if let Some(ref f) = (*parent_class).on_weights {
f(obj, password)
} else {
unimplemented!()
}
let parent_class =
&*(data.as_ref().parent_class() as *mut imp::ImportExportDialogBaseClass);
(parent_class.on_weights)(obj, password)
}
}
}
......@@ -438,8 +418,8 @@ unsafe impl<T: ImportExportDialogBaseImpl> IsSubclassable<T> for ImportExportDia
<gtk::Dialog as IsSubclassable<T>>::class_init(class.upcast_ref_mut());
let klass = class.as_mut();
klass.on_activities = Some(on_activities_trampoline::<T>);
klass.on_weights = Some(on_weights_trampoline::<T>);
klass.on_activities = on_activities_trampoline::<T>;
klass.on_weights = on_weights_trampoline::<T>;
}
fn instance_init(instance: &mut glib::subclass::InitializingObject<T>) {
......@@ -448,26 +428,24 @@ unsafe impl<T: ImportExportDialogBaseImpl> IsSubclassable<T> for ImportExportDia
}
// Virtual method default implementation trampolines
unsafe fn on_activities_trampoline<T: ObjectSubclass>(
fn on_activities_trampoline<T: ObjectSubclass>(
this: &imp::ImportExportDialogBaseInstance,
password: Option<String>,
) -> PinnedResultFuture
where
T: ImportExportDialogBaseImpl,
{
let instance = &*(this as *const _ as *const T::Instance);
let imp = instance.impl_();
let imp = T::from_instance(this.dynamic_cast_ref::<T::Type>().unwrap());
imp.on_activities(this, password)
}
unsafe fn on_weights_trampoline<T: ObjectSubclass>(
fn on_weights_trampoline<T: ObjectSubclass>(
this: &imp::ImportExportDialogBaseInstance,
password: Option<String>,
) -> PinnedResultFuture
where
T: ImportExportDialogBaseImpl,
{
let instance = &*(this as *const _ as *const T::Instance);
let imp = instance.impl_();
let imp = T::from_instance(this.dynamic_cast_ref::<T::Type>().unwrap());
imp.on_weights(this, password)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment