From e8b7da4f5ad63ee1f88192ed7f8ec465f4c9c2f8 Mon Sep 17 00:00:00 2001 From: Matteo Biggio Date: Fri, 13 Jan 2023 09:07:58 +0100 Subject: [PATCH] glib: implement ToGlibPtr<*mut _> for boxed types --- glib/src/boxed.rs | 16 ++++++++++++++++ glib/src/collections/ptr_slice.rs | 8 ++++---- glib/src/value_array.rs | 8 ++++---- pango/src/attr_list.rs | 9 +++------ pango/src/attribute.rs | 25 ++++++++----------------- pango/src/glyph_item.rs | 4 ++-- pango/src/glyph_string.rs | 10 +++++----- pango/src/item.rs | 10 ++++------ 8 files changed, 46 insertions(+), 44 deletions(-) diff --git a/glib/src/boxed.rs b/glib/src/boxed.rs index 3e72569bc983..5c0950f27333 100644 --- a/glib/src/boxed.rs +++ b/glib/src/boxed.rs @@ -90,6 +90,22 @@ macro_rules! glib_boxed_wrapper { } } + #[doc(hidden)] + impl<'a $(, $($generic $(: $bound $(+ $bound2)*)?),+)?> $crate::translate::ToGlibPtr<'a, *mut $ffi_name> for $name $(<$($generic),+>)? { + type Storage = std::marker::PhantomData<&'a $crate::boxed::Boxed<$ffi_name, Self>>; + + #[inline] + fn to_glib_none(&'a self) -> $crate::translate::Stash<'a, *mut $ffi_name, Self> { + let stash = $crate::translate::ToGlibPtr::to_glib_none(&self.inner); + $crate::translate::Stash(stash.0 as *mut _, stash.1) + } + + #[inline] + fn to_glib_full(&self) -> *mut $ffi_name { + $crate::translate::ToGlibPtr::to_glib_full(&self.inner) as *mut _ + } + } + #[doc(hidden)] impl<'a $(, $($generic $(: $bound $(+ $bound2)*)?),+)?> $crate::translate::ToGlibPtrMut<'a, *mut $ffi_name> for $name $(<$($generic),+>)? { type Storage = std::marker::PhantomData<&'a mut $crate::boxed::Boxed<$ffi_name, Self>>; diff --git a/glib/src/collections/ptr_slice.rs b/glib/src/collections/ptr_slice.rs index 8cd17da356ea..967f016a2423 100644 --- a/glib/src/collections/ptr_slice.rs +++ b/glib/src/collections/ptr_slice.rs @@ -1193,10 +1193,10 @@ mod test { let slice = unsafe { let ptr = ffi::g_malloc(mem::size_of::() * 4) as *mut *mut ffi::GError; - ptr::write(ptr.add(0), items[0].to_glib_full() as *mut _); - ptr::write(ptr.add(1), items[1].to_glib_full() as *mut _); - ptr::write(ptr.add(2), items[2].to_glib_full() as *mut _); - ptr::write(ptr.add(3), items[3].to_glib_full() as *mut _); + ptr::write(ptr.add(0), items[0].to_glib_full()); + ptr::write(ptr.add(1), items[1].to_glib_full()); + ptr::write(ptr.add(2), items[2].to_glib_full()); + ptr::write(ptr.add(3), items[3].to_glib_full()); PtrSlice::::from_glib_full_num(ptr, 4, false) }; diff --git a/glib/src/value_array.rs b/glib/src/value_array.rs index 4c0da2970364..1f97d6bca184 100644 --- a/glib/src/value_array.rs +++ b/glib/src/value_array.rs @@ -114,8 +114,8 @@ impl ops::Deref for ValueArray { unsafe { slice::from_raw_parts( - (*self.to_glib_none().0).values as *const Value, - (*self.to_glib_none().0).n_values as usize, + (*self.as_ptr()).values as *const Value, + (*self.as_ptr()).n_values as usize, ) } } @@ -130,8 +130,8 @@ impl ops::DerefMut for ValueArray { unsafe { slice::from_raw_parts_mut( - (*self.to_glib_none().0).values as *mut Value, - (*self.to_glib_none().0).n_values as usize, + (*self.as_ptr()).values as *mut Value, + (*self.as_ptr()).n_values as usize, ) } } diff --git a/pango/src/attr_list.rs b/pango/src/attr_list.rs index 6982b8939595..f210d1e49a18 100644 --- a/pango/src/attr_list.rs +++ b/pango/src/attr_list.rs @@ -11,7 +11,7 @@ impl AttrList { pub fn change(&self, attr: impl Into) { unsafe { let attr = attr.into(); - ffi::pango_attr_list_change(self.to_glib_none().0, attr.to_glib_none().0 as *mut _); + ffi::pango_attr_list_change(self.to_glib_none().0, attr.to_glib_none().0); mem::forget(attr); //As attr transferred fully } } @@ -32,7 +32,7 @@ impl AttrList { pub fn insert(&self, attr: impl Into) { unsafe { let attr = attr.into(); - ffi::pango_attr_list_insert(self.to_glib_none().0, attr.to_glib_none().0 as *mut _); + ffi::pango_attr_list_insert(self.to_glib_none().0, attr.to_glib_none().0); mem::forget(attr); //As attr transferred fully } } @@ -41,10 +41,7 @@ impl AttrList { pub fn insert_before(&self, attr: impl Into) { unsafe { let attr = attr.into(); - ffi::pango_attr_list_insert_before( - self.to_glib_none().0, - attr.to_glib_none().0 as *mut _, - ); + ffi::pango_attr_list_insert_before(self.to_glib_none().0, attr.to_glib_none().0); mem::forget(attr); //As attr transferred fully } } diff --git a/pango/src/attribute.rs b/pango/src/attribute.rs index f44321a9117c..257430a1872e 100644 --- a/pango/src/attribute.rs +++ b/pango/src/attribute.rs @@ -8,45 +8,37 @@ impl Attribute { #[doc(alias = "get_attr_class")] #[inline] pub fn attr_class(&self) -> AttrClass { - unsafe { from_glib_none((*self.to_glib_none().0).klass) } + unsafe { from_glib_none((*self.as_ptr()).klass) } } #[inline] pub fn type_(&self) -> AttrType { - unsafe { from_glib((*(*self.to_glib_none().0).klass).type_) } + unsafe { from_glib((*(*self.as_ptr()).klass).type_) } } #[doc(alias = "get_start_index")] #[inline] pub fn start_index(&self) -> u32 { - unsafe { - let stash = self.to_glib_none(); - (*stash.0).start_index - } + unsafe { (*self.as_ptr()).start_index } } #[doc(alias = "get_end_index")] #[inline] pub fn end_index(&self) -> u32 { - unsafe { - let stash = self.to_glib_none(); - (*stash.0).end_index - } + unsafe { (*self.as_ptr()).end_index } } #[inline] pub fn set_start_index(&mut self, index: u32) { unsafe { - let stash = self.to_glib_none_mut(); - (*stash.0).start_index = index; + (*self.as_ptr()).start_index = index; } } #[inline] pub fn set_end_index(&mut self, index: u32) { unsafe { - let stash = self.to_glib_none_mut(); - (*stash.0).end_index = index; + (*self.as_ptr()).end_index = index; } } @@ -120,9 +112,8 @@ macro_rules! define_attribute_struct { #[doc(alias = "pango_attribute_equal")] fn equal<'a, T: crate::attribute::IsAttribute>(&self, attr2: &'a T) -> bool { unsafe { - glib::translate::from_glib(ffi::pango_attribute_equal( - glib::translate::ToGlibPtr::to_glib_none(self).0 as *const ffi::PangoAttribute, - glib::translate::ToGlibPtr::to_glib_none(attr2.upcast_ref()).0 as *const ffi::PangoAttribute, + glib::translate::from_glib(ffi::pango_attribute_equal(self.as_ptr() as *const ffi::PangoAttribute, + glib::translate::ToGlibPtr::to_glib_none(attr2.upcast_ref()).0, )) } } diff --git a/pango/src/glyph_item.rs b/pango/src/glyph_item.rs index eb3c5fcb9bf8..e98e3e7f9686 100644 --- a/pango/src/glyph_item.rs +++ b/pango/src/glyph_item.rs @@ -6,11 +6,11 @@ use crate::{GlyphItem, GlyphString, Item}; impl GlyphItem { pub fn item(&self) -> Item { - unsafe { from_glib_none((*self.to_glib_none().0).item) } + unsafe { from_glib_none((*self.as_ptr()).item) } } pub fn glyph_string(&self) -> GlyphString { - unsafe { from_glib_none((*self.to_glib_none().0).glyphs) } + unsafe { from_glib_none((*self.as_ptr()).glyphs) } } #[doc(alias = "pango_glyph_item_get_logical_widths")] diff --git a/pango/src/glyph_string.rs b/pango/src/glyph_string.rs index 9f4ec632bd8d..906867e80792 100644 --- a/pango/src/glyph_string.rs +++ b/pango/src/glyph_string.rs @@ -7,13 +7,13 @@ use crate::{GlyphInfo, GlyphString}; impl GlyphString { #[inline] pub fn num_glyphs(&self) -> i32 { - unsafe { (*self.to_glib_none().0).num_glyphs } + unsafe { (*self.as_ptr()).num_glyphs } } #[inline] pub fn glyph_info(&self) -> &[GlyphInfo] { unsafe { - let ptr = (*self.to_glib_none().0).glyphs; + let ptr = (*self.as_ptr()).glyphs; Slice::from_glib_borrow_num(ptr, self.num_glyphs() as usize) } } @@ -21,7 +21,7 @@ impl GlyphString { #[inline] pub fn glyph_info_mut(&mut self) -> &mut [GlyphInfo] { unsafe { - let ptr = (*self.to_glib_none().0).glyphs; + let ptr = (*self.as_ptr()).glyphs; Slice::from_glib_borrow_num_mut(ptr, self.num_glyphs() as usize) } } @@ -29,7 +29,7 @@ impl GlyphString { #[inline] pub fn log_clusters(&self) -> &[i32] { unsafe { - let ptr = (*self.to_glib_none().0).log_clusters as *const i32; + let ptr = (*self.as_ptr()).log_clusters as *const i32; Slice::from_glib_borrow_num(ptr, self.num_glyphs() as usize) } } @@ -37,7 +37,7 @@ impl GlyphString { #[inline] pub fn log_clusters_mut(&mut self) -> &mut [i32] { unsafe { - let ptr = (*self.to_glib_none().0).log_clusters; + let ptr = (*self.as_ptr()).log_clusters; Slice::from_glib_borrow_num_mut(ptr, self.num_glyphs() as usize) } } diff --git a/pango/src/item.rs b/pango/src/item.rs index dd3af6575646..2d09576d2abd 100644 --- a/pango/src/item.rs +++ b/pango/src/item.rs @@ -1,23 +1,21 @@ // Take a look at the license at the top of the repository in the LICENSE file. -use glib::translate::*; - use crate::{Analysis, Item}; impl Item { pub fn offset(&self) -> i32 { - unsafe { (*self.to_glib_none().0).offset } + unsafe { (*self.as_ptr()).offset } } pub fn length(&self) -> i32 { - unsafe { (*self.to_glib_none().0).length } + unsafe { (*self.as_ptr()).length } } pub fn num_chars(&self) -> i32 { - unsafe { (*self.to_glib_none().0).num_chars } + unsafe { (*self.as_ptr()).num_chars } } pub fn analysis(&self) -> &Analysis { - unsafe { &*(&((*self.to_glib_none().0).analysis) as *const _ as *const Analysis) } + unsafe { &*(&((*self.as_ptr()).analysis) as *const _ as *const Analysis) } } }