Skip to content

Commit d7b383d

Browse files
authored
zeroize: impl Zeroize for str and Box<str> (#842)
1 parent 0256fff commit d7b383d

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

zeroize/src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,14 @@ where
476476
}
477477
}
478478

479+
impl Zeroize for str {
480+
fn zeroize(&mut self) {
481+
// Safety:
482+
// A zeroized byte slice is a valid UTF-8 string.
483+
unsafe { self.as_bytes_mut().zeroize() }
484+
}
485+
}
486+
479487
/// [`PhantomData`] is always zero sized so provide a [`Zeroize`] implementation.
480488
impl<Z> Zeroize for PhantomData<Z> {
481489
fn zeroize(&mut self) {}
@@ -588,6 +596,14 @@ where
588596
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
589597
impl<Z> ZeroizeOnDrop for Box<[Z]> where Z: ZeroizeOnDrop {}
590598

599+
#[cfg(feature = "alloc")]
600+
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
601+
impl Zeroize for Box<str> {
602+
fn zeroize(&mut self) {
603+
self.as_mut().zeroize();
604+
}
605+
}
606+
591607
#[cfg(feature = "alloc")]
592608
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
593609
impl Zeroize for String {

0 commit comments

Comments
 (0)