Skip to content

Commit

Permalink
gdk-pixbuf: 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 38b6463 commit 0cbea41
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 27 deletions.
45 changes: 36 additions & 9 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 @@ -32,11 +35,18 @@ pub trait PixbufAnimationImpl: ObjectImpl {
}

mod sealed {
use glib::{prelude::*, subclass::prelude::*};
pub trait Sealed {}
impl<T: super::PixbufAnimationImplExt> Sealed for T {}
impl<T: super::PixbufAnimationImplExt> Sealed for T where
<T as ObjectSubclass>::Type: IsA<super::PixbufAnimation>
{
}
}

pub trait PixbufAnimationImplExt: sealed::Sealed + ObjectSubclass {
pub trait PixbufAnimationImplExt: sealed::Sealed + ObjectSubclass
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 +126,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 +148,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 +162,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 +183,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 +216,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
46 changes: 37 additions & 9 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 @@ -33,11 +36,18 @@ pub trait PixbufAnimationIterImpl: ObjectImpl {
}

mod sealed {
use glib::{prelude::*, subclass::prelude::*};
pub trait Sealed {}
impl<T: super::PixbufAnimationIterImplExt> Sealed for T {}
impl<T: super::PixbufAnimationIterImplExt> Sealed for T where
<T as ObjectSubclass>::Type: IsA<super::PixbufAnimationIter>
{
}
}

pub trait PixbufAnimationIterImplExt: sealed::Sealed + ObjectSubclass {
pub trait PixbufAnimationIterImplExt: sealed::Sealed + ObjectSubclass
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 +131,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 +153,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 +165,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 +184,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 +197,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
44 changes: 35 additions & 9 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 @@ -26,11 +29,18 @@ pub trait PixbufLoaderImpl: ObjectImpl {
}

mod sealed {
use glib::{prelude::*, subclass::prelude::*};
pub trait Sealed {}
impl<T: super::PixbufLoaderImplExt> Sealed for T {}
impl<T: super::PixbufLoaderImplExt> Sealed for T where
<Self as ObjectSubclass>::Type: IsA<super::PixbufLoader>
{
}
}

pub trait PixbufLoaderImplExt: sealed::Sealed + ObjectSubclass {
pub trait PixbufLoaderImplExt: sealed::Sealed + ObjectSubclass
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 +106,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 +130,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 +155,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

0 comments on commit 0cbea41

Please sign in to comment.