redoubt_zero_core/
primitives.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 primitive types.
6//!
7//! This module provides `ZeroizationProbe`, `ZeroizeMetadata`, and `FastZeroizable`
8//! implementations for all Rust primitive types.
9
10/// Implements ZeroizationProbe for integer types using to_le_bytes().
11macro_rules! impl_zeroization_probe_int {
12    ($($ty:ty),* $(,)?) => {
13        $(
14            impl crate::traits::ZeroizationProbe for $ty {
15                #[inline(always)]
16                fn is_zeroized(&self) -> bool {
17                    self.to_le_bytes().iter().all(|b| *b == 0)
18                }
19            }
20        )*
21    };
22}
23
24/// Implements ZeroizationProbe for bool (false is zeroized).
25impl crate::traits::ZeroizationProbe for bool {
26    #[inline(always)]
27    fn is_zeroized(&self) -> bool {
28        !(*self)
29    }
30}
31
32/// Implements ZeroizationProbe for char (null char is zeroized).
33impl crate::traits::ZeroizationProbe for char {
34    #[inline(always)]
35    fn is_zeroized(&self) -> bool {
36        *self == '\0'
37    }
38}
39
40impl_zeroization_probe_int!(
41    u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f32, f64
42);
43
44/// Implements FastZeroizable and ZeroizeMetadata for all primitive types.
45macro_rules! impl_fast_zeroize_primitive {
46    ($($ty:ty),* $(,)?) => {
47        $(
48            impl crate::traits::ZeroizeMetadata for $ty {
49                const CAN_BE_BULK_ZEROIZED: bool = true;
50            }
51
52            impl crate::traits::FastZeroizable for $ty {
53                #[inline(always)]
54                fn fast_zeroize(&mut self) {
55                    redoubt_util::zeroize_primitive(self);
56                }
57            }
58        )*
59    };
60}
61
62impl_fast_zeroize_primitive!(
63    u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f32, f64, bool, char,
64);
65
66/// Unit type is trivially zeroized (no data to zeroize).
67impl crate::traits::ZeroizationProbe for () {
68    #[inline(always)]
69    fn is_zeroized(&self) -> bool {
70        true
71    }
72}
73
74impl crate::traits::ZeroizeMetadata for () {
75    const CAN_BE_BULK_ZEROIZED: bool = true;
76}
77
78impl crate::traits::FastZeroizable for () {
79    #[inline(always)]
80    fn fast_zeroize(&mut self) {}
81}