Skip to content

Commit

Permalink
Add additional type bounds to Impl traits
Browse files Browse the repository at this point in the history
Remove the Sealed traits, as they are now unnecessary.
  • Loading branch information
felinira authored and sdroege committed Sep 30, 2024
1 parent 12d1877 commit 701d8c6
Show file tree
Hide file tree
Showing 18 changed files with 496 additions and 214 deletions.
44 changes: 31 additions & 13 deletions gdk-pixbuf/src/subclass/pixbuf_animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ use glib::{prelude::*, subclass::prelude::*, translate::*};

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

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

mod sealed {
pub trait Sealed {}
impl<T: super::PixbufAnimationImplExt> Sealed for T {}
}

pub trait PixbufAnimationImplExt: sealed::Sealed + ObjectSubclass {
pub trait PixbufAnimationImplExt: ObjectSubclass + PixbufAnimationImpl
where
<Self as ObjectSubclass>::Type: IsA<PixbufAnimation>,
{
fn parent_is_static_image(&self) -> bool {
unsafe {
let data = Self::type_data();
Expand Down Expand Up @@ -116,9 +117,15 @@ pub trait PixbufAnimationImplExt: sealed::Sealed + ObjectSubclass {
}
}

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

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

Expand All @@ -132,7 +139,10 @@ unsafe impl<T: PixbufAnimationImpl> IsSubclassable<T> for PixbufAnimation {

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

Expand All @@ -143,7 +153,9 @@ 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<PixbufAnimation>,
{
if width_ptr.is_null() && height_ptr.is_null() {
return;
}
Expand All @@ -162,7 +174,10 @@ 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 {
) -> *mut ffi::GdkPixbuf
where
<T as ObjectSubclass>::Type: IsA<PixbufAnimation>,
{
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();

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

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

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

pub trait PixbufAnimationIterImpl: ObjectImpl {
pub trait PixbufAnimationIterImpl: ObjectImpl
where
<Self as ObjectSubclass>::Type: IsA<PixbufAnimationIter>,
{
// rustdoc-stripper-ignore-next
/// Time in milliseconds, returning `None` implies showing the same pixbuf forever.
fn delay_time(&self) -> Option<Duration> {
Expand All @@ -32,12 +35,10 @@ pub trait PixbufAnimationIterImpl: ObjectImpl {
}
}

mod sealed {
pub trait Sealed {}
impl<T: super::PixbufAnimationIterImplExt> Sealed for T {}
}

pub trait PixbufAnimationIterImplExt: sealed::Sealed + ObjectSubclass {
pub trait PixbufAnimationIterImplExt: ObjectSubclass + PixbufAnimationIterImpl
where
<Self as ObjectSubclass>::Type: IsA<PixbufAnimationIter>,
{
fn parent_delay_time(&self) -> Option<Duration> {
unsafe {
let data = Self::type_data();
Expand Down Expand Up @@ -121,9 +122,15 @@ pub trait PixbufAnimationIterImplExt: sealed::Sealed + ObjectSubclass {
}
}

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

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

Expand All @@ -137,7 +144,10 @@ unsafe impl<T: PixbufAnimationIterImpl> IsSubclassable<T> for PixbufAnimationIte

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

Expand All @@ -146,7 +156,10 @@ unsafe extern "C" fn animation_iter_get_delay_time<T: PixbufAnimationIterImpl>(

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

Expand All @@ -162,7 +175,10 @@ unsafe extern "C" fn animation_iter_get_pixbuf<T: PixbufAnimationIterImpl>(

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

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

Expand Down
43 changes: 30 additions & 13 deletions gdk-pixbuf/src/subclass/pixbuf_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use glib::{prelude::*, subclass::prelude::*, translate::*};

use crate::{ffi, PixbufLoader};

pub trait PixbufLoaderImpl: ObjectImpl {
pub trait PixbufLoaderImpl: ObjectImpl
where
<Self as ObjectSubclass>::Type: IsA<PixbufLoader>,
{
fn size_prepared(&self, width: i32, height: i32) {
self.parent_size_prepared(width, height)
}
Expand All @@ -25,12 +28,10 @@ pub trait PixbufLoaderImpl: ObjectImpl {
}
}

mod sealed {
pub trait Sealed {}
impl<T: super::PixbufLoaderImplExt> Sealed for T {}
}

pub trait PixbufLoaderImplExt: sealed::Sealed + ObjectSubclass {
pub trait PixbufLoaderImplExt: ObjectSubclass + PixbufLoaderImpl
where
<Self as ObjectSubclass>::Type: IsA<PixbufLoader>,
{
fn parent_size_prepared(&self, width: i32, height: i32) {
unsafe {
let data = Self::type_data();
Expand Down Expand Up @@ -96,9 +97,15 @@ pub trait PixbufLoaderImplExt: sealed::Sealed + ObjectSubclass {
}
}

impl<T: PixbufLoaderImpl> PixbufLoaderImplExt for T {}
impl<T: PixbufLoaderImpl> PixbufLoaderImplExt for T where
<T as ObjectSubclass>::Type: IsA<PixbufLoader>
{
}

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

Expand All @@ -114,14 +121,19 @@ unsafe extern "C" fn loader_size_prepared<T: PixbufLoaderImpl>(
ptr: *mut ffi::GdkPixbufLoader,
width: i32,
height: i32,
) {
) where
<T as ObjectSubclass>::Type: IsA<PixbufLoader>,
{
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();

imp.size_prepared(width, height)
}

unsafe extern "C" fn loader_area_prepared<T: PixbufLoaderImpl>(ptr: *mut ffi::GdkPixbufLoader) {
unsafe extern "C" fn loader_area_prepared<T: PixbufLoaderImpl>(ptr: *mut ffi::GdkPixbufLoader)
where
<T as ObjectSubclass>::Type: IsA<PixbufLoader>,
{
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();

Expand All @@ -134,14 +146,19 @@ unsafe extern "C" fn loader_area_updated<T: PixbufLoaderImpl>(
y: i32,
width: i32,
height: i32,
) {
) where
<T as ObjectSubclass>::Type: IsA<PixbufLoader>,
{
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();

imp.area_updated(x, y, width, height)
}

unsafe extern "C" fn loader_closed<T: PixbufLoaderImpl>(ptr: *mut ffi::GdkPixbufLoader) {
unsafe extern "C" fn loader_closed<T: PixbufLoaderImpl>(ptr: *mut ffi::GdkPixbufLoader)
where
<T as ObjectSubclass>::Type: IsA<PixbufLoader>,
{
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();

Expand Down
Loading

0 comments on commit 701d8c6

Please sign in to comment.