From b267b1bb9ed4d047257d0c5d24230e7d7b721d37 Mon Sep 17 00:00:00 2001 From: Bilal Elmoussaoui Date: Tue, 2 May 2023 15:57:06 +0200 Subject: [PATCH 01/10] pango: Lower pkg-config version requirement for v1_52 Still not released APIs --- pango/sys/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pango/sys/Cargo.toml b/pango/sys/Cargo.toml index 7b4a3ec19703..ac38f72907d1 100644 --- a/pango/sys/Cargo.toml +++ b/pango/sys/Cargo.toml @@ -66,4 +66,4 @@ version = "1.48" version = "1.49.2" [package.metadata.system-deps.pango.v1_52] -version = "1.52" +version = "1.51" From 2575c1814b8731638342b633fc157bc3af7281e6 Mon Sep 17 00:00:00 2001 From: Bilal Elmoussaoui Date: Thu, 4 May 2023 10:46:01 +0200 Subject: [PATCH 02/10] gio: Add missing manual traits --- gio/Gir.toml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/gio/Gir.toml b/gio/Gir.toml index f4a52806a4dd..81350fb86cf8 100644 --- a/gio/Gir.toml +++ b/gio/Gir.toml @@ -665,6 +665,7 @@ manual_traits = ["DebugControllerDBusExtManual"] [[object]] name = "Gio.DesktopAppInfo" status = "generate" +manual_traits = ["DesktopAppInfoExtManual"] cfg_condition = "all(not(windows),not(target_os = \"macos\"))" [[object.function]] name = "get_boolean" @@ -845,6 +846,7 @@ concurrency = "send+sync" [[object]] name = "Gio.InetAddress" status = "generate" +manual_traits = ["InetAddressExtManual"] concurrency = "send+sync" [[object.function]] name = "new_from_bytes" @@ -1121,6 +1123,7 @@ status = "generate" [[object]] name = "Gio.Settings" status = "generate" +manual_traits = ["SettingsExtManual"] [[object.signal]] name = "writable-change-event" inhibit = true From 7a8d5adf2332c6f780250607fbf7dbf1123d9d49 Mon Sep 17 00:00:00 2001 From: Fina Wilke Date: Thu, 4 May 2023 00:05:09 +0200 Subject: [PATCH 03/10] glib-macros: Allow to omit set for construct_only properties --- glib-macros/src/properties.rs | 19 ++++++++++++++++--- glib-macros/tests/properties.rs | 26 +++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/glib-macros/src/properties.rs b/glib-macros/src/properties.rs index bc0666883565..ec8a215ed9b1 100644 --- a/glib-macros/src/properties.rs +++ b/glib-macros/src/properties.rs @@ -236,6 +236,7 @@ struct PropDesc { member: Option, builder: Option<(Punctuated, TokenStream2)>, builder_fields: HashMap>, + is_construct_only: bool, } impl PropDesc { @@ -248,7 +249,7 @@ impl PropDesc { let ReceivedAttrs { nullable, get, - set, + mut set, override_class, override_interface, ty, @@ -258,6 +259,17 @@ impl PropDesc { builder_fields, } = attrs; + let is_construct_only = if builder_fields.iter().any(|(k, _)| *k == "construct_only") { + // Insert a default setter automatically + if set.is_none() { + set = Some(MaybeCustomFn::Default); + } + + true + } else { + false + }; + if get.is_none() && set.is_none() { return Err(syn::Error::new( attrs_span, @@ -295,6 +307,7 @@ impl PropDesc { member, builder, builder_fields, + is_construct_only, }) } } @@ -534,8 +547,8 @@ fn expand_wrapper_getset_properties(props: &[PropDesc]) -> TokenStream2 { self.property::<<#ty as #crate_ident::Property>::Value>(#stripped_name) }) }); - let is_construct_only = p.builder_fields.iter().any(|(k, _)| *k == "construct_only"); - let setter = (p.set.is_some() && !is_construct_only).then(|| { + + let setter = (p.set.is_some() && !p.is_construct_only).then(|| { let ident = format_ident!("set_{}", ident); let target_ty = quote!(<<#ty as #crate_ident::Property>::Value as #crate_ident::HasParamSpec>::SetValue); let set_ty = if p.nullable { diff --git a/glib-macros/tests/properties.rs b/glib-macros/tests/properties.rs index 254962508cc8..5495846d2230 100644 --- a/glib-macros/tests/properties.rs +++ b/glib-macros/tests/properties.rs @@ -160,6 +160,10 @@ mod foo { weak_ref_prop: glib::WeakRef, #[property(get, set)] send_weak_ref_prop: glib::SendWeakRef, + #[property(get, default_value = 0, construct_only)] + construct_only_cell: OnceCell, + #[property(get, set = Self::set_construct_only_custom, construct_only)] + construct_only_custom_setter: OnceCell>, } impl ObjectImpl for Foo { @@ -194,6 +198,11 @@ mod foo { fn overridden(&self) -> u32 { 43 } + fn set_construct_only_custom(&self, value: Option) { + self.construct_only_custom_setter + .set(value.map(|v| format!("custom set: {}", v))) + .expect("Setter to be only called once"); + } } } @@ -382,7 +391,22 @@ fn props() { // object subclass let myobj = glib::BoxedAnyObject::new(""); myfoo.set_object(Some(myobj.upcast_ref())); - assert_eq!(myfoo.object(), Some(myobj.upcast())) + assert_eq!(myfoo.object(), Some(myobj.upcast())); + + // construct_only + let myfoo: foo::Foo = glib::object::Object::builder() + .property("construct-only-cell", 1u32) + .build(); + assert_eq!(myfoo.construct_only_cell(), 1u32); + + // construct_only with custom setter + let myfoo: foo::Foo = glib::object::Object::builder() + .property("construct-only-custom-setter", "foo") + .build(); + assert_eq!( + myfoo.construct_only_custom_setter(), + Some("custom set: foo".to_owned()) + ); } #[cfg(test)] From 00a170af001dd3558615282e55897f379fff6f7d Mon Sep 17 00:00:00 2001 From: Fina Wilke Date: Fri, 5 May 2023 16:56:14 +0200 Subject: [PATCH 04/10] glib-macros: Update docs for the properties macro construct_only attribute --- glib-macros/src/lib.rs | 7 ++++--- glib-macros/src/properties.rs | 15 +++++---------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/glib-macros/src/lib.rs b/glib-macros/src/lib.rs index 6c82c6ec3c6c..97ea2b124b33 100644 --- a/glib-macros/src/lib.rs +++ b/glib-macros/src/lib.rs @@ -851,7 +851,7 @@ pub fn cstr_bytes(item: TokenStream) -> TokenStream { /// This macro enables you to derive object properties in a quick way. /// -/// # Supported #[property] attributes +/// # Supported `#[property]` attributes /// | Attribute | Description | Default | Example | /// | --- | --- | --- | --- | /// | `name = "literal"` | The name of the property | field ident where `_` (leading and trailing `_` are trimmed) is replaced into `-` | `#[property(name = "prop-name")]` | @@ -862,7 +862,8 @@ pub fn cstr_bytes(item: TokenStream) -> TokenStream { /// | `override_interface = expr` | The type of interface of which to override the property from | | `#[property(override_interface = SomeInterface)]` | /// | `nullable` | Whether to use `Option` in the wrapper's generated setter | | `#[property(nullable)]` | /// | `member = ident` | Field of the nested type where property is retrieved and set | | `#[property(member = author)]` | -/// | `builder()[.ident]*` | Used to input required params or add optional Param Spec builder fields | | `#[property(builder(SomeEnum::default()))]`, `#[builder().default_value(1).construct_only()]`, etc. | +/// | `construct_only` | Specify that the property is construct only. This will not generate a public setter and only allow the property to be set during object construction. The use of a custom internal setter is supported. | | `#[property(get, construct_only)]` or `#[property(get, set = set_prop, construct_only)]` | +/// | `builder()[.ident]*` | Used to input required params or add optional Param Spec builder fields | | `#[property(builder(SomeEnum::default()))]`, `#[builder().default_value(1).minimum(0).maximum(5)]`, etc. | /// | `default` | Sets the `default_value` field of the Param Spec builder | | `#[property(default = 1)]` | /// | ` = expr` | Used to add optional Param Spec builder fields | | `#[property(minimum = 0)` , `#[property(minimum = 0, maximum = 1)]`, etc. | /// | `` | Used to add optional Param Spec builder fields | | `#[property(explicit_notify)]` , `#[property(construct_only)]`, etc. | @@ -883,7 +884,7 @@ pub fn cstr_bytes(item: TokenStream) -> TokenStream { /// /// # Internal getters and setters /// By default, they are generated for you. However, you can use a custom getter/setter -/// by assigning an expression to `get`/`set` #[property] attributes: `#[property(get = |_| 2, set)]` or `#[property(get, set = custom_setter_func)]`. +/// by assigning an expression to `get`/`set` `#[property]` attributes: `#[property(get = |_| 2, set)]` or `#[property(get, set = custom_setter_func)]`. /// /// # Supported types /// Every type implementing the trait `Property` is supported. diff --git a/glib-macros/src/properties.rs b/glib-macros/src/properties.rs index ec8a215ed9b1..fe6ee6f2df15 100644 --- a/glib-macros/src/properties.rs +++ b/glib-macros/src/properties.rs @@ -259,16 +259,11 @@ impl PropDesc { builder_fields, } = attrs; - let is_construct_only = if builder_fields.iter().any(|(k, _)| *k == "construct_only") { - // Insert a default setter automatically - if set.is_none() { - set = Some(MaybeCustomFn::Default); - } - - true - } else { - false - }; + let is_construct_only = builder_fields.iter().any(|(k, _)| *k == "construct_only"); + if is_construct_only && set.is_none() { + // Insert a default internal setter automatically + set = Some(MaybeCustomFn::Default); + } if get.is_none() && set.is_none() { return Err(syn::Error::new( From dc9ae5fdf6dc98b67bb67431cb48cc3649f19d71 Mon Sep 17 00:00:00 2001 From: Benji Smith <6193112+Benjins@users.noreply.github.com> Date: Sat, 20 May 2023 18:05:13 -0400 Subject: [PATCH 05/10] Upgrade plain-HTTP links to HTTPS in Cargo.toml files --- gdk-pixbuf/sys/Cargo.toml | 2 +- gio/sys/Cargo.toml | 2 +- glib/gobject-sys/Cargo.toml | 2 +- glib/sys/Cargo.toml | 2 +- pango/sys/Cargo.toml | 2 +- pangocairo/sys/Cargo.toml | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/gdk-pixbuf/sys/Cargo.toml b/gdk-pixbuf/sys/Cargo.toml index 4e528a74e743..e3a6995ffde4 100644 --- a/gdk-pixbuf/sys/Cargo.toml +++ b/gdk-pixbuf/sys/Cargo.toml @@ -35,7 +35,7 @@ name = "gdk_pixbuf_sys" authors = ["The gtk-rs Project Developers"] build = "build.rs" description = "FFI bindings to libgdk_pixbuf-2.0" -homepage = "http://gtk-rs.org/" +homepage = "https://gtk-rs.org/" keywords = ["gdk-pixbuf", "ffi", "gtk-rs", "gnome"] license = "MIT" name = "gdk-pixbuf-sys" diff --git a/gio/sys/Cargo.toml b/gio/sys/Cargo.toml index 6901e7ba5cd3..0f3e4abf3b2c 100644 --- a/gio/sys/Cargo.toml +++ b/gio/sys/Cargo.toml @@ -42,7 +42,7 @@ name = "gio_sys" authors = ["The gtk-rs Project Developers"] build = "build.rs" description = "FFI bindings to libgio-2.0" -homepage = "http://gtk-rs.org/" +homepage = "https://gtk-rs.org/" keywords = ["gio", "ffi", "gtk-rs", "gnome"] license = "MIT" name = "gio-sys" diff --git a/glib/gobject-sys/Cargo.toml b/glib/gobject-sys/Cargo.toml index a54220fd280a..673719a6013a 100644 --- a/glib/gobject-sys/Cargo.toml +++ b/glib/gobject-sys/Cargo.toml @@ -31,7 +31,7 @@ name = "gobject_sys" authors = ["The gtk-rs Project Developers"] build = "build.rs" description = "FFI bindings to libgobject-2.0" -homepage = "http://gtk-rs.org/" +homepage = "https://gtk-rs.org/" keywords = ["gobject", "ffi", "gtk-rs", "gnome"] license = "MIT" name = "gobject-sys" diff --git a/glib/sys/Cargo.toml b/glib/sys/Cargo.toml index 5d04c46e489d..686375717132 100644 --- a/glib/sys/Cargo.toml +++ b/glib/sys/Cargo.toml @@ -28,7 +28,7 @@ name = "glib_sys" authors = ["The gtk-rs Project Developers"] build = "build.rs" description = "FFI bindings to libglib-2.0" -homepage = "http://gtk-rs.org/" +homepage = "https://gtk-rs.org/" keywords = ["glib", "ffi", "gtk-rs", "gnome"] license = "MIT" name = "glib-sys" diff --git a/pango/sys/Cargo.toml b/pango/sys/Cargo.toml index ac38f72907d1..5fb834fe5764 100644 --- a/pango/sys/Cargo.toml +++ b/pango/sys/Cargo.toml @@ -34,7 +34,7 @@ name = "pango_sys" authors = ["The gtk-rs Project Developers"] build = "build.rs" description = "FFI bindings to libpango-1.0" -homepage = "http://gtk-rs.org/" +homepage = "https://gtk-rs.org/" keywords = ["pango", "ffi", "gtk-rs", "gnome"] license = "MIT" name = "pango-sys" diff --git a/pangocairo/sys/Cargo.toml b/pangocairo/sys/Cargo.toml index 417b7cdd55ea..92946d41dcf5 100644 --- a/pangocairo/sys/Cargo.toml +++ b/pangocairo/sys/Cargo.toml @@ -33,7 +33,7 @@ name = "pango_cairo_sys" authors = ["The gtk-rs Project Developers"] build = "build.rs" description = "FFI bindings to PangoCairo" -homepage = "http://gtk-rs.org/" +homepage = "https://gtk-rs.org/" keywords = ["gtk", "ffi", "gtk-rs", "gnome"] license = "MIT" name = "pangocairo-sys" From f54c9b261e01b2e250027c22e013d1974e3d7547 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Tue, 30 May 2023 10:07:49 +0300 Subject: [PATCH 06/10] Fix a couple of trivial clippy warnings --- cairo/src/svg.rs | 3 ++- glib/src/translate.rs | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/cairo/src/svg.rs b/cairo/src/svg.rs index d63accd96d9c..0662f2bdb4d4 100644 --- a/cairo/src/svg.rs +++ b/cairo/src/svg.rs @@ -146,10 +146,11 @@ mod test { *surface.finish_output_stream().unwrap().downcast().unwrap() } + #[track_caller] fn assert_len_close_enough(len_a: usize, len_b: usize) { // It seems cairo randomizes some element IDs which might make one svg slightly // larger than the other. Here we make sure the difference is within ~10%. - let len_diff = (len_a as isize - len_b as isize).abs() as usize; + let len_diff = usize::abs_diff(len_a, len_b); assert!(len_diff < len_b / 10); } diff --git a/glib/src/translate.rs b/glib/src/translate.rs index 760be8115a59..11cfec05f01b 100644 --- a/glib/src/translate.rs +++ b/glib/src/translate.rs @@ -958,7 +958,7 @@ macro_rules! impl_to_glib_container_from_slice_fundamental { } unsafe { - let res = ffi::g_malloc(mem::size_of::<$name>() * t.len()) as *mut $name; + let res = ffi::g_malloc(mem::size_of_val(t)) as *mut $name; ptr::copy_nonoverlapping(t.as_ptr(), res, t.len()); res } From 01a5517cc62109ee1de06223bd3d90e4aa0903f7 Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Tue, 30 May 2023 19:51:32 -0400 Subject: [PATCH 07/10] Fix heap buffer overflow due to operator precedence --- glib/src/collections/strv.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/glib/src/collections/strv.rs b/glib/src/collections/strv.rs index 35782e2f8d37..030733df9048 100644 --- a/glib/src/collections/strv.rs +++ b/glib/src/collections/strv.rs @@ -446,8 +446,10 @@ impl StrV { if len == 0 { StrV::default() } else { + // Allocate space for len + 1 pointers, one pointer for each string and a trailing + // null pointer. let new_ptr = - ffi::g_malloc(mem::size_of::<*mut c_char>() * len + 1) as *mut *mut c_char; + ffi::g_malloc(mem::size_of::<*mut c_char>() * (len + 1)) as *mut *mut c_char; // Need to clone every item because we don't own it here for i in 0..len { From 50600bff6ad417e631dc101d73df04bd2e318569 Mon Sep 17 00:00:00 2001 From: Maximiliano Sandoval R Date: Sat, 3 Jun 2023 09:15:27 +0200 Subject: [PATCH 08/10] strv: Implement From for constant GStr slices This was implemented for GString and &str but not for &GStr. --- glib/src/collections/strv.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/glib/src/collections/strv.rs b/glib/src/collections/strv.rs index 030733df9048..c8b410fe232e 100644 --- a/glib/src/collections/strv.rs +++ b/glib/src/collections/strv.rs @@ -358,6 +358,21 @@ impl<'a, const N: usize> From<[&'a str; N]> for StrV { } } +impl<'a, const N: usize> From<[&'a GStr; N]> for StrV { + #[inline] + fn from(value: [&'a GStr; N]) -> Self { + unsafe { + let mut s = Self::with_capacity(value.len()); + for (i, item) in value.iter().enumerate() { + *s.ptr.as_ptr().add(i) = GString::from(*item).into_glib_ptr(); + } + s.len = value.len(); + *s.ptr.as_ptr().add(s.len) = ptr::null_mut(); + s + } + } +} + impl<'a> From<&'a [&'a str]> for StrV { #[inline] fn from(value: &'a [&'a str]) -> Self { @@ -1398,6 +1413,20 @@ mod test { } } + #[test] + fn test_from_slice() { + let items = [ + crate::gstr!("str1"), + crate::gstr!("str2"), + crate::gstr!("str3"), + ]; + + let slice1 = StrV::from(&items[..]); + let slice2 = StrV::from(items); + assert_eq!(slice1.len(), 3); + assert_eq!(slice1, slice2); + } + #[test] fn test_safe_api() { let items = [ From 01f019795fb5438cd74729c2f01948afff2572c3 Mon Sep 17 00:00:00 2001 From: "Jan Alexander Steffens (heftig)" Date: Mon, 5 Jun 2023 14:56:27 +0200 Subject: [PATCH 09/10] glib: Do not use ptr::offset/offset_from for private/impl offset MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Doing this violates Rust's [strict provenance rules][1]. Unfortunately, the API for doing this properly ([`ptr::expose_addr`][2]) isn't ready yet. However, we can work around this on stable Rust by casting through `isize` for offset calculations. [1]: https://doc.rust-lang.org/std/ptr/index.html [2]: https://doc.rust-lang.org/std/primitive.pointer.html#method.expose_addr Co-authored-by: Sebastian Dröge --- glib/src/subclass/types.rs | 97 ++++++++++++++++++++++++++------------ 1 file changed, 66 insertions(+), 31 deletions(-) diff --git a/glib/src/subclass/types.rs b/glib/src/subclass/types.rs index 802bf2c3971a..7e1cdd566e88 100644 --- a/glib/src/subclass/types.rs +++ b/glib/src/subclass/types.rs @@ -88,16 +88,57 @@ pub unsafe trait InstanceStructExt: InstanceStruct { fn class(&self) -> &::Class; } +// rustdoc-stripper-ignore-next +/// Offset `ptr` by `offset` *bytes* and cast the result to `*const U`. +/// +/// The result must be a correctly aligned pointer to a valid value of type `U`. +/// +/// # Panics: +/// +/// This function panics if debug assertions are enabled if adding `offset` causes `ptr` to +/// overflow or if the resulting pointer is not correctly aligned. +#[inline] +fn offset_ptr_by_bytes(ptr: *const T, offset: isize) -> *const U { + // FIXME: Use `ptr::expose_addr()` once stable + let ptr = ptr as usize; + let ptr = if offset < 0 { + ptr - (-offset) as usize + } else { + ptr + offset as usize + }; + debug_assert_eq!(ptr & (mem::align_of::() - 1), 0); + ptr as *const U +} + +// rustdoc-stripper-ignore-next +/// Offset `ptr` by `offset` *bytes* and cast the result to `*mut U`. +/// +/// The result must be a correctly aligned pointer to a valid value of type `U`. +/// +/// # Panics: +/// +/// This function panics if debug assertions are enabled if adding `offset` causes `ptr` to +/// overflow or if the resulting pointer is not correctly aligned. +#[inline] +fn offset_ptr_by_bytes_mut(ptr: *mut T, offset: isize) -> *mut U { + // FIXME: Use `ptr::expose_addr()` once stable + let ptr = ptr as usize; + let ptr = if offset < 0 { + ptr - (-offset) as usize + } else { + ptr + offset as usize + }; + debug_assert_eq!(ptr & (mem::align_of::() - 1), 0); + ptr as *mut U +} + unsafe impl InstanceStructExt for T { #[inline] fn imp(&self) -> &Self::Type { unsafe { let data = Self::Type::type_data(); let private_offset = data.as_ref().impl_offset(); - let ptr: *const u8 = self as *const _ as *const u8; - let imp_ptr = ptr.offset(private_offset); - let imp = imp_ptr as *const Self::Type; - + let imp = offset_ptr_by_bytes::(self, private_offset); &*imp } } @@ -717,17 +758,15 @@ impl ObjectSubclassExt for T { debug_assert!(type_.is_valid()); let offset = -data.as_ref().impl_offset(); - - let ptr = self as *const Self as *const u8; - let ptr = ptr.offset(offset); - let ptr = ptr as *mut u8 as *mut ::GlibType; + let ptr = + offset_ptr_by_bytes::::GlibType>(self, offset); // The object might just be finalized, and in that case it's unsafe to access // it and use any API on it. This can only happen from inside the Drop impl // of Self. - debug_assert_ne!((*(ptr as *mut gobject_ffi::GObject)).ref_count, 0); + debug_assert_ne!((*(ptr as *const gobject_ffi::GObject)).ref_count, 0); - crate::BorrowedObject::new(ptr) + crate::BorrowedObject::new(mut_override(ptr)) } } @@ -752,10 +791,7 @@ impl ObjectSubclassExt for T { debug_assert!(self_type_.is_valid()); let offset = -type_data.as_ref().private_imp_offset; - - let ptr = self as *const Self as *const u8; - let ptr = ptr.offset(offset); - let ptr = ptr as *const PrivateStruct; + let ptr = offset_ptr_by_bytes::>(self, offset); let priv_ = &*ptr; match priv_.instance_data { @@ -821,9 +857,10 @@ impl InitializingObject { let offset = type_data.as_ref().private_offset; - let ptr = self.0.as_ptr() as *mut u8; - let ptr = ptr.offset(offset); - let ptr = ptr as *mut PrivateStruct; + let ptr = offset_ptr_by_bytes_mut::< + <::Type as ObjectType>::GlibType, + PrivateStruct, + >(self.0.as_ptr(), offset); let priv_ = &mut *ptr; if priv_.instance_data.is_none() { @@ -885,8 +922,10 @@ unsafe extern "C" fn instance_init( // and actually store it in that place. let mut data = T::type_data(); let private_offset = data.as_mut().private_offset; - let ptr = obj as *mut u8; - let priv_ptr = ptr.offset(private_offset); + let priv_ptr = offset_ptr_by_bytes_mut::>( + obj, + private_offset, + ); assert!( priv_ptr as usize & (mem::align_of::>() - 1) == 0, @@ -897,13 +936,11 @@ unsafe extern "C" fn instance_init( 2 * mem::size_of::(), ); - let priv_storage = priv_ptr as *mut PrivateStruct; - let klass = &*(klass as *const T::Class); let imp = T::with_class(klass); ptr::write( - priv_storage, + priv_ptr, PrivateStruct { imp, instance_data: None, @@ -925,11 +962,10 @@ unsafe extern "C" fn finalize(obj: *mut gobject_ffi::GObject) // Retrieve the private struct and drop it for freeing all associated memory. let mut data = T::type_data(); let private_offset = data.as_mut().private_offset; - let ptr = obj as *mut u8; - let priv_ptr = ptr.offset(private_offset); - let priv_storage = &mut *(priv_ptr as *mut PrivateStruct); - ptr::drop_in_place(&mut priv_storage.imp); - ptr::drop_in_place(&mut priv_storage.instance_data); + let priv_ptr = + offset_ptr_by_bytes_mut::>(obj, private_offset); + ptr::drop_in_place(ptr::addr_of_mut!((*priv_ptr).imp)); + ptr::drop_in_place(ptr::addr_of_mut!((*priv_ptr).instance_data)); // Chain up to the parent class' finalize implementation, if any. let parent_class = &*(data.as_ref().parent_class() as *const gobject_ffi::GObjectClass); @@ -996,11 +1032,10 @@ pub fn register_type() -> Type { // some hoops because Rust doesn't have an offsetof operator yet. data.as_mut().private_imp_offset = { // Must not be a dangling pointer so let's create some uninitialized memory - let priv_ = std::mem::MaybeUninit::>::uninit(); + let priv_ = mem::MaybeUninit::>::uninit(); let ptr = priv_.as_ptr(); - let imp_ptr = std::ptr::addr_of!((*ptr).imp) as *const u8; - let ptr = ptr as *const u8; - imp_ptr.offset_from(ptr) + let imp_ptr = ptr::addr_of!((*ptr).imp); + (imp_ptr as isize) - (ptr as isize) }; let iface_types = T::Interfaces::iface_infos(); From 840a2eb094df6286c8ff16e2833c8dc3f68cf21a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Tue, 6 Jun 2023 13:46:56 +0300 Subject: [PATCH 10/10] Fix/silence a few more clippy warnings --- glib/src/collections/list.rs | 3 ++- glib/src/collections/slist.rs | 3 ++- glib/src/main_context_futures.rs | 3 +-- glib/src/variant.rs | 2 +- pangocairo/src/prelude.rs | 2 ++ 5 files changed, 8 insertions(+), 5 deletions(-) diff --git a/glib/src/collections/list.rs b/glib/src/collections/list.rs index aa6a24ed1183..71bf92b3e933 100644 --- a/glib/src/collections/list.rs +++ b/glib/src/collections/list.rs @@ -385,7 +385,8 @@ impl List { /// Consumes the list and returns the underlying pointer. #[inline] pub fn into_raw(mut self) -> *mut ffi::GList { - mem::replace(&mut self.ptr, None) + self.ptr + .take() .map(|p| p.as_ptr()) .unwrap_or(ptr::null_mut()) } diff --git a/glib/src/collections/slist.rs b/glib/src/collections/slist.rs index 2aa03f97b07b..70f013b202cd 100644 --- a/glib/src/collections/slist.rs +++ b/glib/src/collections/slist.rs @@ -380,7 +380,8 @@ impl SList { /// Consumes the list and returns the underlying pointer. #[inline] pub fn into_raw(mut self) -> *mut ffi::GSList { - mem::replace(&mut self.ptr, None) + self.ptr + .take() .map(|p| p.as_ptr()) .unwrap_or(ptr::null_mut()) } diff --git a/glib/src/main_context_futures.rs b/glib/src/main_context_futures.rs index a86f89d145bc..5e0f86e5ff87 100644 --- a/glib/src/main_context_futures.rs +++ b/glib/src/main_context_futures.rs @@ -620,11 +620,10 @@ impl MainContext { pub fn block_on(&self, f: F) -> F::Output { let mut res = None; let l = MainLoop::new(Some(self), false); - let l_clone = l.clone(); let f = async { res = Some(panic::AssertUnwindSafe(f).catch_unwind().await); - l_clone.quit(); + l.quit(); }; let f = unsafe { diff --git a/glib/src/variant.rs b/glib/src/variant.rs index 59163e810d1b..7793e804d23b 100644 --- a/glib/src/variant.rs +++ b/glib/src/variant.rs @@ -1958,7 +1958,7 @@ impl, T: FixedSizeVariantType> From> f let data = Box::new(a.0); let (data_ptr, len) = { let data = (*data).as_ref(); - (data.as_ptr(), data.len() * mem::size_of::()) + (data.as_ptr(), mem::size_of_val(data)) }; unsafe extern "C" fn free_data, T: FixedSizeVariantType>( diff --git a/pangocairo/src/prelude.rs b/pangocairo/src/prelude.rs index 3992832392b4..80249c607629 100644 --- a/pangocairo/src/prelude.rs +++ b/pangocairo/src/prelude.rs @@ -1,5 +1,7 @@ // Take a look at the license at the top of the repository in the LICENSE file. +#![allow(ambiguous_glob_reexports)] + #[doc(hidden)] pub use glib::prelude::*; #[doc(hidden)]