Skip to content

Commit

Permalink
Merge pull request #1519 from felinira/wip/impl-trait-bounds
Browse files Browse the repository at this point in the history
Add additional type bounds to Impl traits
  • Loading branch information
sdroege authored Oct 3, 2024
2 parents d8dfa3a + 80569bd commit 5ec8462
Show file tree
Hide file tree
Showing 26 changed files with 823 additions and 262 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ Paolo Borelli:
- gio: use `StrV` for the `file_info` API
- gio: use `GStr` for the manual extension point implementation
- glib: list: mark as transparent and impl TransparentPtr
- glib: Rename `StrVItem` to `GStrPtr` and make it clonable and transparent
- glib: Rename `StrVItem` to `GStrPtr` and make it cloneable and transparent
- glib: `key_file`: return `PtrSlice<GStrPtr>`
- glib-macros: further tweak docs
- glib: Rename `GStrPtr` to `GStringPtr`
Expand Down
53 changes: 40 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,11 @@ 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<glib::Object>,
<Self as ObjectSubclass>::Type: IsA<PixbufAnimation>,
{
fn is_static_image(&self) -> bool {
self.parent_is_static_image()
}
Expand All @@ -31,12 +35,11 @@ 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<glib::Object>,
<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 +119,18 @@ pub trait PixbufAnimationImplExt: sealed::Sealed + ObjectSubclass {
}
}

impl<T: PixbufAnimationImpl> PixbufAnimationImplExt for T {}
impl<T: PixbufAnimationImpl> PixbufAnimationImplExt for T
where
<T as ObjectSubclass>::Type: IsA<glib::Object>,
<T 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<glib::Object>,
<T as ObjectSubclass>::Type: IsA<PixbufAnimation>,
{
fn class_init(class: &mut ::glib::Class<Self>) {
Self::parent_class_init::<T>(class);

Expand All @@ -132,7 +144,11 @@ 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<glib::Object>,
<T as ObjectSubclass>::Type: IsA<PixbufAnimation>,
{
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();

Expand All @@ -143,7 +159,10 @@ 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 @@ -162,7 +181,11 @@ 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<glib::Object>,
<T as ObjectSubclass>::Type: IsA<PixbufAnimation>,
{
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();

Expand Down Expand Up @@ -192,7 +215,11 @@ 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<glib::Object>,
<T as ObjectSubclass>::Type: IsA<PixbufAnimation>,
{
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();

Expand Down
54 changes: 41 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,11 @@ 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<glib::Object>,
<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 +36,11 @@ 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<glib::Object>,
<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 +124,18 @@ pub trait PixbufAnimationIterImplExt: sealed::Sealed + ObjectSubclass {
}
}

impl<T: PixbufAnimationIterImpl> PixbufAnimationIterImplExt for T {}
impl<T: PixbufAnimationIterImpl> PixbufAnimationIterImplExt for T
where
<T as ObjectSubclass>::Type: IsA<glib::Object>,
<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<glib::Object>,
<T as ObjectSubclass>::Type: IsA<PixbufAnimationIter>,
{
fn class_init(class: &mut ::glib::Class<Self>) {
Self::parent_class_init::<T>(class);

Expand All @@ -137,7 +149,11 @@ 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<glib::Object>,
<T as ObjectSubclass>::Type: IsA<PixbufAnimationIter>,
{
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();

Expand All @@ -146,7 +162,11 @@ 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<glib::Object>,
<T as ObjectSubclass>::Type: IsA<PixbufAnimationIter>,
{
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();

Expand All @@ -162,7 +182,11 @@ 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<glib::Object>,
<T as ObjectSubclass>::Type: IsA<PixbufAnimationIter>,
{
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();

Expand All @@ -172,7 +196,11 @@ 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<glib::Object>,
<T as ObjectSubclass>::Type: IsA<PixbufAnimationIter>,
{
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();

Expand Down
52 changes: 39 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,11 @@ use glib::{prelude::*, subclass::prelude::*, translate::*};

use crate::{ffi, PixbufLoader};

pub trait PixbufLoaderImpl: ObjectImpl {
pub trait PixbufLoaderImpl: ObjectImpl
where
<Self as ObjectSubclass>::Type: IsA<glib::Object>,
<Self as ObjectSubclass>::Type: IsA<PixbufLoader>,
{
fn size_prepared(&self, width: i32, height: i32) {
self.parent_size_prepared(width, height)
}
Expand All @@ -25,12 +29,11 @@ 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<glib::Object>,
<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 +99,18 @@ pub trait PixbufLoaderImplExt: sealed::Sealed + ObjectSubclass {
}
}

impl<T: PixbufLoaderImpl> PixbufLoaderImplExt for T {}
impl<T: PixbufLoaderImpl> PixbufLoaderImplExt for T
where
<T as ObjectSubclass>::Type: IsA<glib::Object>,
<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<glib::Object>,
<T as ObjectSubclass>::Type: IsA<PixbufLoader>,
{
fn class_init(class: &mut ::glib::Class<Self>) {
Self::parent_class_init::<T>(class);

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

Expand All @@ -134,14 +153,21 @@ unsafe extern "C" fn loader_area_updated<T: PixbufLoaderImpl>(
y: i32,
width: i32,
height: i32,
) {
) where
<T as ObjectSubclass>::Type: IsA<glib::Object>,
<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<glib::Object>,
<T as ObjectSubclass>::Type: IsA<PixbufLoader>,
{
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();

Expand Down
Loading

0 comments on commit 5ec8462

Please sign in to comment.