From b307d299fd0b90fae2d7e28217be2c299b5e1902 Mon Sep 17 00:00:00 2001 From: BiagioFesta <15035284+BiagioFesta@users.noreply.github.com> Date: Mon, 24 Feb 2025 22:54:48 +0100 Subject: [PATCH] Remove unsafe interface requirement for into ptr conversion --- cairo/src/context.rs | 2 +- cairo/src/device.rs | 2 +- cairo/src/region.rs | 2 +- cairo/src/surface.rs | 2 +- cairo/src/surface_macros.rs | 2 +- glib-macros/src/boxed_derive.rs | 2 +- glib-macros/src/shared_boxed_derive.rs | 2 +- glib/src/boxed.rs | 4 ++-- glib/src/boxed_inline.rs | 4 ++-- glib/src/collections/list.rs | 2 +- glib/src/collections/ptr_slice.rs | 2 +- glib/src/collections/slice.rs | 2 +- glib/src/collections/slist.rs | 2 +- glib/src/collections/strv.rs | 2 +- glib/src/gstring.rs | 18 +++++++++--------- glib/src/match_info.rs | 4 ++-- glib/src/object.rs | 4 ++-- glib/src/param_spec.rs | 4 ++-- glib/src/shared.rs | 4 ++-- glib/src/subclass/boxed.rs | 2 +- glib/src/subclass/shared.rs | 6 +++--- glib/src/translate.rs | 4 ++-- glib/src/variant_type.rs | 2 +- 23 files changed, 40 insertions(+), 40 deletions(-) diff --git a/cairo/src/context.rs b/cairo/src/context.rs index 0400386f8a9d..6293cd1f5aea 100644 --- a/cairo/src/context.rs +++ b/cairo/src/context.rs @@ -60,7 +60,7 @@ pub struct Context(ptr::NonNull); #[cfg_attr(docsrs, doc(cfg(feature = "use_glib")))] impl IntoGlibPtr<*mut ffi::cairo_t> for Context { #[inline] - unsafe fn into_glib_ptr(self) -> *mut ffi::cairo_t { + fn into_glib_ptr(self) -> *mut ffi::cairo_t { (&*std::mem::ManuallyDrop::new(self)).to_glib_none().0 } } diff --git a/cairo/src/device.rs b/cairo/src/device.rs index c35069396561..31436deeab9d 100644 --- a/cairo/src/device.rs +++ b/cairo/src/device.rs @@ -317,7 +317,7 @@ impl Device { #[cfg_attr(docsrs, doc(cfg(feature = "use_glib")))] impl IntoGlibPtr<*mut ffi::cairo_device_t> for Device { #[inline] - unsafe fn into_glib_ptr(self) -> *mut ffi::cairo_device_t { + fn into_glib_ptr(self) -> *mut ffi::cairo_device_t { std::mem::ManuallyDrop::new(self).to_glib_none().0 } } diff --git a/cairo/src/region.rs b/cairo/src/region.rs index bfcced686660..930a5983dc9e 100644 --- a/cairo/src/region.rs +++ b/cairo/src/region.rs @@ -17,7 +17,7 @@ pub struct Region(ptr::NonNull); #[cfg(feature = "use_glib")] impl IntoGlibPtr<*mut ffi::cairo_region_t> for Region { #[inline] - unsafe fn into_glib_ptr(self) -> *mut ffi::cairo_region_t { + fn into_glib_ptr(self) -> *mut ffi::cairo_region_t { (&*std::mem::ManuallyDrop::new(self)).to_glib_none().0 } } diff --git a/cairo/src/surface.rs b/cairo/src/surface.rs index 6407c6731dba..8ea7034913e7 100644 --- a/cairo/src/surface.rs +++ b/cairo/src/surface.rs @@ -292,7 +292,7 @@ impl Surface { #[cfg(feature = "use_glib")] impl IntoGlibPtr<*mut ffi::cairo_surface_t> for Surface { #[inline] - unsafe fn into_glib_ptr(self) -> *mut ffi::cairo_surface_t { + fn into_glib_ptr(self) -> *mut ffi::cairo_surface_t { std::mem::ManuallyDrop::new(self).to_glib_none().0 } } diff --git a/cairo/src/surface_macros.rs b/cairo/src/surface_macros.rs index 3236c4b86046..f21a00aa1d7d 100644 --- a/cairo/src/surface_macros.rs +++ b/cairo/src/surface_macros.rs @@ -41,7 +41,7 @@ macro_rules! declare_surface { #[cfg(feature = "use_glib")] impl IntoGlibPtr<*mut crate::ffi::cairo_surface_t> for $surf_name { #[inline] - unsafe fn into_glib_ptr(self) -> *mut crate::ffi::cairo_surface_t { + fn into_glib_ptr(self) -> *mut crate::ffi::cairo_surface_t { std::mem::ManuallyDrop::new(self).to_glib_none().0 } } diff --git a/glib-macros/src/boxed_derive.rs b/glib-macros/src/boxed_derive.rs index c6e254b43582..ac5822019972 100644 --- a/glib-macros/src/boxed_derive.rs +++ b/glib-macros/src/boxed_derive.rs @@ -241,7 +241,7 @@ pub fn impl_boxed(input: &syn::DeriveInput) -> syn::Result { impl #crate_ident::translate::IntoGlibPtr<*mut #name> for #name { #[inline] - unsafe fn into_glib_ptr(self) -> *mut #name { + fn into_glib_ptr(self) -> *mut #name { ::std::boxed::Box::into_raw(::std::boxed::Box::new(self)) as *mut _ } } diff --git a/glib-macros/src/shared_boxed_derive.rs b/glib-macros/src/shared_boxed_derive.rs index dc56813a1caf..c724cf713b6a 100644 --- a/glib-macros/src/shared_boxed_derive.rs +++ b/glib-macros/src/shared_boxed_derive.rs @@ -262,7 +262,7 @@ pub fn impl_shared_boxed(input: &syn::DeriveInput) -> syn::Result for #name { #[inline] - unsafe fn into_glib_ptr(self) -> *mut #refcounted_type_prefix::InnerType { + fn into_glib_ptr(self) -> *mut #refcounted_type_prefix::InnerType { let r = ::into_refcounted(self); #refcounted_type_prefix::into_raw(r) as *mut _ } diff --git a/glib/src/boxed.rs b/glib/src/boxed.rs index d44d7778c5de..8eb5fe61bbe9 100644 --- a/glib/src/boxed.rs +++ b/glib/src/boxed.rs @@ -306,7 +306,7 @@ macro_rules! glib_boxed_wrapper { #[doc(hidden)] impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::IntoGlibPtr<*mut $ffi_name> for $name $(<$($generic),+>)? { #[inline] - unsafe fn into_glib_ptr(self) -> *mut $ffi_name { + fn into_glib_ptr(self) -> *mut $ffi_name { let s = std::mem::ManuallyDrop::new(self); $crate::translate::ToGlibPtr::<*const $ffi_name>::to_glib_none(&*s).0 as *mut _ } @@ -315,7 +315,7 @@ macro_rules! glib_boxed_wrapper { #[doc(hidden)] impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::IntoGlibPtr<*const $ffi_name> for $name $(<$($generic),+>)? { #[inline] - unsafe fn into_glib_ptr(self) -> *const $ffi_name { + fn into_glib_ptr(self) -> *const $ffi_name { let s = std::mem::ManuallyDrop::new(self); $crate::translate::ToGlibPtr::<*const $ffi_name>::to_glib_none(&*s).0 as *const _ } diff --git a/glib/src/boxed_inline.rs b/glib/src/boxed_inline.rs index fe610c79acf9..55c7d0b0abaa 100644 --- a/glib/src/boxed_inline.rs +++ b/glib/src/boxed_inline.rs @@ -538,7 +538,7 @@ macro_rules! glib_boxed_inline_wrapper { #[doc(hidden)] impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::IntoGlibPtr<*mut $ffi_name> for $name $(<$($generic),+>)? { #[inline] - unsafe fn into_glib_ptr(self) -> *mut $ffi_name { + fn into_glib_ptr(self) -> *mut $ffi_name { $crate::translate::ToGlibPtr::<*const $ffi_name>::to_glib_full(&self) as *mut _ } } @@ -546,7 +546,7 @@ macro_rules! glib_boxed_inline_wrapper { #[doc(hidden)] impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::IntoGlibPtr<*const $ffi_name> for $name $(<$($generic),+>)? { #[inline] - unsafe fn into_glib_ptr(self) -> *const $ffi_name { + fn into_glib_ptr(self) -> *const $ffi_name { $crate::translate::ToGlibPtr::<*const $ffi_name>::to_glib_full(&self) } } diff --git a/glib/src/collections/list.rs b/glib/src/collections/list.rs index c2c8096d72fa..b5e3cffbdab7 100644 --- a/glib/src/collections/list.rs +++ b/glib/src/collections/list.rs @@ -621,7 +621,7 @@ impl<'a, T: TransparentPtrType + 'a> ToGlibPtrMut<'a, *mut ffi::GList> for List< impl IntoGlibPtr<*mut ffi::GList> for List { #[inline] - unsafe fn into_glib_ptr(self) -> *mut ffi::GList { + fn into_glib_ptr(self) -> *mut ffi::GList { self.into_raw() } } diff --git a/glib/src/collections/ptr_slice.rs b/glib/src/collections/ptr_slice.rs index 62d2653e34c3..ce5c76cb9a65 100644 --- a/glib/src/collections/ptr_slice.rs +++ b/glib/src/collections/ptr_slice.rs @@ -1050,7 +1050,7 @@ impl<'a, T: TransparentPtrType + 'a> ToGlibPtrMut<'a, *mut impl IntoGlibPtr<*mut ::GlibType> for PtrSlice { #[inline] - unsafe fn into_glib_ptr(self) -> *mut ::GlibType { + fn into_glib_ptr(self) -> *mut ::GlibType { self.into_raw() } } diff --git a/glib/src/collections/slice.rs b/glib/src/collections/slice.rs index eb674c342480..6b7ad394cc05 100644 --- a/glib/src/collections/slice.rs +++ b/glib/src/collections/slice.rs @@ -874,7 +874,7 @@ impl<'a, T: TransparentType + 'a> ToGlibPtrMut<'a, *mut T::GlibType> for Slice IntoGlibPtr<*mut T::GlibType> for Slice { #[inline] - unsafe fn into_glib_ptr(self) -> *mut T::GlibType { + fn into_glib_ptr(self) -> *mut T::GlibType { self.into_raw() } } diff --git a/glib/src/collections/slist.rs b/glib/src/collections/slist.rs index fce48dd52645..70188ba1d71a 100644 --- a/glib/src/collections/slist.rs +++ b/glib/src/collections/slist.rs @@ -615,7 +615,7 @@ impl<'a, T: TransparentPtrType + 'a> ToGlibPtrMut<'a, *mut ffi::GSList> for SLis impl IntoGlibPtr<*mut ffi::GSList> for SList { #[inline] - unsafe fn into_glib_ptr(self) -> *mut ffi::GSList { + fn into_glib_ptr(self) -> *mut ffi::GSList { self.into_raw() } } diff --git a/glib/src/collections/strv.rs b/glib/src/collections/strv.rs index 05ec5821c373..6852256908d4 100644 --- a/glib/src/collections/strv.rs +++ b/glib/src/collections/strv.rs @@ -1010,7 +1010,7 @@ impl<'a> ToGlibPtr<'a, *const *mut c_char> for StrV { impl IntoGlibPtr<*mut *mut c_char> for StrV { #[inline] - unsafe fn into_glib_ptr(self) -> *mut *mut c_char { + fn into_glib_ptr(self) -> *mut *mut c_char { self.into_raw() } } diff --git a/glib/src/gstring.rs b/glib/src/gstring.rs index 0ef5674ce25b..29e2b16d7d8c 100644 --- a/glib/src/gstring.rs +++ b/glib/src/gstring.rs @@ -775,7 +775,7 @@ impl Deref for GStringPtr { impl IntoGlibPtr<*mut c_char> for GStringPtr { #[inline] - unsafe fn into_glib_ptr(self) -> *mut c_char { + fn into_glib_ptr(self) -> *mut c_char { self.0.as_ptr() } } @@ -1518,16 +1518,16 @@ impl IntoGlibPtr<*mut c_char> for GString { // rustdoc-stripper-ignore-next /// Transform into a nul-terminated raw C string pointer. #[inline] - unsafe fn into_glib_ptr(self) -> *mut c_char { + fn into_glib_ptr(self) -> *mut c_char { match self.0 { - Inner::Native(ref s) => ffi::g_strndup(s.as_ptr() as *const _, s.len()), + Inner::Native(ref s) => unsafe { ffi::g_strndup(s.as_ptr() as *const _, s.len()) }, Inner::Foreign { ptr, .. } => { let _s = mem::ManuallyDrop::new(self); ptr.as_ptr() } - Inner::Inline { len, ref data } => { + Inner::Inline { len, ref data } => unsafe { ffi::g_strndup(data.as_ptr() as *const _, len as usize) - } + }, } } } @@ -2127,7 +2127,7 @@ impl<'a> ToGlibPtr<'a, *const u8> for GString { #[inline] fn to_glib_full(&self) -> *const u8 { - unsafe { self.clone().into_glib_ptr() as *const u8 } + self.clone().into_glib_ptr() as *const u8 } } @@ -2143,7 +2143,7 @@ impl<'a> ToGlibPtr<'a, *const i8> for GString { #[inline] fn to_glib_full(&self) -> *const i8 { - unsafe { self.clone().into_glib_ptr() as *const i8 } + self.clone().into_glib_ptr() as *const i8 } } @@ -2159,7 +2159,7 @@ impl<'a> ToGlibPtr<'a, *mut u8> for GString { #[inline] fn to_glib_full(&self) -> *mut u8 { - unsafe { self.clone().into_glib_ptr() as *mut u8 } + self.clone().into_glib_ptr() as *mut u8 } } @@ -2175,7 +2175,7 @@ impl<'a> ToGlibPtr<'a, *mut i8> for GString { #[inline] fn to_glib_full(&self) -> *mut i8 { - unsafe { self.clone().into_glib_ptr() as *mut i8 } + self.clone().into_glib_ptr() as *mut i8 } } diff --git a/glib/src/match_info.rs b/glib/src/match_info.rs index 488c7a214472..69f24ca1f1c0 100644 --- a/glib/src/match_info.rs +++ b/glib/src/match_info.rs @@ -152,7 +152,7 @@ impl FromGlibPtrBorrow<*const ffi::GMatchInfo> for MatchInfo<'_> { #[doc(hidden)] impl IntoGlibPtr<*mut ffi::GMatchInfo> for MatchInfo<'_> { #[inline] - unsafe fn into_glib_ptr(self) -> *mut ffi::GMatchInfo { + fn into_glib_ptr(self) -> *mut ffi::GMatchInfo { let s = std::mem::ManuallyDrop::new(self); ToGlibPtr::<*const ffi::GMatchInfo>::to_glib_none(&*s).0 as *mut _ } @@ -160,7 +160,7 @@ impl IntoGlibPtr<*mut ffi::GMatchInfo> for MatchInfo<'_> { #[doc(hidden)] impl IntoGlibPtr<*const ffi::GMatchInfo> for MatchInfo<'_> { #[inline] - unsafe fn into_glib_ptr(self) -> *const ffi::GMatchInfo { + fn into_glib_ptr(self) -> *const ffi::GMatchInfo { let s = std::mem::ManuallyDrop::new(self); ToGlibPtr::<*const ffi::GMatchInfo>::to_glib_none(&*s).0 as *const _ } diff --git a/glib/src/object.rs b/glib/src/object.rs index e694da21f864..77b8b616d16e 100644 --- a/glib/src/object.rs +++ b/glib/src/object.rs @@ -813,7 +813,7 @@ macro_rules! glib_object_wrapper { #[doc(hidden)] impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::IntoGlibPtr<*mut $ffi_name> for $name $(<$($generic),+>)? { #[inline] - unsafe fn into_glib_ptr(self) -> *mut $ffi_name { + fn into_glib_ptr(self) -> *mut $ffi_name { let s = std::mem::ManuallyDrop::new(self); $crate::translate::ToGlibPtr::<*const $ffi_name>::to_glib_none(&*s).0 as *mut _ } @@ -822,7 +822,7 @@ macro_rules! glib_object_wrapper { #[doc(hidden)] impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::IntoGlibPtr<*const $ffi_name> for $name $(<$($generic),+>)? { #[inline] - unsafe fn into_glib_ptr(self) -> *const $ffi_name { + fn into_glib_ptr(self) -> *const $ffi_name { let s = std::mem::ManuallyDrop::new(self); $crate::translate::ToGlibPtr::<*const $ffi_name>::to_glib_none(&*s).0 as *const _ } diff --git a/glib/src/param_spec.rs b/glib/src/param_spec.rs index ec6628e970c4..1b6356498a7f 100644 --- a/glib/src/param_spec.rs +++ b/glib/src/param_spec.rs @@ -434,7 +434,7 @@ macro_rules! define_param_spec { #[doc(hidden)] impl IntoGlibPtr<*mut gobject_ffi::GParamSpec> for $rust_type { #[inline] - unsafe fn into_glib_ptr(self) -> *mut gobject_ffi::GParamSpec { + fn into_glib_ptr(self) -> *mut gobject_ffi::GParamSpec { let s = std::mem::ManuallyDrop::new(self); s.to_glib_none().0 } @@ -443,7 +443,7 @@ macro_rules! define_param_spec { #[doc(hidden)] impl IntoGlibPtr<*const gobject_ffi::GParamSpec> for $rust_type { #[inline] - unsafe fn into_glib_ptr(self) -> *const gobject_ffi::GParamSpec { + fn into_glib_ptr(self) -> *const gobject_ffi::GParamSpec { let s = std::mem::ManuallyDrop::new(self); s.to_glib_none().0 } diff --git a/glib/src/shared.rs b/glib/src/shared.rs index 95754fed8d88..4282780fb284 100644 --- a/glib/src/shared.rs +++ b/glib/src/shared.rs @@ -323,7 +323,7 @@ macro_rules! glib_shared_wrapper { #[doc(hidden)] impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::IntoGlibPtr<*mut $ffi_name> for $name $(<$($generic),+>)? { #[inline] - unsafe fn into_glib_ptr(self) -> *mut $ffi_name { + fn into_glib_ptr(self) -> *mut $ffi_name { let s = std::mem::ManuallyDrop::new(self); $crate::translate::ToGlibPtr::<*const $ffi_name>::to_glib_none(&*s).0 as *mut _ } @@ -332,7 +332,7 @@ macro_rules! glib_shared_wrapper { #[doc(hidden)] impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::IntoGlibPtr<*const $ffi_name> for $name $(<$($generic),+>)? { #[inline] - unsafe fn into_glib_ptr(self) -> *const $ffi_name { + fn into_glib_ptr(self) -> *const $ffi_name { let s = std::mem::ManuallyDrop::new(self); $crate::translate::ToGlibPtr::<*const $ffi_name>::to_glib_none(&*s).0 as *const _ } diff --git a/glib/src/subclass/boxed.rs b/glib/src/subclass/boxed.rs index 6bb300f17df2..5df6745b8d06 100644 --- a/glib/src/subclass/boxed.rs +++ b/glib/src/subclass/boxed.rs @@ -131,7 +131,7 @@ mod test { assert!(MyBoxed::static_type().is_valid()); let b = MyBoxed(String::from("abc")); - let raw_ptr = unsafe { MyBoxed::into_glib_ptr(b) }; + let raw_ptr = MyBoxed::into_glib_ptr(b); // test that the from_glib_borrow does not take ownership of the raw_ptr let _ = unsafe { MyBoxed::from_glib_borrow(raw_ptr) }; diff --git a/glib/src/subclass/shared.rs b/glib/src/subclass/shared.rs index 4cf2fcc61a80..8565cf189a3b 100644 --- a/glib/src/subclass/shared.rs +++ b/glib/src/subclass/shared.rs @@ -20,7 +20,7 @@ pub unsafe trait RefCounted: Clone + Sized + 'static { // rustdoc-stripper-ignore-next /// Converts the RefCounted object to a raw pointer to InnerType - unsafe fn into_raw(self) -> *const Self::InnerType; + fn into_raw(self) -> *const Self::InnerType; // rustdoc-stripper-ignore-next /// Converts a raw pointer to InnerType to a RefCounted object @@ -45,7 +45,7 @@ where } #[inline] - unsafe fn into_raw(self) -> *const Self::InnerType { + fn into_raw(self) -> *const Self::InnerType { std::sync::Arc::into_raw(self) } @@ -74,7 +74,7 @@ where } #[inline] - unsafe fn into_raw(self) -> *const Self::InnerType { + fn into_raw(self) -> *const Self::InnerType { std::rc::Rc::into_raw(self) } diff --git a/glib/src/translate.rs b/glib/src/translate.rs index a738d3f1a68c..60a446b1c330 100644 --- a/glib/src/translate.rs +++ b/glib/src/translate.rs @@ -716,12 +716,12 @@ pub trait IntoGlibPtr { // rustdoc-stripper-ignore-next /// Transfer: full. #[allow(clippy::wrong_self_convention)] - unsafe fn into_glib_ptr(self) -> P; + fn into_glib_ptr(self) -> P; } impl> IntoGlibPtr

for Option { #[inline] - unsafe fn into_glib_ptr(self) -> P { + fn into_glib_ptr(self) -> P { self.map_or(Ptr::from::<()>(ptr::null_mut()), |s| { IntoGlibPtr::into_glib_ptr(s) }) diff --git a/glib/src/variant_type.rs b/glib/src/variant_type.rs index ee477896bc22..00c2c96e9382 100644 --- a/glib/src/variant_type.rs +++ b/glib/src/variant_type.rs @@ -186,7 +186,7 @@ impl<'a> From for Cow<'a, VariantTy> { #[doc(hidden)] impl IntoGlibPtr<*mut ffi::GVariantType> for VariantType { #[inline] - unsafe fn into_glib_ptr(self) -> *mut ffi::GVariantType { + fn into_glib_ptr(self) -> *mut ffi::GVariantType { std::mem::ManuallyDrop::new(self).to_glib_none().0 } }