Skip to content

Commit 776120e

Browse files
authored
Merge pull request #895 from pbor/boxed_mut
glib: implement ToGlibPtr<*mut _> for boxed types
2 parents b9d0f77 + e8b7da4 commit 776120e

File tree

8 files changed

+46
-44
lines changed

8 files changed

+46
-44
lines changed

glib/src/boxed.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,22 @@ macro_rules! glib_boxed_wrapper {
9090
}
9191
}
9292

93+
#[doc(hidden)]
94+
impl<'a $(, $($generic $(: $bound $(+ $bound2)*)?),+)?> $crate::translate::ToGlibPtr<'a, *mut $ffi_name> for $name $(<$($generic),+>)? {
95+
type Storage = std::marker::PhantomData<&'a $crate::boxed::Boxed<$ffi_name, Self>>;
96+
97+
#[inline]
98+
fn to_glib_none(&'a self) -> $crate::translate::Stash<'a, *mut $ffi_name, Self> {
99+
let stash = $crate::translate::ToGlibPtr::to_glib_none(&self.inner);
100+
$crate::translate::Stash(stash.0 as *mut _, stash.1)
101+
}
102+
103+
#[inline]
104+
fn to_glib_full(&self) -> *mut $ffi_name {
105+
$crate::translate::ToGlibPtr::to_glib_full(&self.inner) as *mut _
106+
}
107+
}
108+
93109
#[doc(hidden)]
94110
impl<'a $(, $($generic $(: $bound $(+ $bound2)*)?),+)?> $crate::translate::ToGlibPtrMut<'a, *mut $ffi_name> for $name $(<$($generic),+>)? {
95111
type Storage = std::marker::PhantomData<&'a mut $crate::boxed::Boxed<$ffi_name, Self>>;

glib/src/collections/ptr_slice.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,10 +1193,10 @@ mod test {
11931193

11941194
let slice = unsafe {
11951195
let ptr = ffi::g_malloc(mem::size_of::<ffi::GDate>() * 4) as *mut *mut ffi::GError;
1196-
ptr::write(ptr.add(0), items[0].to_glib_full() as *mut _);
1197-
ptr::write(ptr.add(1), items[1].to_glib_full() as *mut _);
1198-
ptr::write(ptr.add(2), items[2].to_glib_full() as *mut _);
1199-
ptr::write(ptr.add(3), items[3].to_glib_full() as *mut _);
1196+
ptr::write(ptr.add(0), items[0].to_glib_full());
1197+
ptr::write(ptr.add(1), items[1].to_glib_full());
1198+
ptr::write(ptr.add(2), items[2].to_glib_full());
1199+
ptr::write(ptr.add(3), items[3].to_glib_full());
12001200

12011201
PtrSlice::<crate::Error>::from_glib_full_num(ptr, 4, false)
12021202
};

glib/src/value_array.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ impl ops::Deref for ValueArray {
114114

115115
unsafe {
116116
slice::from_raw_parts(
117-
(*self.to_glib_none().0).values as *const Value,
118-
(*self.to_glib_none().0).n_values as usize,
117+
(*self.as_ptr()).values as *const Value,
118+
(*self.as_ptr()).n_values as usize,
119119
)
120120
}
121121
}
@@ -130,8 +130,8 @@ impl ops::DerefMut for ValueArray {
130130

131131
unsafe {
132132
slice::from_raw_parts_mut(
133-
(*self.to_glib_none().0).values as *mut Value,
134-
(*self.to_glib_none().0).n_values as usize,
133+
(*self.as_ptr()).values as *mut Value,
134+
(*self.as_ptr()).n_values as usize,
135135
)
136136
}
137137
}

pango/src/attr_list.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ impl AttrList {
1111
pub fn change(&self, attr: impl Into<Attribute>) {
1212
unsafe {
1313
let attr = attr.into();
14-
ffi::pango_attr_list_change(self.to_glib_none().0, attr.to_glib_none().0 as *mut _);
14+
ffi::pango_attr_list_change(self.to_glib_none().0, attr.to_glib_none().0);
1515
mem::forget(attr); //As attr transferred fully
1616
}
1717
}
@@ -32,7 +32,7 @@ impl AttrList {
3232
pub fn insert(&self, attr: impl Into<Attribute>) {
3333
unsafe {
3434
let attr = attr.into();
35-
ffi::pango_attr_list_insert(self.to_glib_none().0, attr.to_glib_none().0 as *mut _);
35+
ffi::pango_attr_list_insert(self.to_glib_none().0, attr.to_glib_none().0);
3636
mem::forget(attr); //As attr transferred fully
3737
}
3838
}
@@ -41,10 +41,7 @@ impl AttrList {
4141
pub fn insert_before(&self, attr: impl Into<Attribute>) {
4242
unsafe {
4343
let attr = attr.into();
44-
ffi::pango_attr_list_insert_before(
45-
self.to_glib_none().0,
46-
attr.to_glib_none().0 as *mut _,
47-
);
44+
ffi::pango_attr_list_insert_before(self.to_glib_none().0, attr.to_glib_none().0);
4845
mem::forget(attr); //As attr transferred fully
4946
}
5047
}

pango/src/attribute.rs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,37 @@ impl Attribute {
88
#[doc(alias = "get_attr_class")]
99
#[inline]
1010
pub fn attr_class(&self) -> AttrClass {
11-
unsafe { from_glib_none((*self.to_glib_none().0).klass) }
11+
unsafe { from_glib_none((*self.as_ptr()).klass) }
1212
}
1313

1414
#[inline]
1515
pub fn type_(&self) -> AttrType {
16-
unsafe { from_glib((*(*self.to_glib_none().0).klass).type_) }
16+
unsafe { from_glib((*(*self.as_ptr()).klass).type_) }
1717
}
1818

1919
#[doc(alias = "get_start_index")]
2020
#[inline]
2121
pub fn start_index(&self) -> u32 {
22-
unsafe {
23-
let stash = self.to_glib_none();
24-
(*stash.0).start_index
25-
}
22+
unsafe { (*self.as_ptr()).start_index }
2623
}
2724

2825
#[doc(alias = "get_end_index")]
2926
#[inline]
3027
pub fn end_index(&self) -> u32 {
31-
unsafe {
32-
let stash = self.to_glib_none();
33-
(*stash.0).end_index
34-
}
28+
unsafe { (*self.as_ptr()).end_index }
3529
}
3630

3731
#[inline]
3832
pub fn set_start_index(&mut self, index: u32) {
3933
unsafe {
40-
let stash = self.to_glib_none_mut();
41-
(*stash.0).start_index = index;
34+
(*self.as_ptr()).start_index = index;
4235
}
4336
}
4437

4538
#[inline]
4639
pub fn set_end_index(&mut self, index: u32) {
4740
unsafe {
48-
let stash = self.to_glib_none_mut();
49-
(*stash.0).end_index = index;
41+
(*self.as_ptr()).end_index = index;
5042
}
5143
}
5244

@@ -120,9 +112,8 @@ macro_rules! define_attribute_struct {
120112
#[doc(alias = "pango_attribute_equal")]
121113
fn equal<'a, T: crate::attribute::IsAttribute>(&self, attr2: &'a T) -> bool {
122114
unsafe {
123-
glib::translate::from_glib(ffi::pango_attribute_equal(
124-
glib::translate::ToGlibPtr::to_glib_none(self).0 as *const ffi::PangoAttribute,
125-
glib::translate::ToGlibPtr::to_glib_none(attr2.upcast_ref()).0 as *const ffi::PangoAttribute,
115+
glib::translate::from_glib(ffi::pango_attribute_equal(self.as_ptr() as *const ffi::PangoAttribute,
116+
glib::translate::ToGlibPtr::to_glib_none(attr2.upcast_ref()).0,
126117
))
127118
}
128119
}

pango/src/glyph_item.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ use crate::{GlyphItem, GlyphString, Item};
66

77
impl GlyphItem {
88
pub fn item(&self) -> Item {
9-
unsafe { from_glib_none((*self.to_glib_none().0).item) }
9+
unsafe { from_glib_none((*self.as_ptr()).item) }
1010
}
1111

1212
pub fn glyph_string(&self) -> GlyphString {
13-
unsafe { from_glib_none((*self.to_glib_none().0).glyphs) }
13+
unsafe { from_glib_none((*self.as_ptr()).glyphs) }
1414
}
1515

1616
#[doc(alias = "pango_glyph_item_get_logical_widths")]

pango/src/glyph_string.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,37 @@ use crate::{GlyphInfo, GlyphString};
77
impl GlyphString {
88
#[inline]
99
pub fn num_glyphs(&self) -> i32 {
10-
unsafe { (*self.to_glib_none().0).num_glyphs }
10+
unsafe { (*self.as_ptr()).num_glyphs }
1111
}
1212

1313
#[inline]
1414
pub fn glyph_info(&self) -> &[GlyphInfo] {
1515
unsafe {
16-
let ptr = (*self.to_glib_none().0).glyphs;
16+
let ptr = (*self.as_ptr()).glyphs;
1717
Slice::from_glib_borrow_num(ptr, self.num_glyphs() as usize)
1818
}
1919
}
2020

2121
#[inline]
2222
pub fn glyph_info_mut(&mut self) -> &mut [GlyphInfo] {
2323
unsafe {
24-
let ptr = (*self.to_glib_none().0).glyphs;
24+
let ptr = (*self.as_ptr()).glyphs;
2525
Slice::from_glib_borrow_num_mut(ptr, self.num_glyphs() as usize)
2626
}
2727
}
2828

2929
#[inline]
3030
pub fn log_clusters(&self) -> &[i32] {
3131
unsafe {
32-
let ptr = (*self.to_glib_none().0).log_clusters as *const i32;
32+
let ptr = (*self.as_ptr()).log_clusters as *const i32;
3333
Slice::from_glib_borrow_num(ptr, self.num_glyphs() as usize)
3434
}
3535
}
3636

3737
#[inline]
3838
pub fn log_clusters_mut(&mut self) -> &mut [i32] {
3939
unsafe {
40-
let ptr = (*self.to_glib_none().0).log_clusters;
40+
let ptr = (*self.as_ptr()).log_clusters;
4141
Slice::from_glib_borrow_num_mut(ptr, self.num_glyphs() as usize)
4242
}
4343
}

pango/src/item.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

3-
use glib::translate::*;
4-
53
use crate::{Analysis, Item};
64

75
impl Item {
86
pub fn offset(&self) -> i32 {
9-
unsafe { (*self.to_glib_none().0).offset }
7+
unsafe { (*self.as_ptr()).offset }
108
}
119

1210
pub fn length(&self) -> i32 {
13-
unsafe { (*self.to_glib_none().0).length }
11+
unsafe { (*self.as_ptr()).length }
1412
}
1513

1614
pub fn num_chars(&self) -> i32 {
17-
unsafe { (*self.to_glib_none().0).num_chars }
15+
unsafe { (*self.as_ptr()).num_chars }
1816
}
1917

2018
pub fn analysis(&self) -> &Analysis {
21-
unsafe { &*(&((*self.to_glib_none().0).analysis) as *const _ as *const Analysis) }
19+
unsafe { &*(&((*self.as_ptr()).analysis) as *const _ as *const Analysis) }
2220
}
2321
}

0 commit comments

Comments
 (0)