redoubt_zero_core/
atomics.rs

1// Copyright (c) 2025-2026 Federico Hoerth <memparanoid@gmail.com>
2// SPDX-License-Identifier: GPL-3.0-only
3// See LICENSE in the repository root for full license text.
4
5//! Trait implementations for atomic types.
6//!
7//! This module provides `ZeroizationProbe`, `ZeroizeMetadata`, and `FastZeroizable`
8//! implementations for all Rust atomic types.
9
10use core::sync::atomic::Ordering;
11
12/// Implements ZeroizationProbe, ZeroizeMetadata, and FastZeroizable for atomic integer types.
13macro_rules! impl_fast_zeroize_atomic_int {
14    ($($ty:ty => $zero:expr),* $(,)?) => {
15        $(
16            impl crate::traits::ZeroizationProbe for $ty {
17                #[inline(always)]
18                fn is_zeroized(&self) -> bool {
19                    self.load(Ordering::Relaxed) == $zero
20                }
21            }
22
23            impl crate::traits::ZeroizeMetadata for $ty {
24                const CAN_BE_BULK_ZEROIZED: bool = false;
25            }
26
27            impl crate::traits::FastZeroizable for $ty {
28                #[inline(always)]
29                fn fast_zeroize(&mut self) {
30                    self.store($zero, Ordering::Relaxed);
31                }
32            }
33        )*
34    };
35}
36
37use core::sync::atomic::{AtomicBool, AtomicI8, AtomicI16, AtomicI32, AtomicI64, AtomicIsize};
38use core::sync::atomic::{AtomicU8, AtomicU16, AtomicU32, AtomicU64, AtomicUsize};
39
40impl_fast_zeroize_atomic_int!(
41    AtomicBool => false,
42    AtomicU8 => 0,
43    AtomicU16 => 0,
44    AtomicU32 => 0,
45    AtomicU64 => 0,
46    AtomicUsize => 0,
47    AtomicI8 => 0,
48    AtomicI16 => 0,
49    AtomicI32 => 0,
50    AtomicI64 => 0,
51    AtomicIsize => 0,
52);