Skip to content

Commit 3795b46

Browse files
committed
Reduce compile time of bevy_ptr::OwnedPtr::make function
1 parent 2526410 commit 3795b46

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

crates/bevy_ptr/src/lib.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,13 +397,23 @@ impl<'a, T: ?Sized> From<&'a mut T> for PtrMut<'a> {
397397
}
398398

399399
impl<'a> OwningPtr<'a> {
400+
/// This exists mostly to reduce compile times;
401+
/// code is only duplicated per type, rather than per function called.
402+
///
403+
/// # Safety
404+
///
405+
/// Safety constraints of [`PtrMut::promote`] must be upheld.
406+
unsafe fn make_internal<T>(temp: &mut ManuallyDrop<T>) -> OwningPtr<'_> {
407+
// SAFETY: The constraints of `promote` are upheld by caller.
408+
unsafe { PtrMut::from(&mut *temp).promote() }
409+
}
410+
400411
/// Consumes a value and creates an [`OwningPtr`] to it while ensuring a double drop does not happen.
401412
#[inline]
402413
pub fn make<T, F: FnOnce(OwningPtr<'_>) -> R, R>(val: T, f: F) -> R {
403-
let mut temp = ManuallyDrop::new(val);
404414
// SAFETY: The value behind the pointer will not get dropped or observed later,
405415
// so it's safe to promote it to an owning pointer.
406-
f(unsafe { PtrMut::from(&mut *temp).promote() })
416+
f(unsafe { Self::make_internal(&mut ManuallyDrop::new(val)) })
407417
}
408418
}
409419

0 commit comments

Comments
 (0)