Skip to content

Commit

Permalink
Use rust 1.79 implied supertraits to simplify trait bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
felinira committed Oct 5, 2024
1 parent 6224359 commit bafbe83
Show file tree
Hide file tree
Showing 27 changed files with 205 additions and 863 deletions.
16 changes: 2 additions & 14 deletions examples/virtual_methods/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,7 @@ impl Default for Cat {
///
/// By convention we still create an empty `CatImpl` trait, this allows us to add
/// 'protected' cat methods only available to be called by other Cats later.
pub trait CatImpl: PetImpl
where
<Self as ObjectSubclass>::Type: IsA<glib::Object>,
<Self as ObjectSubclass>::Type: IsA<Pet>,
<Self as ObjectSubclass>::Type: IsA<Cat>,
{
}
pub trait CatImpl: PetImpl + ObjectSubclass<Type: IsA<Cat>> {}

/// To make this class subclassable we need to implement IsSubclassable
unsafe impl<Obj: CatImpl + PetImpl> IsSubclassable<Obj> for Cat
where
<Obj as ObjectSubclass>::Type: IsA<glib::Object>,
<Obj as ObjectSubclass>::Type: IsA<Pet>,
<Obj as ObjectSubclass>::Type: IsA<Cat>,
{
}
unsafe impl<Obj: CatImpl + PetImpl> IsSubclassable<Obj> for Cat {}
25 changes: 4 additions & 21 deletions examples/virtual_methods/pet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,7 @@ pub trait PetExt: IsA<Pet> {
impl<T: IsA<Pet>> PetExt for T {}

/// The `PetImpl` trait contains overridable virtual function definitions for [`Pet`] objects.
pub trait PetImpl: ObjectImpl
where
<Self as ObjectSubclass>::Type: IsA<glib::Object>,
<Self as ObjectSubclass>::Type: IsA<Pet>,
{
pub trait PetImpl: ObjectImpl + ObjectSubclass<Type: IsA<Pet>> {
/// Default implementation of a virtual method.
///
/// This always calls into the implementation of the parent class so that if
Expand All @@ -152,11 +148,7 @@ where
/// The `PetImplExt` trait contains non-overridable methods for subclasses to use.
///
/// These are supposed to be called only from inside implementations of `Pet` subclasses.
pub trait PetImplExt: PetImpl
where
<Self as ObjectSubclass>::Type: IsA<glib::Object>,
<Self as ObjectSubclass>::Type: IsA<Pet>,
{
pub trait PetImplExt: PetImpl {
/// Chains up to the parent implementation of [`PetImpl::pet`]
fn parent_pet(&self) -> bool {
let data = Self::type_data();
Expand All @@ -177,19 +169,10 @@ where
}

/// The `PetImplExt` trait is implemented for all subclasses that have [`Pet`] in the class hierarchy
impl<T: PetImpl> PetImplExt for T
where
<Self as ObjectSubclass>::Type: IsA<glib::Object>,
<Self as ObjectSubclass>::Type: IsA<Pet>,
{
}
impl<T: PetImpl> PetImplExt for T {}

/// To make this class subclassable we need to implement IsSubclassable
unsafe impl<Obj: PetImpl> IsSubclassable<Obj> for Pet
where
<Obj as ObjectSubclass>::Type: IsA<glib::Object>,
<Obj as ObjectSubclass>::Type: IsA<Pet>,
{
unsafe impl<Obj: PetImpl> IsSubclassable<Obj> for Pet {
/// Override the virtual method function pointers in subclasses to call directly into the
/// `PetImpl` of the subclass.
///
Expand Down
25 changes: 4 additions & 21 deletions examples/virtual_methods/purrable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,7 @@ pub trait PurrableExt: IsA<Purrable> {
impl<T: IsA<Purrable>> PurrableExt for T {}

/// The `PurrableImpl` trait contains virtual function definitions for [`Purrable`] objects.
pub trait PurrableImpl: ObjectImpl
where
<Self as ObjectSubclass>::Type: IsA<glib::Object>,
<Self as ObjectSubclass>::Type: IsA<Purrable>,
{
pub trait PurrableImpl: ObjectImpl + ObjectSubclass<Type: IsA<Purrable>> {
/// Return the current purring status.
///
/// The default implementation chains up to the parent implementation,
Expand All @@ -100,11 +96,7 @@ where
/// The `PurrableImplExt` trait contains non-overridable methods for subclasses to use.
///
/// These are supposed to be called only from inside implementations of `Pet` subclasses.
pub trait PurrableImplExt: PurrableImpl
where
<Self as ObjectSubclass>::Type: IsA<glib::Object>,
<Self as ObjectSubclass>::Type: IsA<Purrable>,
{
pub trait PurrableImplExt: PurrableImpl {
/// Chains up to the parent implementation of [`PurrableExt::is_purring`]
fn parent_is_purring(&self) -> bool {
let data = Self::type_data();
Expand All @@ -117,19 +109,10 @@ where
}

/// The `PurrableImplExt` trait is implemented for all classes that implement [`Purrable`].
impl<T: PurrableImpl> PurrableImplExt for T
where
<Self as ObjectSubclass>::Type: IsA<glib::Object>,
<Self as ObjectSubclass>::Type: IsA<Purrable>,
{
}
impl<T: PurrableImpl> PurrableImplExt for T {}

/// To make this interface implementable we need to implement [`IsImplementable`]
unsafe impl<Obj: PurrableImpl> IsImplementable<Obj> for Purrable
where
<Obj as ObjectSubclass>::Type: IsA<glib::Object>,
<Obj as ObjectSubclass>::Type: IsA<Purrable>,
{
unsafe impl<Obj: PurrableImpl> IsImplementable<Obj> for Purrable {
fn interface_init(iface: &mut glib::Interface<Self>) {
let klass = iface.as_mut();

Expand Down
48 changes: 8 additions & 40 deletions gdk-pixbuf/src/subclass/pixbuf_animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ use glib::{prelude::*, subclass::prelude::*, translate::*};

use crate::{ffi, Pixbuf, PixbufAnimation, PixbufAnimationIter};

pub trait PixbufAnimationImpl: ObjectImpl
where
<Self as ObjectSubclass>::Type: IsA<glib::Object>,
<Self as ObjectSubclass>::Type: IsA<PixbufAnimation>,
{
pub trait PixbufAnimationImpl: ObjectImpl + ObjectSubclass<Type: IsA<PixbufAnimation>> {
fn is_static_image(&self) -> bool {
self.parent_is_static_image()
}
Expand All @@ -35,11 +31,7 @@ where
}
}

pub trait PixbufAnimationImplExt: ObjectSubclass + PixbufAnimationImpl
where
<Self as ObjectSubclass>::Type: IsA<glib::Object>,
<Self as ObjectSubclass>::Type: IsA<PixbufAnimation>,
{
pub trait PixbufAnimationImplExt: PixbufAnimationImpl {
fn parent_is_static_image(&self) -> bool {
unsafe {
let data = Self::type_data();
Expand Down Expand Up @@ -119,18 +111,9 @@ where
}
}

impl<T: PixbufAnimationImpl> PixbufAnimationImplExt for T
where
<T as ObjectSubclass>::Type: IsA<glib::Object>,
<T as ObjectSubclass>::Type: IsA<PixbufAnimation>,
{
}
impl<T: PixbufAnimationImpl> PixbufAnimationImplExt for T {}

unsafe impl<T: PixbufAnimationImpl> IsSubclassable<T> for PixbufAnimation
where
<T as ObjectSubclass>::Type: IsA<glib::Object>,
<T as ObjectSubclass>::Type: IsA<PixbufAnimation>,
{
unsafe impl<T: PixbufAnimationImpl> IsSubclassable<T> for PixbufAnimation {
fn class_init(class: &mut ::glib::Class<Self>) {
Self::parent_class_init::<T>(class);

Expand All @@ -144,11 +127,7 @@ where

unsafe extern "C" fn animation_is_static_image<T: PixbufAnimationImpl>(
ptr: *mut ffi::GdkPixbufAnimation,
) -> glib::ffi::gboolean
where
<T as ObjectSubclass>::Type: IsA<glib::Object>,
<T as ObjectSubclass>::Type: IsA<PixbufAnimation>,
{
) -> glib::ffi::gboolean {
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();

Expand All @@ -159,10 +138,7 @@ unsafe extern "C" fn animation_get_size<T: PixbufAnimationImpl>(
ptr: *mut ffi::GdkPixbufAnimation,
width_ptr: *mut libc::c_int,
height_ptr: *mut libc::c_int,
) where
<T as ObjectSubclass>::Type: IsA<glib::Object>,
<T as ObjectSubclass>::Type: IsA<PixbufAnimation>,
{
) {
if width_ptr.is_null() && height_ptr.is_null() {
return;
}
Expand All @@ -181,11 +157,7 @@ unsafe extern "C" fn animation_get_size<T: PixbufAnimationImpl>(

unsafe extern "C" fn animation_get_static_image<T: PixbufAnimationImpl>(
ptr: *mut ffi::GdkPixbufAnimation,
) -> *mut ffi::GdkPixbuf
where
<T as ObjectSubclass>::Type: IsA<glib::Object>,
<T as ObjectSubclass>::Type: IsA<PixbufAnimation>,
{
) -> *mut ffi::GdkPixbuf {
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();

Expand Down Expand Up @@ -215,11 +187,7 @@ where
unsafe extern "C" fn animation_get_iter<T: PixbufAnimationImpl>(
ptr: *mut ffi::GdkPixbufAnimation,
start_time_ptr: *const glib::ffi::GTimeVal,
) -> *mut ffi::GdkPixbufAnimationIter
where
<T as ObjectSubclass>::Type: IsA<glib::Object>,
<T as ObjectSubclass>::Type: IsA<PixbufAnimation>,
{
) -> *mut ffi::GdkPixbufAnimationIter {
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();

Expand Down
49 changes: 9 additions & 40 deletions gdk-pixbuf/src/subclass/pixbuf_animation_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ use glib::{prelude::*, subclass::prelude::*, translate::*};

use crate::{ffi, Pixbuf, PixbufAnimationIter};

pub trait PixbufAnimationIterImpl: ObjectImpl
where
<Self as ObjectSubclass>::Type: IsA<glib::Object>,
<Self as ObjectSubclass>::Type: IsA<PixbufAnimationIter>,
pub trait PixbufAnimationIterImpl:
ObjectImpl + ObjectSubclass<Type: IsA<PixbufAnimationIter>>
{
// rustdoc-stripper-ignore-next
/// Time in milliseconds, returning `None` implies showing the same pixbuf forever.
Expand All @@ -36,11 +34,7 @@ where
}
}

pub trait PixbufAnimationIterImplExt: ObjectSubclass + PixbufAnimationIterImpl
where
<Self as ObjectSubclass>::Type: IsA<glib::Object>,
<Self as ObjectSubclass>::Type: IsA<PixbufAnimationIter>,
{
pub trait PixbufAnimationIterImplExt: PixbufAnimationIterImpl {
fn parent_delay_time(&self) -> Option<Duration> {
unsafe {
let data = Self::type_data();
Expand Down Expand Up @@ -124,18 +118,9 @@ where
}
}

impl<T: PixbufAnimationIterImpl> PixbufAnimationIterImplExt for T
where
<T as ObjectSubclass>::Type: IsA<glib::Object>,
<T as ObjectSubclass>::Type: IsA<PixbufAnimationIter>,
{
}
impl<T: PixbufAnimationIterImpl> PixbufAnimationIterImplExt for T {}

unsafe impl<T: PixbufAnimationIterImpl> IsSubclassable<T> for PixbufAnimationIter
where
<T as ObjectSubclass>::Type: IsA<glib::Object>,
<T as ObjectSubclass>::Type: IsA<PixbufAnimationIter>,
{
unsafe impl<T: PixbufAnimationIterImpl> IsSubclassable<T> for PixbufAnimationIter {
fn class_init(class: &mut ::glib::Class<Self>) {
Self::parent_class_init::<T>(class);

Expand All @@ -149,11 +134,7 @@ where

unsafe extern "C" fn animation_iter_get_delay_time<T: PixbufAnimationIterImpl>(
ptr: *mut ffi::GdkPixbufAnimationIter,
) -> i32
where
<T as ObjectSubclass>::Type: IsA<glib::Object>,
<T as ObjectSubclass>::Type: IsA<PixbufAnimationIter>,
{
) -> i32 {
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();

Expand All @@ -162,11 +143,7 @@ where

unsafe extern "C" fn animation_iter_get_pixbuf<T: PixbufAnimationIterImpl>(
ptr: *mut ffi::GdkPixbufAnimationIter,
) -> *mut ffi::GdkPixbuf
where
<T as ObjectSubclass>::Type: IsA<glib::Object>,
<T as ObjectSubclass>::Type: IsA<PixbufAnimationIter>,
{
) -> *mut ffi::GdkPixbuf {
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();

Expand All @@ -182,11 +159,7 @@ where

unsafe extern "C" fn animation_iter_on_currently_loading_frame<T: PixbufAnimationIterImpl>(
ptr: *mut ffi::GdkPixbufAnimationIter,
) -> glib::ffi::gboolean
where
<T as ObjectSubclass>::Type: IsA<glib::Object>,
<T as ObjectSubclass>::Type: IsA<PixbufAnimationIter>,
{
) -> glib::ffi::gboolean {
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();

Expand All @@ -196,11 +169,7 @@ where
unsafe extern "C" fn animation_iter_advance<T: PixbufAnimationIterImpl>(
ptr: *mut ffi::GdkPixbufAnimationIter,
current_time_ptr: *const glib::ffi::GTimeVal,
) -> glib::ffi::gboolean
where
<T as ObjectSubclass>::Type: IsA<glib::Object>,
<T as ObjectSubclass>::Type: IsA<PixbufAnimationIter>,
{
) -> glib::ffi::gboolean {
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();

Expand Down
Loading

0 comments on commit bafbe83

Please sign in to comment.