bevy icon indicating copy to clipboard operation
bevy copied to clipboard

Add safe constructors for untyped pointers `Ptr` and `PtrMut`

Open joseph-gio opened this issue 3 years ago • 1 comments

Objective

Currently, Ptr and PtrMut can only be constructed via unsafe code. This means that downgrading a reference to an untyped pointer is very cumbersome, despite being a very simple operation.

Solution

Define conversions for easily and safely constructing untyped pointers. This is the non-owned counterpart to OwningPtr::make.

Before:

let ptr = unsafe { PtrMut::new(NonNull::from(&mut value).cast()) };

After:

let ptr = PtrMut::from(&mut value);

joseph-gio avatar Nov 10 '22 18:11 joseph-gio

This looks reasonable to me, but I've added S-Controversial so it doesn't get merged before @TheRawMeatball has given their input.

DJMcNab avatar Nov 10 '22 19:11 DJMcNab

bors r+

cart avatar Nov 14 '22 22:11 cart