Skip to content

Commit

Permalink
Merge pull request #1666 from BiagioFesta/wip/bfesta/remove-unsafe-to…
Browse files Browse the repository at this point in the history
…-ptr

Remove unsafe interface requirement for into ptr conversion
  • Loading branch information
sdroege authored Feb 25, 2025
2 parents c10cbc1 + b307d29 commit 2967438
Show file tree
Hide file tree
Showing 23 changed files with 40 additions and 40 deletions.
2 changes: 1 addition & 1 deletion cairo/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub struct Context(ptr::NonNull<ffi::cairo_t>);
#[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
}
}
Expand Down
2 changes: 1 addition & 1 deletion cairo/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
2 changes: 1 addition & 1 deletion cairo/src/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct Region(ptr::NonNull<ffi::cairo_region_t>);
#[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
}
}
Expand Down
2 changes: 1 addition & 1 deletion cairo/src/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
2 changes: 1 addition & 1 deletion cairo/src/surface_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
2 changes: 1 addition & 1 deletion glib-macros/src/boxed_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ pub fn impl_boxed(input: &syn::DeriveInput) -> syn::Result<TokenStream> {

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 _
}
}
Expand Down
2 changes: 1 addition & 1 deletion glib-macros/src/shared_boxed_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ pub fn impl_shared_boxed(input: &syn::DeriveInput) -> syn::Result<proc_macro2::T

impl #crate_ident::translate::IntoGlibPtr<*mut #refcounted_type_prefix::InnerType> 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 = <Self as #crate_ident::subclass::shared::SharedType>::into_refcounted(self);
#refcounted_type_prefix::into_raw(r) as *mut _
}
Expand Down
4 changes: 2 additions & 2 deletions glib/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 _
}
Expand All @@ -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 _
}
Expand Down
4 changes: 2 additions & 2 deletions glib/src/boxed_inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,15 +538,15 @@ 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 _
}
}

#[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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion glib/src/collections/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ impl<'a, T: TransparentPtrType + 'a> ToGlibPtrMut<'a, *mut ffi::GList> for List<

impl<T: TransparentPtrType> IntoGlibPtr<*mut ffi::GList> for List<T> {
#[inline]
unsafe fn into_glib_ptr(self) -> *mut ffi::GList {
fn into_glib_ptr(self) -> *mut ffi::GList {
self.into_raw()
}
}
Expand Down
2 changes: 1 addition & 1 deletion glib/src/collections/ptr_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ impl<'a, T: TransparentPtrType + 'a> ToGlibPtrMut<'a, *mut <T as GlibPtrDefault>

impl<T: TransparentPtrType> IntoGlibPtr<*mut <T as GlibPtrDefault>::GlibType> for PtrSlice<T> {
#[inline]
unsafe fn into_glib_ptr(self) -> *mut <T as GlibPtrDefault>::GlibType {
fn into_glib_ptr(self) -> *mut <T as GlibPtrDefault>::GlibType {
self.into_raw()
}
}
Expand Down
2 changes: 1 addition & 1 deletion glib/src/collections/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ impl<'a, T: TransparentType + 'a> ToGlibPtrMut<'a, *mut T::GlibType> for Slice<T

impl<T: TransparentType + 'static> IntoGlibPtr<*mut T::GlibType> for Slice<T> {
#[inline]
unsafe fn into_glib_ptr(self) -> *mut T::GlibType {
fn into_glib_ptr(self) -> *mut T::GlibType {
self.into_raw()
}
}
Expand Down
2 changes: 1 addition & 1 deletion glib/src/collections/slist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ impl<'a, T: TransparentPtrType + 'a> ToGlibPtrMut<'a, *mut ffi::GSList> for SLis

impl<T: TransparentPtrType> IntoGlibPtr<*mut ffi::GSList> for SList<T> {
#[inline]
unsafe fn into_glib_ptr(self) -> *mut ffi::GSList {
fn into_glib_ptr(self) -> *mut ffi::GSList {
self.into_raw()
}
}
Expand Down
2 changes: 1 addition & 1 deletion glib/src/collections/strv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down
18 changes: 9 additions & 9 deletions glib/src/gstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down Expand Up @@ -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)
}
},
}
}
}
Expand Down Expand Up @@ -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
}
}

Expand All @@ -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
}
}

Expand All @@ -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
}
}

Expand All @@ -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
}
}

Expand Down
4 changes: 2 additions & 2 deletions glib/src/match_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,15 @@ 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 _
}
}
#[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 _
}
Expand Down
4 changes: 2 additions & 2 deletions glib/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 _
}
Expand All @@ -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 _
}
Expand Down
4 changes: 2 additions & 2 deletions glib/src/param_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions glib/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 _
}
Expand All @@ -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 _
}
Expand Down
2 changes: 1 addition & 1 deletion glib/src/subclass/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) };
Expand Down
6 changes: 3 additions & 3 deletions glib/src/subclass/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down
4 changes: 2 additions & 2 deletions glib/src/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,12 +716,12 @@ pub trait IntoGlibPtr<P: Ptr> {
// 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<P: Ptr, T: IntoGlibPtr<P>> IntoGlibPtr<P> for Option<T> {
#[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)
})
Expand Down
2 changes: 1 addition & 1 deletion glib/src/variant_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl<'a> From<VariantType> 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
}
}
Expand Down

0 comments on commit 2967438

Please sign in to comment.