Module clear_on_drop::clear [] [src]

Traits to completely overwrite a value, without leaking data.

Examples

Basic use:

#[derive(Default)]
struct MyData {
    value: u32,
}

let mut place = MyData { value: 0x01234567 };
place.clear();
assert_eq!(place.value, 0);

Showing no data is leaked:

#[derive(Default)]
struct MyData {
    value: Option<u32>,
}

let mut place = MyData { value: Some(0x41414141) };
place.clear();
assert_eq!(place.value, None);

fn as_bytes<T>(x: &T) -> &[u8] {
    unsafe {
        slice::from_raw_parts(x as *const T as *const u8, mem::size_of_val(x))
    }
}
assert!(!as_bytes(&place).contains(&0x41));

Traits

Clear

An operation to completely overwrite a value, without leaking data.

InitializableFromZeroed

A type that can be initialized to a valid value, after being set to all-bits-zero.

ZeroSafe

Unsafe trait to indicate which types are safe to set to all-bits-zero.