gtk-rs / gtk-rs-core

Rust bindings for GNOME libraries
https://gtk-rs.org/gtk-rs-core
MIT License
293 stars 115 forks source link

Class structs should be Copy #1381

Open felinira opened 7 months ago

felinira commented 7 months ago

Class structs should have a bound on Copy, because that's what GObject is doing internally. If we were exposing base_init this could be avoided.

See https://github.com/gtk-rs/gtk-rs-core/pull/1378#discussion_r1585972731_

sdroege commented 7 months ago

Also the instance struct

sdroege commented 6 months ago

The goal here would be the following, and making it actually compile, which requires adding bounds and derive Clone+Copy impls in various places

diff --git a/glib/src/subclass/types.rs b/glib/src/subclass/types.rs
index d4547986ce4..18697626d8f 100644
--- a/glib/src/subclass/types.rs
+++ b/glib/src/subclass/types.rs
@@ -47,7 +47,10 @@ struct PrivateStruct<T: ObjectSubclass> {
 /// required in the instance struct.
 ///
 /// [`basic::InstanceStruct`]: ../basic/struct.InstanceStruct.html
-pub unsafe trait InstanceStruct: Sized + 'static {
+pub unsafe trait InstanceStruct: Sized + 'static
+where
+    Self: Copy,
+{
     // rustdoc-stripper-ignore-next
     /// Corresponding object subclass type for this instance struct.
     type Type: ObjectSubclass;
@@ -175,7 +178,10 @@ impl<T: ObjectSubclassIs<Subclass = S>, S: ObjectSubclass<Type = Self>> ObjectSu
 /// required in the class struct, e.g. for declaring new virtual methods.
 ///
 /// [`basic::ClassStruct`]: ../basic/struct.ClassStruct.html
-pub unsafe trait ClassStruct: Sized + 'static {
+pub unsafe trait ClassStruct: Sized + 'static
+where
+    Self: Copy,
+{
     // rustdoc-stripper-ignore-next
     /// Corresponding object subclass type for this class struct.
     type Type: ObjectSubclass;
sdroege commented 6 months ago

We should also consider not copying the interface struct ourselves here but simply taking the pointer of it that is already stored inside GObject. g_type_class_peek_parent() or so should be able to get us that.