moved Debug from derive to impl_ptr in bevy_ptr (#18042)

# Objective

Fixes #17988

## Solution

Added two Debug impls to the `impl_ptr` macro - one for Aligned and one
for Unaligned.

## Testing

No tests have been added. Would a test guaranteeing debug layout be
appropriate?

---

## Showcase

The debug representation of a `Ptr<'_, Aligned>` follows. `PtrMut` and
`OwningPtr` are similar.

`Ptr<Aligned>(0x0123456789ab)`
This commit is contained in:
Will Schroeder 2025-02-28 13:54:46 +11:00 committed by GitHub
parent b73811d40e
commit e43b28c1ec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -9,7 +9,7 @@
use core::{ use core::{
cell::UnsafeCell, cell::UnsafeCell,
fmt::{self, Formatter, Pointer}, fmt::{self, Debug, Formatter, Pointer},
marker::PhantomData, marker::PhantomData,
mem::ManuallyDrop, mem::ManuallyDrop,
num::NonZeroUsize, num::NonZeroUsize,
@ -17,11 +17,11 @@ use core::{
}; };
/// Used as a type argument to [`Ptr`], [`PtrMut`] and [`OwningPtr`] to specify that the pointer is aligned. /// Used as a type argument to [`Ptr`], [`PtrMut`] and [`OwningPtr`] to specify that the pointer is aligned.
#[derive(Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub struct Aligned; pub struct Aligned;
/// Used as a type argument to [`Ptr`], [`PtrMut`] and [`OwningPtr`] to specify that the pointer is not aligned. /// Used as a type argument to [`Ptr`], [`PtrMut`] and [`OwningPtr`] to specify that the pointer is not aligned.
#[derive(Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub struct Unaligned; pub struct Unaligned;
/// Trait that is only implemented for [`Aligned`] and [`Unaligned`] to work around the lack of ability /// Trait that is only implemented for [`Aligned`] and [`Unaligned`] to work around the lack of ability
@ -159,7 +159,7 @@ impl<'a, T: ?Sized> From<&'a mut T> for ConstNonNull<T> {
/// ///
/// It may be helpful to think of this type as similar to `&'a dyn Any` but without /// It may be helpful to think of this type as similar to `&'a dyn Any` but without
/// the metadata and able to point to data that does not correspond to a Rust type. /// the metadata and able to point to data that does not correspond to a Rust type.
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone)]
#[repr(transparent)] #[repr(transparent)]
pub struct Ptr<'a, A: IsAligned = Aligned>(NonNull<u8>, PhantomData<(&'a u8, A)>); pub struct Ptr<'a, A: IsAligned = Aligned>(NonNull<u8>, PhantomData<(&'a u8, A)>);
@ -174,7 +174,6 @@ pub struct Ptr<'a, A: IsAligned = Aligned>(NonNull<u8>, PhantomData<(&'a u8, A)>
/// ///
/// It may be helpful to think of this type as similar to `&'a mut dyn Any` but without /// It may be helpful to think of this type as similar to `&'a mut dyn Any` but without
/// the metadata and able to point to data that does not correspond to a Rust type. /// the metadata and able to point to data that does not correspond to a Rust type.
#[derive(Debug)]
#[repr(transparent)] #[repr(transparent)]
pub struct PtrMut<'a, A: IsAligned = Aligned>(NonNull<u8>, PhantomData<(&'a mut u8, A)>); pub struct PtrMut<'a, A: IsAligned = Aligned>(NonNull<u8>, PhantomData<(&'a mut u8, A)>);
@ -194,7 +193,6 @@ pub struct PtrMut<'a, A: IsAligned = Aligned>(NonNull<u8>, PhantomData<(&'a mut
/// ///
/// It may be helpful to think of this type as similar to `&'a mut ManuallyDrop<dyn Any>` but /// It may be helpful to think of this type as similar to `&'a mut ManuallyDrop<dyn Any>` but
/// without the metadata and able to point to data that does not correspond to a Rust type. /// without the metadata and able to point to data that does not correspond to a Rust type.
#[derive(Debug)]
#[repr(transparent)] #[repr(transparent)]
pub struct OwningPtr<'a, A: IsAligned = Aligned>(NonNull<u8>, PhantomData<(&'a mut u8, A)>); pub struct OwningPtr<'a, A: IsAligned = Aligned>(NonNull<u8>, PhantomData<(&'a mut u8, A)>);
@ -265,6 +263,19 @@ macro_rules! impl_ptr {
Pointer::fmt(&self.0, f) Pointer::fmt(&self.0, f)
} }
} }
impl Debug for $ptr<'_, Aligned> {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}<Aligned>({:?})", stringify!($ptr), self.0)
}
}
impl Debug for $ptr<'_, Unaligned> {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}<Unaligned>({:?})", stringify!($ptr), self.0)
}
}
}; };
} }