ptr: allow Ptr and PtrMut construction for references to values of ?Sized types (#14479)

# Objective

- Currently `bevy_ptr::{Ptr, PtrMut}` have `From` implementations from
references.
- These implementations impose an implicit `Sized` bound so `bevy_ptr`
types cannot be created from references to slices and trait objects.
- I ran into this trying to use `Ptr<'static>` as an untyped `&'static
dyn Any`, and [had to work around
it](f32b41512c/src/registry.rs (L214-L219)).

## Solution

- Relax the `Sized` bound on the relevant `From` implementations.
This commit is contained in:
radiish 2024-07-26 00:14:16 +01:00 committed by GitHub
parent 438217035d
commit 6dbc8b8f6f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -314,7 +314,7 @@ impl<'a, A: IsAligned> Ptr<'a, A> {
}
}
impl<'a, T> From<&'a T> for Ptr<'a> {
impl<'a, T: ?Sized> From<&'a T> for Ptr<'a> {
#[inline]
fn from(val: &'a T) -> Self {
// SAFETY: The returned pointer has the same lifetime as the passed reference.
@ -384,7 +384,7 @@ impl<'a, A: IsAligned> PtrMut<'a, A> {
}
}
impl<'a, T> From<&'a mut T> for PtrMut<'a> {
impl<'a, T: ?Sized> From<&'a mut T> for PtrMut<'a> {
#[inline]
fn from(val: &'a mut T) -> Self {
// SAFETY: The returned pointer has the same lifetime as the passed reference.