Skip to content

Commit d9c876d

Browse files
committed
Add new FromGlibPtrBorrow2 and FromGlibPtrBorrowMut2 traits
These borrow more safely the underlying type and are supposed to replace the old FromGlibPtrBorrow trait once all users are gone, as well as the manual implementations for the trait functions which are deprecated now.
1 parent e919282 commit d9c876d

File tree

13 files changed

+280
-138
lines changed

13 files changed

+280
-138
lines changed

glib/src/boxed.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ macro_rules! glib_boxed_wrapper {
4747

4848
#[doc = "Borrows the underlying C value."]
4949
#[inline]
50+
#[deprecated = "Use FromGlibBorrow2 trait"]
5051
pub unsafe fn from_glib_ptr_borrow(ptr: &*mut $ffi_name) -> &Self {
5152
debug_assert_eq!(
5253
std::mem::size_of::<Self>(),
@@ -58,6 +59,7 @@ macro_rules! glib_boxed_wrapper {
5859

5960
#[doc = "Borrows the underlying C value mutably."]
6061
#[inline]
62+
#[deprecated = "Use FromGlibBorrowMut2 trait"]
6163
pub unsafe fn from_glib_ptr_borrow_mut(ptr: &mut *mut $ffi_name) -> &mut Self {
6264
debug_assert_eq!(
6365
std::mem::size_of::<Self>(),
@@ -252,6 +254,45 @@ macro_rules! glib_boxed_wrapper {
252254
}
253255
}
254256

257+
#[doc(hidden)]
258+
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::FromGlibPtrBorrow2<*mut $ffi_name> for $name $(<$($generic),+>)? {
259+
#[inline]
260+
unsafe fn from_glib_borrow2(ptr: &*mut $ffi_name) -> &Self {
261+
debug_assert_eq!(
262+
std::mem::size_of::<Self>(),
263+
std::mem::size_of::<$crate::ffi::gpointer>()
264+
);
265+
debug_assert!(!ptr.is_null());
266+
&*(ptr as *const *mut $ffi_name as *const Self)
267+
}
268+
}
269+
270+
#[doc(hidden)]
271+
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::FromGlibPtrBorrow2<*const $ffi_name> for $name $(<$($generic),+>)? {
272+
#[inline]
273+
unsafe fn from_glib_borrow2(ptr: &*const $ffi_name) -> &Self {
274+
debug_assert_eq!(
275+
std::mem::size_of::<Self>(),
276+
std::mem::size_of::<$crate::ffi::gpointer>()
277+
);
278+
debug_assert!(!ptr.is_null());
279+
&*(ptr as *const *const $ffi_name as *const Self)
280+
}
281+
}
282+
283+
#[doc(hidden)]
284+
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::FromGlibPtrBorrowMut2<*mut $ffi_name> for $name $(<$($generic),+>)? {
285+
#[inline]
286+
unsafe fn from_glib_borrow_mut2(ptr: &mut *mut $ffi_name) -> &mut Self {
287+
debug_assert_eq!(
288+
std::mem::size_of::<Self>(),
289+
std::mem::size_of::<$crate::ffi::gpointer>()
290+
);
291+
debug_assert!(!ptr.is_null());
292+
&mut *(ptr as *mut *mut $ffi_name as *mut Self)
293+
}
294+
}
295+
255296
#[doc(hidden)]
256297
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::FromGlibContainerAsVec<*mut $ffi_name, *mut *mut $ffi_name> for $name $(<$($generic),+>)? {
257298
unsafe fn from_glib_none_num_as_vec(ptr: *mut *mut $ffi_name, num: usize) -> Vec<Self> {
@@ -363,7 +404,7 @@ macro_rules! glib_boxed_wrapper {
363404
#[inline]
364405
unsafe fn from_value(value: &'a $crate::Value) -> Self {
365406
let value = &*(value as *const $crate::Value as *const $crate::gobject_ffi::GValue);
366-
<$name $(<$($generic),+>)?>::from_glib_ptr_borrow(&*(&value.data[0].v_pointer as *const $crate::ffi::gpointer as *const *mut $ffi_name))
407+
<$name $(<$($generic),+>)? as $crate::translate::FromGlibPtrBorrow2<*mut $ffi_name>>::from_glib_borrow2(&*(&value.data[0].v_pointer as *const $crate::ffi::gpointer as *const *mut $ffi_name))
367408
}
368409
}
369410

glib/src/boxed_inline.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,15 @@ macro_rules! glib_boxed_inline_wrapper {
171171

172172
#[doc = "Borrows the underlying C value."]
173173
#[inline]
174+
#[deprecated = "Use FromGlibBorrow2 trait"]
174175
pub unsafe fn from_glib_ptr_borrow<'a>(ptr: *const $ffi_name) -> &'a Self {
175176
debug_assert!(!ptr.is_null());
176177
&*(ptr as *const Self)
177178
}
178179

179180
#[doc = "Borrows the underlying C value mutably."]
180181
#[inline]
182+
#[deprecated = "Use FromGlibBorrowMut2 trait"]
181183
pub unsafe fn from_glib_ptr_borrow_mut<'a>(ptr: *mut $ffi_name) -> &'a mut Self {
182184
debug_assert!(!ptr.is_null());
183185
&mut *(ptr as *mut Self)
@@ -229,6 +231,7 @@ macro_rules! glib_boxed_inline_wrapper {
229231
}
230232

231233
#[inline]
234+
#[allow(unused_unsafe)]
232235
fn to_glib_full(&self) -> *const $ffi_name {
233236
unsafe {
234237
let copy = |$copy_arg: *const $ffi_name| $copy_expr;
@@ -430,6 +433,30 @@ macro_rules! glib_boxed_inline_wrapper {
430433
}
431434
}
432435

436+
#[doc(hidden)]
437+
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::FromGlibPtrBorrow2<$ffi_name> for $name $(<$($generic),+>)? {
438+
#[inline]
439+
unsafe fn from_glib_borrow2(ptr: &$ffi_name) -> &Self {
440+
debug_assert_eq!(
441+
std::mem::size_of::<Self>(),
442+
std::mem::size_of::<$ffi_name>()
443+
);
444+
&*(ptr as *const $ffi_name as *const Self)
445+
}
446+
}
447+
448+
#[doc(hidden)]
449+
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::FromGlibPtrBorrowMut2<$ffi_name> for $name $(<$($generic),+>)? {
450+
#[inline]
451+
unsafe fn from_glib_borrow_mut2(ptr: &mut $ffi_name) -> &mut Self {
452+
debug_assert_eq!(
453+
std::mem::size_of::<Self>(),
454+
std::mem::size_of::<$ffi_name>()
455+
);
456+
&mut *(ptr as *mut $ffi_name as *mut Self)
457+
}
458+
}
459+
433460
#[doc(hidden)]
434461
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::FromGlibContainerAsVec<*mut $ffi_name, *mut $ffi_name> for $name $(<$($generic),+>)? {
435462
unsafe fn from_glib_none_num_as_vec(ptr: *mut $ffi_name, num: usize) -> Vec<Self> {
@@ -593,7 +620,7 @@ macro_rules! glib_boxed_inline_wrapper {
593620
unsafe fn from_value(value: &'_ $crate::Value) -> Self {
594621
let ptr = $crate::gobject_ffi::g_value_get_boxed($crate::translate::ToGlibPtr::to_glib_none(value).0);
595622
debug_assert!(!ptr.is_null());
596-
&*(ptr as *const $ffi_name as *const $name $(<$($generic),+>)?)
623+
<$name $(<$($generic),+>)? as $crate::translate::FromGlibPtrBorrow2<$ffi_name>>::from_glib_borrow2(&*(ptr as *const $ffi_name))
597624
}
598625
}
599626

glib/src/gobject/interface_info.rs

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,18 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

3-
use crate::gobject_ffi;
3+
use crate::{gobject_ffi, translate::*};
44

5-
#[derive(Debug, Copy, Clone)]
6-
#[doc(alias = "GInterfaceInfo")]
7-
#[repr(transparent)]
8-
pub struct InterfaceInfo(pub(crate) gobject_ffi::GInterfaceInfo);
9-
10-
impl InterfaceInfo {
11-
// rustdoc-stripper-ignore-next
12-
/// Returns a `GInterfaceInfo` pointer.
13-
#[doc(hidden)]
14-
#[inline]
15-
pub fn as_ptr(&self) -> *mut gobject_ffi::GInterfaceInfo {
16-
&self.0 as *const gobject_ffi::GInterfaceInfo as *mut _
17-
}
18-
19-
// rustdoc-stripper-ignore-next
20-
/// Borrows the underlying C value mutably.
21-
#[doc(hidden)]
22-
#[inline]
23-
pub unsafe fn from_glib_ptr_borrow_mut<'a>(
24-
ptr: *mut gobject_ffi::GInterfaceInfo,
25-
) -> &'a mut Self {
26-
&mut *(ptr as *mut Self)
27-
}
5+
wrapper! {
6+
#[derive(Debug)]
7+
#[doc(alias = "GInterfaceInfo")]
8+
pub struct InterfaceInfo(BoxedInline<gobject_ffi::GInterfaceInfo>);
289
}
2910

11+
unsafe impl Send for InterfaceInfo {}
12+
unsafe impl Sync for InterfaceInfo {}
13+
3014
impl Default for InterfaceInfo {
31-
// rustdoc-stripper-ignore-next
32-
/// Creates a new InterfaceInfo with default value.
3315
fn default() -> Self {
34-
Self(gobject_ffi::GInterfaceInfo {
35-
interface_init: None,
36-
interface_finalize: None,
37-
interface_data: ::std::ptr::null_mut(),
38-
})
16+
unsafe { Self::uninitialized() }
3917
}
4018
}

glib/src/gobject/type_info.rs

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,18 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

3-
use crate::gobject_ffi;
3+
use crate::{gobject_ffi, translate::*};
44

5-
#[derive(Debug, Copy, Clone)]
6-
#[doc(alias = "GTypeInfo")]
7-
#[repr(transparent)]
8-
pub struct TypeInfo(pub(crate) gobject_ffi::GTypeInfo);
9-
10-
impl TypeInfo {
11-
// rustdoc-stripper-ignore-next
12-
/// Returns a `GTypeInfo` pointer.
13-
#[doc(hidden)]
14-
#[inline]
15-
pub fn as_ptr(&self) -> *mut gobject_ffi::GTypeInfo {
16-
&self.0 as *const gobject_ffi::GTypeInfo as *mut _
17-
}
18-
19-
// rustdoc-stripper-ignore-next
20-
/// Borrows the underlying C value mutably.
21-
#[doc(hidden)]
22-
#[inline]
23-
pub unsafe fn from_glib_ptr_borrow_mut<'a>(ptr: *mut gobject_ffi::GTypeInfo) -> &'a mut Self {
24-
&mut *(ptr as *mut Self)
25-
}
5+
wrapper! {
6+
#[derive(Debug)]
7+
#[doc(alias = "GTypeInfo")]
8+
pub struct TypeInfo(BoxedInline<gobject_ffi::GTypeInfo>);
269
}
2710

11+
unsafe impl Send for TypeInfo {}
12+
unsafe impl Sync for TypeInfo {}
13+
2814
impl Default for TypeInfo {
29-
// rustdoc-stripper-ignore-next
30-
/// Creates a new TypeInfo with default value.
3115
fn default() -> Self {
32-
Self(gobject_ffi::GTypeInfo {
33-
class_size: 0u16,
34-
base_init: None,
35-
base_finalize: None,
36-
class_init: None,
37-
class_finalize: None,
38-
class_data: ::std::ptr::null(),
39-
instance_size: 0,
40-
n_preallocs: 0,
41-
instance_init: None,
42-
value_table: ::std::ptr::null(),
43-
})
16+
unsafe { Self::uninitialized() }
4417
}
4518
}

glib/src/gobject/type_value_table.rs

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,18 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

3-
use crate::gobject_ffi;
3+
use crate::{gobject_ffi, translate::*};
44

5-
#[derive(Debug, Copy, Clone)]
6-
#[doc(alias = "GTypeValueTable")]
7-
#[repr(transparent)]
8-
pub struct TypeValueTable(pub(crate) gobject_ffi::GTypeValueTable);
9-
10-
impl TypeValueTable {
11-
// rustdoc-stripper-ignore-next
12-
/// Returns a `GTypeValueTable` pointer.
13-
#[doc(hidden)]
14-
#[inline]
15-
pub fn as_ptr(&self) -> *mut gobject_ffi::GTypeValueTable {
16-
&self.0 as *const gobject_ffi::GTypeValueTable as *mut _
17-
}
18-
19-
// rustdoc-stripper-ignore-next
20-
/// Borrows the underlying C value mutably.
21-
#[doc(hidden)]
22-
#[inline]
23-
pub unsafe fn from_glib_ptr_borrow_mut<'a>(
24-
ptr: *mut gobject_ffi::GTypeValueTable,
25-
) -> &'a mut Self {
26-
&mut *(ptr as *mut Self)
27-
}
5+
wrapper! {
6+
#[derive(Debug)]
7+
#[doc(alias = "GTypeValueTable")]
8+
pub struct TypeValueTable(BoxedInline<gobject_ffi::GTypeValueTable>);
289
}
2910

11+
unsafe impl Send for TypeValueTable {}
12+
unsafe impl Sync for TypeValueTable {}
13+
3014
impl Default for TypeValueTable {
31-
// rustdoc-stripper-ignore-next
32-
/// Creates a new TypeValueTable with default value.
3315
fn default() -> Self {
34-
Self(gobject_ffi::GTypeValueTable {
35-
value_init: None,
36-
value_free: None,
37-
value_copy: None,
38-
value_peek_pointer: None,
39-
collect_format: ::std::ptr::null(),
40-
collect_value: None,
41-
lcopy_format: ::std::ptr::null(),
42-
lcopy_value: None,
43-
})
16+
unsafe { Self::uninitialized() }
4417
}
4518
}

glib/src/match_info.rs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,6 @@ impl MatchInfo<'_> {
3636
pub fn as_ptr(&self) -> *mut ffi::GMatchInfo {
3737
self.inner.as_ptr()
3838
}
39-
#[doc = "Borrows the underlying C value."]
40-
#[inline]
41-
pub unsafe fn from_glib_ptr_borrow(ptr: &*mut ffi::GMatchInfo) -> &Self {
42-
debug_assert_eq!(
43-
std::mem::size_of::<Self>(),
44-
std::mem::size_of::<crate::ffi::gpointer>()
45-
);
46-
debug_assert!(!ptr.is_null());
47-
&*(ptr as *const *mut ffi::GMatchInfo as *const Self)
48-
}
4939
}
5040

5141
#[doc(hidden)]
@@ -149,6 +139,32 @@ impl FromGlibPtrBorrow<*const ffi::GMatchInfo> for MatchInfo<'_> {
149139
}
150140
}
151141

142+
impl<'a> FromGlibPtrBorrow2<*mut ffi::GMatchInfo> for MatchInfo<'a> {
143+
#[doc = "Borrows the underlying C value."]
144+
#[inline]
145+
unsafe fn from_glib_borrow2(ptr: &*mut ffi::GMatchInfo) -> &Self {
146+
debug_assert_eq!(
147+
std::mem::size_of::<Self>(),
148+
std::mem::size_of::<crate::ffi::gpointer>()
149+
);
150+
debug_assert!(!ptr.is_null());
151+
&*(ptr as *const *mut ffi::GMatchInfo as *const Self)
152+
}
153+
}
154+
155+
impl<'a> FromGlibPtrBorrow2<*const ffi::GMatchInfo> for MatchInfo<'a> {
156+
#[doc = "Borrows the underlying C value."]
157+
#[inline]
158+
unsafe fn from_glib_borrow2(ptr: &*const ffi::GMatchInfo) -> &Self {
159+
debug_assert_eq!(
160+
std::mem::size_of::<Self>(),
161+
std::mem::size_of::<crate::ffi::gpointer>()
162+
);
163+
debug_assert!(!ptr.is_null());
164+
&*(ptr as *const *const ffi::GMatchInfo as *const Self)
165+
}
166+
}
167+
152168
#[doc(hidden)]
153169
impl IntoGlibPtr<*mut ffi::GMatchInfo> for MatchInfo<'_> {
154170
#[inline]
@@ -200,7 +216,7 @@ unsafe impl<'a, 'input: 'a> crate::value::FromValue<'a> for &'a MatchInfo<'input
200216
#[inline]
201217
unsafe fn from_value(value: &'a crate::Value) -> Self {
202218
let value = &*(value as *const crate::Value as *const crate::gobject_ffi::GValue);
203-
<MatchInfo<'input>>::from_glib_ptr_borrow(
219+
<MatchInfo<'input>>::from_glib_borrow2(
204220
&*(&value.data[0].v_pointer as *const crate::ffi::gpointer
205221
as *const *mut ffi::GMatchInfo),
206222
)

0 commit comments

Comments
 (0)