Function zeroize_flat_type

unsafe fn zeroize_flat_type<F: Sized>(data: *mut F)

Zeroizes a flat type/struct. Only zeroizes the values that it owns, and it does not work on dynamically sized values or trait objects. It would be inefficient to use this function on a type that already implements ZeroizeOnDrop.

Safety

Incompatible data types

Some data types that cannot be safely zeroized using zeroize_flat_type include, but are not limited to:

Examples

Safe usage for a struct containing strictly flat data:

use zeroize::{ZeroizeOnDrop, zeroize_flat_type};

struct DataToZeroize {
    flat_data_1: [u8; 32],
    flat_data_2: SomeMoreFlatData,
}

struct SomeMoreFlatData(u64);

impl Drop for DataToZeroize {
    fn drop(&mut self) {
        unsafe { zeroize_flat_type(self as *mut Self) }
    }
}
impl ZeroizeOnDrop for DataToZeroize {}

let mut data = DataToZeroize {
    flat_data_1: [3u8; 32],
    flat_data_2: SomeMoreFlatData(123u64)
};

// data gets zeroized when dropped