Repository: vercel/next.js
File: turbopack/crates/turbo-tasks/src/macro_helpers.rs (Rust)

Code before:
///
/// This macro is a wrapper around `inventory::submit` that adds a `#[not(cfg(rust_analyzer))]`
/// attribute to the item. This is to avoid warnings about unused items when using Rust Analyzer.
#[doc(hidden)]
#[macro_export]
macro_rules! inventory_submit {
    ($($item:tt)*) => {
        #[cfg(not(rust_analyzer))]
        $crate::macro_helpers::inventory_submit_inner! { $($item)* }
    }
}

/// Exported so the above macro can reference it.
#[doc(hidden)]
pub use inventory::submit as inventory_submit_inner;

#[doc(hidden)]
#[macro_export]
macro_rules! debug_assert_runs_once {
    () => {
        #[cfg(debug_assertions)]
        {
            use ::std::sync::atomic::{AtomicBool, Ordering};
            static ONLY_RUN_ONCE: AtomicBool = AtomicBool::new(false);
            assert!(!AtomicBool::load(&ONLY_RUN_ONCE, Ordering::Acquire));
            AtomicBool::store(&ONLY_RUN_ONCE, true, Ordering::Release);
        }
    };
}

/// Use `type_name` to get globally unique identifier that's stable across multiple executions of
/// the same Turbopack version, potentially allowing cache sharing across platforms/architectures.
///
/// The stdlib docs explicitly recommend against using type_name to get a unique identifier, but the
/// way we're using it here seems unlikely to break. We've got runtime logic to panic if it breaks.
#[doc(hidden)]
#[macro_export]
macro_rules! global_name_for_type {
    ($item:ty) => {
        ::std::any::type_name::<$item>()
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! global_name_for_method {
    ($ty:ty, $method:ident) => {{
        // We cannot use `concat!` because `type_name` is not const, so we leak the string instead
        // to get a &'static str.
        //
        // Assumption: the code that invokes this macro is only run once (e.g. part of an

Review comment (suggestion):
```suggestion
            assert!(!AtomicBool::swap(&ONLY_RUN_ONCE, true, Ordering::AcqRel));
```

Code after:
///
/// This macro is a wrapper around `inventory::submit` that adds a `#[not(cfg(rust_analyzer))]`
/// attribute to the item. This is to avoid warnings about unused items when using Rust Analyzer.
#[doc(hidden)]
#[macro_export]
macro_rules! inventory_submit {
    ($($item:tt)*) => {
        #[cfg(not(rust_analyzer))]
        $crate::macro_helpers::inventory_submit_inner! { $($item)* }
    }
}

/// Exported so the above macro can reference it.
#[doc(hidden)]
pub use inventory::submit as inventory_submit_inner;

#[doc(hidden)]
#[macro_export]
macro_rules! debug_assert_runs_once {
    () => {
        #[cfg(debug_assertions)]
        {
            use ::std::sync::atomic::{AtomicBool, Ordering};
            static ONLY_RUN_ONCE: AtomicBool = AtomicBool::new(false);
            assert!(!AtomicBool::swap(&ONLY_RUN_ONCE, true, Ordering::AcqRel));
        }
    };
}

/// Use `type_name` to get globally unique identifier that's stable across multiple executions of
/// the same Turbopack version, potentially allowing cache sharing across platforms/architectures.
///
/// The stdlib docs explicitly recommend against using type_name to get a unique identifier, but the
/// way we're using it here seems unlikely to break. We've got runtime logic to panic if it breaks.
#[doc(hidden)]
#[macro_export]
macro_rules! global_name_for_type {
    ($item:ty) => {
        ::std::any::type_name::<$item>()
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! global_name_for_method {
    ($ty:ty, $method:ident) => {{
        // We cannot use `concat!` because `type_name` is not const, so we leak the string instead
        // to get a &'static str.
        //
        // Assumption: the code that invokes this macro is only run once (e.g. part of an
        // `inventory::submit!` callsite), so we're leaking a bounded number of strings.