Skip to content

Commit

Permalink
glib: Add additional type bounds to Impl traits
Browse files Browse the repository at this point in the history
  • Loading branch information
felinira committed Sep 24, 2024
1 parent b97554d commit 38b6463
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 15 deletions.
1 change: 1 addition & 0 deletions glib/src/gobject/dynamic_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub trait DynamicObjectRegisterExt: AsRef<TypePlugin> + sealed::Sealed + 'static
impl<O: IsA<TypePlugin> + ObjectSubclassIsExt> DynamicObjectRegisterExt for O
where
O::Subclass: TypePluginRegisterImpl,
<O::Subclass as ObjectSubclass>::Type: IsA<TypePlugin>,
{
fn add_dynamic_interface(
&self,
Expand Down
30 changes: 24 additions & 6 deletions glib/src/subclass/type_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

use crate::{ffi, gobject_ffi, prelude::*, subclass::prelude::*, translate::*, TypeModule};

pub trait TypeModuleImpl: ObjectImpl + TypeModuleImplExt {
pub trait TypeModuleImpl: ObjectImpl + TypeModuleImplExt
where
<Self as ObjectSubclass>::Type: IsA<TypeModule>,
{
// rustdoc-stripper-ignore-next
/// Loads the module, registers one or more object subclasses using
/// [`register_dynamic_type`] and registers one or more object interfaces
Expand All @@ -21,12 +24,18 @@ pub trait TypeModuleImpl: ObjectImpl + TypeModuleImplExt {
fn unload(&self);
}

pub trait TypeModuleImplExt: ObjectSubclass {
pub trait TypeModuleImplExt: ObjectSubclass
where
<Self as ObjectSubclass>::Type: IsA<TypeModule>,
{
fn parent_load(&self) -> bool;
fn parent_unload(&self);
}

impl<T: TypeModuleImpl> TypeModuleImplExt for T {
impl<T: TypeModuleImpl> TypeModuleImplExt for T
where
<Self as ObjectSubclass>::Type: IsA<TypeModule>,
{
fn parent_load(&self) -> bool {
unsafe {
let data = T::type_data();
Expand Down Expand Up @@ -58,7 +67,10 @@ impl<T: TypeModuleImpl> TypeModuleImplExt for T {
}
}

unsafe impl<T: TypeModuleImpl> IsSubclassable<T> for TypeModule {
unsafe impl<T: TypeModuleImpl> IsSubclassable<T> for TypeModule
where
<T as ObjectSubclass>::Type: IsA<TypeModule>,
{
fn class_init(class: &mut crate::Class<Self>) {
Self::parent_class_init::<T>(class);

Expand All @@ -70,7 +82,10 @@ unsafe impl<T: TypeModuleImpl> IsSubclassable<T> for TypeModule {

unsafe extern "C" fn load<T: TypeModuleImpl>(
type_module: *mut gobject_ffi::GTypeModule,
) -> ffi::gboolean {
) -> ffi::gboolean
where
<T as ObjectSubclass>::Type: IsA<TypeModule>,
{
let instance = &*(type_module as *mut T::Instance);
let imp = instance.imp();

Expand All @@ -90,7 +105,10 @@ unsafe extern "C" fn load<T: TypeModuleImpl>(
res.into_glib()
}

unsafe extern "C" fn unload<T: TypeModuleImpl>(type_module: *mut gobject_ffi::GTypeModule) {
unsafe extern "C" fn unload<T: TypeModuleImpl>(type_module: *mut gobject_ffi::GTypeModule)
where
<T as ObjectSubclass>::Type: IsA<TypeModule>,
{
let instance = &*(type_module as *mut T::Instance);
let imp = instance.imp();

Expand Down
43 changes: 34 additions & 9 deletions glib/src/subclass/type_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use crate::{
Type, TypeFlags, TypeInfo, TypePlugin, TypeValueTable,
};

pub trait TypePluginImpl: ObjectImpl + TypePluginImplExt {
pub trait TypePluginImpl: ObjectImpl + TypePluginImplExt
where
<Self as ObjectSubclass>::Type: IsA<TypePlugin>,
{
fn use_plugin(&self) {
self.parent_use_plugin();
}
Expand All @@ -24,7 +27,10 @@ pub trait TypePluginImpl: ObjectImpl + TypePluginImplExt {
}
}

pub trait TypePluginImplExt: ObjectSubclass {
pub trait TypePluginImplExt: ObjectSubclass
where
<Self as ObjectSubclass>::Type: IsA<TypePlugin>,
{
fn parent_use_plugin(&self);
fn parent_unuse_plugin(&self);
fn parent_complete_type_info(&self, type_: Type) -> (TypeInfo, TypeValueTable);
Expand All @@ -35,7 +41,10 @@ pub trait TypePluginImplExt: ObjectSubclass {
) -> InterfaceInfo;
}

impl<T: TypePluginImpl> TypePluginImplExt for T {
impl<T: TypePluginImpl> TypePluginImplExt for T
where
<Self as ObjectSubclass>::Type: IsA<TypePlugin>,
{
fn parent_use_plugin(&self) {
unsafe {
let type_data = Self::type_data();
Expand Down Expand Up @@ -113,7 +122,10 @@ impl<T: TypePluginImpl> TypePluginImplExt for T {
}
}

unsafe impl<T: TypePluginImpl> IsImplementable<T> for TypePlugin {
unsafe impl<T: TypePluginImpl> IsImplementable<T> for TypePlugin
where
<T as ObjectSubclass>::Type: IsA<TypePlugin>,
{
fn interface_init(iface: &mut Interface<Self>) {
let iface = iface.as_mut();

Expand All @@ -124,14 +136,20 @@ unsafe impl<T: TypePluginImpl> IsImplementable<T> for TypePlugin {
}
}

unsafe extern "C" fn use_plugin<T: TypePluginImpl>(type_plugin: *mut gobject_ffi::GTypePlugin) {
unsafe extern "C" fn use_plugin<T: TypePluginImpl>(type_plugin: *mut gobject_ffi::GTypePlugin)
where
<T as ObjectSubclass>::Type: IsA<TypePlugin>,
{
let instance = &*(type_plugin as *mut T::Instance);
let imp = instance.imp();

imp.use_plugin();
}

unsafe extern "C" fn unuse_plugin<T: TypePluginImpl>(type_plugin: *mut gobject_ffi::GTypePlugin) {
unsafe extern "C" fn unuse_plugin<T: TypePluginImpl>(type_plugin: *mut gobject_ffi::GTypePlugin)
where
<T as ObjectSubclass>::Type: IsA<TypePlugin>,
{
let instance = &*(type_plugin as *mut T::Instance);
let imp = instance.imp();

Expand All @@ -143,7 +161,9 @@ unsafe extern "C" fn complete_type_info<T: TypePluginImpl>(
gtype: ffi::GType,
info_ptr: *mut gobject_ffi::GTypeInfo,
value_table_ptr: *mut gobject_ffi::GTypeValueTable,
) {
) where
<T as ObjectSubclass>::Type: IsA<TypePlugin>,
{
assert!(!info_ptr.is_null());
assert!(!value_table_ptr.is_null());
let instance = &*(type_plugin as *mut T::Instance);
Expand All @@ -163,7 +183,9 @@ unsafe extern "C" fn complete_interface_info<T: TypePluginImpl>(
instance_gtype: ffi::GType,
interface_gtype: ffi::GType,
info_ptr: *mut gobject_ffi::GInterfaceInfo,
) {
) where
<T as ObjectSubclass>::Type: IsA<TypePlugin>,
{
assert!(!info_ptr.is_null());
let instance = &*(type_plugin as *mut T::Instance);
let imp = instance.imp();
Expand All @@ -175,7 +197,10 @@ unsafe extern "C" fn complete_interface_info<T: TypePluginImpl>(
*info = info_;
}

pub trait TypePluginRegisterImpl: ObjectImpl + TypePluginImpl {
pub trait TypePluginRegisterImpl: ObjectImpl + TypePluginImpl
where
<Self as ObjectSubclass>::Type: IsA<TypePlugin>,
{
fn add_dynamic_interface(
&self,
_instance_type: Type,
Expand Down

0 comments on commit 38b6463

Please sign in to comment.