pub struct Secret<T>(/* private fields */);Expand description
A wrapper for secret values that prevents accidental leakage.
- Debug and Display show
[REDACTED] - Zeroized on drop
- Access requires explicit
expose()call
§Type Constraints
Only use with flat data types that have no pointers (e.g. [u8; N]).
See module-level documentation for details.
Implementations§
Source§impl<T> Secret<T>
impl<T> Secret<T>
Sourcepub fn expose<R>(&self, f: impl for<'a> FnOnce(&'a T) -> R) -> R
pub fn expose<R>(&self, f: impl for<'a> FnOnce(&'a T) -> R) -> R
Exposes the secret value for read-only access within a closure.
§Note
The closure uses a higher-ranked trait bound (for<'a>) to prevent
the returned value from containing references to the secret data.
This ensures the reference cannot escape the closure scope. However,
this does not prevent copying or cloning the secret value within
the closure (e.g., secret.expose(|s| s.clone())). Callers should
avoid leaking secrets through such patterns.
Additionally, any temporaries derived from the secret (e.g.
s.as_slice()) may leave secret data on the stack that will not be
automatically zeroized. Callers should wrap such temporaries in
zeroize::Zeroizing if they contain sensitive data.
Sourcepub fn expose_unwrap(self) -> T
pub fn expose_unwrap(self) -> T
Consumes the Secret and returns the inner value, zeroizing the original memory location.
Use this when you need to transfer ownership of the secret value (e.g., for APIs that consume the value).
Trait Implementations§
impl<T: CtEq> Eq for Secret<T>
impl<T> ZeroizeOnDrop for Secret<T>
Auto Trait Implementations§
impl<T> Freeze for Secret<T>where
T: Freeze,
impl<T> RefUnwindSafe for Secret<T>where
T: RefUnwindSafe,
impl<T> Send for Secret<T>where
T: Send,
impl<T> Sync for Secret<T>where
T: Sync,
impl<T> Unpin for Secret<T>where
T: Unpin,
impl<T> UnwindSafe for Secret<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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more