Skip to content

Commit fc2917d

Browse files
authored
Merge pull request #1559 from sdroege/derive-boxed-no-transparent-ptr-type
glib-macros: Derived boxed types are not TransparentPtrType but `TransparentType`
2 parents 8c8b9b1 + 84192fa commit fc2917d

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

glib-macros/src/boxed_derive.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,9 @@ pub fn impl_boxed(input: &syn::DeriveInput) -> syn::Result<TokenStream> {
192192

193193
#impl_from_value
194194

195-
unsafe impl #crate_ident::translate::TransparentPtrType for #name {}
195+
unsafe impl #crate_ident::translate::TransparentType for #name {
196+
type GlibType = #name;
197+
}
196198

197199
impl #crate_ident::translate::GlibPtrDefault for #name {
198200
type GlibType = *mut #name;

glib-macros/tests/test.rs

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

33
use glib::{
4-
collections::PtrSlice,
4+
collections::Slice,
55
prelude::*,
66
translate::{FromGlib, IntoGlib},
77
};
@@ -185,19 +185,16 @@ fn derive_boxed_nullable() {
185185
}
186186

187187
#[test]
188-
fn boxed_transparent_ptr() {
188+
fn boxed_transparent() {
189189
#[derive(Clone, Debug, PartialEq, Eq, glib::Boxed)]
190190
#[boxed_type(name = "MyBoxed")]
191191
struct MyBoxed(String);
192192

193193
let vec = vec![MyBoxed(String::from("abc")), MyBoxed(String::from("dfg"))];
194194

195-
// PtrSlice requires TransparentPtrType trait
196-
let ptr_slice = PtrSlice::from(vec);
197-
assert_eq!(
198-
ptr_slice.last(),
199-
Some(MyBoxed(String::from("dfg"))).as_ref()
200-
);
195+
// Slice requires TransparentType trait
196+
let slice = Slice::from(vec);
197+
assert_eq!(slice.last(), Some(MyBoxed(String::from("dfg"))).as_ref());
201198
}
202199

203200
#[test]

glib/src/collections/ptr_slice.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,11 @@ impl<T: TransparentPtrType> PtrSlice<T> {
601601
/// Creates a new empty slice.
602602
#[inline]
603603
pub fn new() -> Self {
604+
debug_assert_eq!(
605+
mem::size_of::<T>(),
606+
mem::size_of::<<T as GlibPtrDefault>::GlibType>()
607+
);
608+
604609
PtrSlice {
605610
ptr: ptr::NonNull::dangling(),
606611
len: 0,

glib/src/collections/slice.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ impl<T: TransparentType> Slice<T> {
403403
/// Borrows a C array.
404404
#[inline]
405405
pub unsafe fn from_glib_borrow_num<'a>(ptr: *const T::GlibType, len: usize) -> &'a [T] {
406+
debug_assert_eq!(mem::size_of::<T>(), mem::size_of::<T::GlibType>());
406407
debug_assert!(!ptr.is_null() || len == 0);
407408

408409
if len == 0 {
@@ -416,6 +417,7 @@ impl<T: TransparentType> Slice<T> {
416417
/// Borrows a mutable C array.
417418
#[inline]
418419
pub unsafe fn from_glib_borrow_num_mut<'a>(ptr: *mut T::GlibType, len: usize) -> &'a mut [T] {
420+
debug_assert_eq!(mem::size_of::<T>(), mem::size_of::<T::GlibType>());
419421
debug_assert!(!ptr.is_null() || len == 0);
420422

421423
if len == 0 {
@@ -432,6 +434,7 @@ impl<T: TransparentType> Slice<T> {
432434
ptr: *const *const T::GlibType,
433435
len: usize,
434436
) -> &'a [&'a T] {
437+
debug_assert_eq!(mem::size_of::<T>(), mem::size_of::<T::GlibType>());
435438
debug_assert!(!ptr.is_null() || len == 0);
436439

437440
if len == 0 {
@@ -448,6 +451,7 @@ impl<T: TransparentType> Slice<T> {
448451
ptr: *mut *mut T::GlibType,
449452
len: usize,
450453
) -> &'a mut [&'a mut T] {
454+
debug_assert_eq!(mem::size_of::<T>(), mem::size_of::<T::GlibType>());
451455
debug_assert!(!ptr.is_null() || len == 0);
452456

453457
if len == 0 {
@@ -522,6 +526,8 @@ impl<T: TransparentType> Slice<T> {
522526
/// Creates a new empty slice.
523527
#[inline]
524528
pub fn new() -> Self {
529+
debug_assert_eq!(mem::size_of::<T>(), mem::size_of::<T::GlibType>());
530+
525531
Slice {
526532
ptr: ptr::NonNull::dangling(),
527533
len: 0,

0 commit comments

Comments
 (0)