tc_zeroize 0.1.0

Explicit memory erasure with volatile writes and opt-in scope guards.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//! Volatile erasure of possibly uninitialized storage.

use crate::Zeroize;
use core::mem::MaybeUninit;
use core::ptr;
use core::sync::atomic::{Ordering, compiler_fence};

impl<T> Zeroize for MaybeUninit<T> {
    fn zeroize(&mut self) {
        // SAFETY: `self` is a valid, uniquely borrowed place. `MaybeUninit<T>`
        // has no validity invariant, so an all-zero pattern is always legal for
        // it, and no destructor is skipped because the slot is never treated as
        // initialized. Volatility changes optimization semantics, not validity.
        unsafe { ptr::write_volatile(self, MaybeUninit::zeroed()) };
        compiler_fence(Ordering::SeqCst);
    }
}