expression
Gate an atomic counter on target capabilities
Not every embedded target provides the same atomic operations. Use a compile-time capability check before importing AtomicU32. This no_std library exposes a relaxed count on supporting targets and explicitly reports absence otherwise. Relaxed ordering is appropriate for an independent count only, not for publishing related data. The host harness expects a common 32-bit-atomic-capable desktop; fallback compilation is a separate target check. Do not silently substitute volatile access. Expected host stdout (not executed): Some(2) Common mistake: Assuming every target supports AtomicU32 or emulating missing atomic operations with volatile.
Formal Syntax & Structure
rust syntaxrust
#[cfg(target_has_atomic = "32")]Working Example
Example Snippetrust
#![no_std]#[cfg(target_has_atomic = "32")]pub fn count_two() -> Option<u32> { use core::sync::atomic::{AtomicU32, Ordering}; let counter = AtomicU32::new(0); counter.fetch_add(1, Ordering::Relaxed); counter.fetch_add(1, Ordering::Relaxed); Some(counter.load(Ordering::Relaxed))}#[cfg(not(target_has_atomic = "32"))]pub fn count_two() -> Option<u32> { None }