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).