pub struct ZeroDrop<T>(/* private fields */)
where
T: Copy;Expand description
Zeroing drop wrapper type for Copy type.
Assuming T: Copy, a ZeroDrop<T> wraps a Box<T>
and zeros it when dropped. We must use Box because
LLVM moves data on the stack willy nilly.
let p : *const [u8; 32];
let s = zerodrop::ZeroDrop::new_copy(&[3u8; 32]);
p = &*s;
std::mem::drop(s);
unsafe { assert_eq!(*p,[0u8; 32]); }We recommend abstracting usage of ZeroDrop as follows because
ZeroDrop does not mlock data.
ⓘ
type Secret<T> = ZeroDrop<T> where T: Copy+Default;We similarly encurage wrapping ZeroDrop yourself so as to limit
where and how secret data can be used in your code, including avoiding
any trait magic that seems overly subtle.
ⓘ
struct MySecret(pub ZeroDrop<[u8; 32]>);Implementations§
Source§impl<T> ZeroDrop<T>where
T: Copy,
Create a ZeroDrop<T> for a T: Copy consisting of a Box<T>
that will be zeroed when dropped.
impl<T> ZeroDrop<T>where
T: Copy,
Create a ZeroDrop<T> for a T: Copy consisting of a Box<T>
that will be zeroed when dropped.
Sourcepub fn new_insecure(t: T) -> ZeroDrop<T>
pub fn new_insecure(t: T) -> ZeroDrop<T>
Insecure as t likely gets placed on the stack
Sourcepub unsafe fn new_uninitialized() -> ZeroDrop<T>
pub unsafe fn new_uninitialized() -> ZeroDrop<T>
Secure but unsafe
pub unsafe fn zero_out(&mut self)
pub fn new_zeroed() -> ZeroDrop<T>
Trait Implementations§
Source§impl<T> BorrowMut<T> for ZeroDrop<T>where
T: Copy,
Delegate BorrowMut<_> to Box
impl<T> BorrowMut<T> for ZeroDrop<T>where
T: Copy,
Delegate BorrowMut<_> to Box
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Auto Trait Implementations§
impl<T> Freeze for ZeroDrop<T>
impl<T> RefUnwindSafe for ZeroDrop<T>where
T: RefUnwindSafe,
impl<T> Send for ZeroDrop<T>where
T: Send,
impl<T> Sync for ZeroDrop<T>where
T: Sync,
impl<T> Unpin for ZeroDrop<T>
impl<T> UnwindSafe for ZeroDrop<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more