From 04a0a74157c34ebc4bf9f1779a67e95ce0830c74 Mon Sep 17 00:00:00 2001 From: Zachary Harrold Date: Sat, 22 Mar 2025 22:21:18 +1100 Subject: [PATCH] Address lints in `bevy_platform_support` (#18477) # Objective @mockersf noticed there were some failing lints in `bevy_platform_support`. ## Solution Addressed the lints! ## Testing - CI --- .../bevy_platform_support/src/sync/barrier.rs | 6 +-- .../src/sync/lazy_lock.rs | 6 +-- .../bevy_platform_support/src/sync/mutex.rs | 8 +-- crates/bevy_platform_support/src/sync/once.rs | 7 +-- .../bevy_platform_support/src/sync/poison.rs | 6 +-- .../bevy_platform_support/src/sync/rwlock.rs | 8 +-- .../src/time/fallback.rs | 49 ++++++++++--------- 7 files changed, 46 insertions(+), 44 deletions(-) diff --git a/crates/bevy_platform_support/src/sync/barrier.rs b/crates/bevy_platform_support/src/sync/barrier.rs index 6c179d81d6..2968a78b01 100644 --- a/crates/bevy_platform_support/src/sync/barrier.rs +++ b/crates/bevy_platform_support/src/sync/barrier.rs @@ -1,12 +1,12 @@ //! Provides `Barrier` and `BarrierWaitResult` -pub use barrier::{Barrier, BarrierWaitResult}; +pub use implementation::{Barrier, BarrierWaitResult}; #[cfg(feature = "std")] -use std::sync as barrier; +use std::sync as implementation; #[cfg(not(feature = "std"))] -mod barrier { +mod implementation { use core::fmt; /// Fallback implementation of `Barrier` from the standard library. diff --git a/crates/bevy_platform_support/src/sync/lazy_lock.rs b/crates/bevy_platform_support/src/sync/lazy_lock.rs index 8a13c1bef2..c756daeb94 100644 --- a/crates/bevy_platform_support/src/sync/lazy_lock.rs +++ b/crates/bevy_platform_support/src/sync/lazy_lock.rs @@ -1,11 +1,11 @@ //! Provides `LazyLock` -pub use lazy_lock::LazyLock; +pub use implementation::LazyLock; #[cfg(feature = "std")] -use std::sync as lazy_lock; +use std::sync as implementation; #[cfg(not(feature = "std"))] -mod lazy_lock { +mod implementation { pub use spin::Lazy as LazyLock; } diff --git a/crates/bevy_platform_support/src/sync/mutex.rs b/crates/bevy_platform_support/src/sync/mutex.rs index a059d670e9..7ff363f574 100644 --- a/crates/bevy_platform_support/src/sync/mutex.rs +++ b/crates/bevy_platform_support/src/sync/mutex.rs @@ -1,12 +1,12 @@ //! Provides `Mutex` and `MutexGuard` -pub use mutex::{Mutex, MutexGuard}; +pub use implementation::{Mutex, MutexGuard}; #[cfg(feature = "std")] -use std::sync as mutex; +use std::sync as implementation; #[cfg(not(feature = "std"))] -mod mutex { +mod implementation { use crate::sync::{LockResult, TryLockError, TryLockResult}; use core::fmt; @@ -81,7 +81,7 @@ mod mutex { } } - impl Default for Mutex { + impl Default for Mutex { fn default() -> Mutex { Mutex::new(Default::default()) } diff --git a/crates/bevy_platform_support/src/sync/once.rs b/crates/bevy_platform_support/src/sync/once.rs index 2ae733f387..f4ac34b905 100644 --- a/crates/bevy_platform_support/src/sync/once.rs +++ b/crates/bevy_platform_support/src/sync/once.rs @@ -1,12 +1,12 @@ //! Provides `Once`, `OnceState`, `OnceLock` -pub use once::{Once, OnceLock, OnceState}; +pub use implementation::{Once, OnceLock, OnceState}; #[cfg(feature = "std")] -use std::sync as once; +use std::sync as implementation; #[cfg(not(feature = "std"))] -mod once { +mod implementation { use core::{ fmt, panic::{RefUnwindSafe, UnwindSafe}, @@ -145,6 +145,7 @@ mod once { /// Creates a new `Once` value. /// /// See the standard library for further details. + #[expect(clippy::new_without_default, reason = "matching std::sync::Once")] pub const fn new() -> Self { Self { inner: OnceLock::new(), diff --git a/crates/bevy_platform_support/src/sync/poison.rs b/crates/bevy_platform_support/src/sync/poison.rs index 0aa8e168c2..79eafc4250 100644 --- a/crates/bevy_platform_support/src/sync/poison.rs +++ b/crates/bevy_platform_support/src/sync/poison.rs @@ -1,12 +1,12 @@ //! Provides `LockResult`, `PoisonError`, `TryLockError`, `TryLockResult` -pub use poison::{LockResult, PoisonError, TryLockError, TryLockResult}; +pub use implementation::{LockResult, PoisonError, TryLockError, TryLockResult}; #[cfg(feature = "std")] -use std::sync as poison; +use std::sync as implementation; #[cfg(not(feature = "std"))] -mod poison { +mod implementation { use core::{error::Error, fmt}; /// Fallback implementation of `PoisonError` from the standard library. diff --git a/crates/bevy_platform_support/src/sync/rwlock.rs b/crates/bevy_platform_support/src/sync/rwlock.rs index 627da73f32..f1f529baaf 100644 --- a/crates/bevy_platform_support/src/sync/rwlock.rs +++ b/crates/bevy_platform_support/src/sync/rwlock.rs @@ -1,12 +1,12 @@ -//! TODO: Implement `RwLock`, `RwLockReadGuard`, `RwLockWriteGuard` +//! Provides `RwLock`, `RwLockReadGuard`, `RwLockWriteGuard` -pub use rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard}; +pub use implementation::{RwLock, RwLockReadGuard, RwLockWriteGuard}; #[cfg(feature = "std")] -use std::sync as rwlock; +use std::sync as implementation; #[cfg(not(feature = "std"))] -mod rwlock { +mod implementation { use crate::sync::{LockResult, TryLockError, TryLockResult}; use core::fmt; diff --git a/crates/bevy_platform_support/src/time/fallback.rs b/crates/bevy_platform_support/src/time/fallback.rs index a0a9354902..344fe74baf 100644 --- a/crates/bevy_platform_support/src/time/fallback.rs +++ b/crates/bevy_platform_support/src/time/fallback.rs @@ -36,7 +36,7 @@ impl Instant { let getter = ELAPSED_GETTER.load(Ordering::Acquire); // SAFETY: Function pointer is always valid - let getter = unsafe { core::mem::transmute::<_, fn() -> Duration>(getter) }; + let getter = unsafe { core::mem::transmute::<*mut (), fn() -> Duration>(getter) }; Self((getter)()) } @@ -149,28 +149,29 @@ impl fmt::Debug for Instant { } fn unset_getter() -> Duration { - let _nanos: u64; - - #[cfg(target_arch = "x86")] - unsafe { - _nanos = core::arch::x86::_rdtsc(); + cfg_if::cfg_if! { + if #[cfg(target_arch = "x86")] { + // SAFETY: standard technique for getting a nanosecond counter on x86 + let nanos = unsafe { + core::arch::x86::_rdtsc() + }; + Duration::from_nanos(nanos) + } else if #[cfg(target_arch = "x86_64")] { + // SAFETY: standard technique for getting a nanosecond counter on x86_64 + let nanos = unsafe { + core::arch::x86_64::_rdtsc() + }; + Duration::from_nanos(nanos) + } else if #[cfg(target_arch = "aarch64")] { + // SAFETY: standard technique for getting a nanosecond counter of aarch64 + let nanos = unsafe { + let mut ticks: u64; + core::arch::asm!("mrs {}, cntvct_el0", out(reg) ticks); + ticks + }; + Duration::from_nanos(nanos) + } else { + panic!("An elapsed time getter has not been provided to `Instant`. Please use `Instant::set_elapsed(...)` before calling `Instant::now()`") + } } - - #[cfg(target_arch = "x86_64")] - unsafe { - _nanos = core::arch::x86_64::_rdtsc(); - } - - #[cfg(target_arch = "aarch64")] - unsafe { - let mut ticks: u64; - core::arch::asm!("mrs {}, cntvct_el0", out(reg) ticks); - _nanos = ticks; - } - - #[cfg(not(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64")))] - panic!("An elapsed time getter has not been provided to `Instant`. Please use `Instant::set_elapsed(...)` before calling `Instant::now()`"); - - #[cfg(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64"))] - return Duration::from_nanos(_nanos); }