Skip to content

Commit

Permalink
glib: collections: Add some more debug assertions about type sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
sdroege committed Oct 29, 2024
1 parent 6d9a350 commit 84192fa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions glib/src/collections/ptr_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,11 @@ impl<T: TransparentPtrType> PtrSlice<T> {
/// Creates a new empty slice.
#[inline]
pub fn new() -> Self {
debug_assert_eq!(
mem::size_of::<T>(),
mem::size_of::<<T as GlibPtrDefault>::GlibType>()
);

PtrSlice {
ptr: ptr::NonNull::dangling(),
len: 0,
Expand Down
6 changes: 6 additions & 0 deletions glib/src/collections/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ impl<T: TransparentType> Slice<T> {
/// Borrows a C array.
#[inline]
pub unsafe fn from_glib_borrow_num<'a>(ptr: *const T::GlibType, len: usize) -> &'a [T] {
debug_assert_eq!(mem::size_of::<T>(), mem::size_of::<T::GlibType>());
debug_assert!(!ptr.is_null() || len == 0);

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

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

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

if len == 0 {
Expand Down Expand Up @@ -522,6 +526,8 @@ impl<T: TransparentType> Slice<T> {
/// Creates a new empty slice.
#[inline]
pub fn new() -> Self {
debug_assert_eq!(mem::size_of::<T>(), mem::size_of::<T::GlibType>());

Slice {
ptr: ptr::NonNull::dangling(),
len: 0,
Expand Down

0 comments on commit 84192fa

Please sign in to comment.