bevy/crates/bevy_utils/src/once.rs
Carter Anderson e9a0ef49f9
Rename bevy_platform_support to bevy_platform (#18813)
# Objective

The goal of `bevy_platform_support` is to provide a set of platform
agnostic APIs, alongside platform-specific functionality. This is a high
traffic crate (providing things like HashMap and Instant). Especially in
light of https://github.com/bevyengine/bevy/discussions/18799, it
deserves a friendlier / shorter name.

Given that it hasn't had a full release yet, getting this change in
before Bevy 0.16 makes sense.

## Solution

- Rename `bevy_platform_support` to `bevy_platform`.
2025-04-11 23:13:28 +00:00

36 lines
869 B
Rust

use bevy_platform::sync::atomic::{AtomicBool, Ordering};
/// Wrapper around an [`AtomicBool`], abstracting the backing implementation and
/// ordering considerations.
#[doc(hidden)]
pub struct OnceFlag(AtomicBool);
impl OnceFlag {
/// Create a new flag in the unset state.
pub const fn new() -> Self {
Self(AtomicBool::new(true))
}
/// Sets this flag. Will return `true` if this flag hasn't been set before.
pub fn set(&self) -> bool {
self.0.swap(false, Ordering::Relaxed)
}
}
impl Default for OnceFlag {
fn default() -> Self {
Self::new()
}
}
/// Call some expression only once per call site.
#[macro_export]
macro_rules! once {
($expression:expr) => {{
static SHOULD_FIRE: $crate::OnceFlag = $crate::OnceFlag::new();
if SHOULD_FIRE.set() {
$expression;
}
}};
}