From 36d0c40b55a5b1c40d9bf550eab47a88b7a6d3ff Mon Sep 17 00:00:00 2001 From: Mathspy Date: Thu, 23 Jan 2025 22:40:45 -0500 Subject: [PATCH] Improve ergonomics of platform_support's Instant Thanks to my friend https://github.com/repnop for helping me understand how to deal with function pointers in `unsafe` environments Co-authored-by: Wesley Norris --- crates/bevy_platform_support/src/time.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/bevy_platform_support/src/time.rs b/crates/bevy_platform_support/src/time.rs index 5bf7678fd1e48..79fc81c7fe85d 100644 --- a/crates/bevy_platform_support/src/time.rs +++ b/crates/bevy_platform_support/src/time.rs @@ -30,7 +30,7 @@ mod fallback { time::Duration, }; - static ELAPSED_GETTER: AtomicPtr Duration> = AtomicPtr::new(unset_getter as *mut _); + static ELAPSED_GETTER: AtomicPtr<()> = AtomicPtr::new(unset_getter as *mut _); /// Fallback implementation of `Instant` suitable for a `no_std` environment. /// @@ -53,7 +53,7 @@ mod fallback { let getter = ELAPSED_GETTER.load(Ordering::Acquire); // SAFETY: Function pointer is always valid - let getter = unsafe { *getter }; + let getter = unsafe { core::mem::transmute::<_, fn() -> Duration>(getter) }; Self((getter)()) } @@ -66,8 +66,8 @@ mod fallback { /// - The function provided must accurately represent the elapsed time. /// - The function must preserve all invariants of the [`Instant`] type. /// - The pointer to the function must be valid whenever [`Instant::now`] is called. - pub unsafe fn set_elapsed(getter: *mut fn() -> Duration) { - ELAPSED_GETTER.store(getter, Ordering::Release); + pub unsafe fn set_elapsed(getter: fn() -> Duration) { + ELAPSED_GETTER.store(getter as *mut _, Ordering::Release); } /// Returns the amount of time elapsed from another instant to this one,