Skip to content

Commit

Permalink
Merge pull request #895 from pbor/boxed_mut
Browse files Browse the repository at this point in the history
glib: implement ToGlibPtr<*mut _> for boxed types
  • Loading branch information
sdroege authored Jan 17, 2023
2 parents b9d0f77 + e8b7da4 commit 776120e
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 44 deletions.
16 changes: 16 additions & 0 deletions glib/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>;
Expand Down
8 changes: 4 additions & 4 deletions glib/src/collections/ptr_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1193,10 +1193,10 @@ mod test {

let slice = unsafe {
let ptr = ffi::g_malloc(mem::size_of::<ffi::GDate>() * 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::<crate::Error>::from_glib_full_num(ptr, 4, false)
};
Expand Down
8 changes: 4 additions & 4 deletions glib/src/value_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}
}
Expand All @@ -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,
)
}
}
Expand Down
9 changes: 3 additions & 6 deletions pango/src/attr_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl AttrList {
pub fn change(&self, attr: impl Into<Attribute>) {
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
}
}
Expand All @@ -32,7 +32,7 @@ impl AttrList {
pub fn insert(&self, attr: impl Into<Attribute>) {
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
}
}
Expand All @@ -41,10 +41,7 @@ impl AttrList {
pub fn insert_before(&self, attr: impl Into<Attribute>) {
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
}
}
Expand Down
25 changes: 8 additions & 17 deletions pango/src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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,
))
}
}
Expand Down
4 changes: 2 additions & 2 deletions pango/src/glyph_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
10 changes: 5 additions & 5 deletions pango/src/glyph_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,37 @@ 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)
}
}

#[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)
}
}

#[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)
}
}

#[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)
}
}
Expand Down
10 changes: 4 additions & 6 deletions pango/src/item.rs
Original file line number Diff line number Diff line change
@@ -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) }
}
}

0 comments on commit 776120e

Please sign in to comment.