Skip to main content

ExposeSecret

Trait ExposeSecret 

Source
pub trait ExposeSecret {
    type Inner: ?Sized;

    // Required methods
    fn with_secret<F, R>(&self, f: F) -> R
       where F: FnOnce(&Self::Inner) -> R;
    fn expose_secret(&self) -> &Self::Inner;
    fn len(&self) -> usize;

    // Provided method
    fn is_empty(&self) -> bool { ... }
}
Expand description

Trait for read-only access to secrets, including metadata.

§Usage

Import these traits to access secret values and their metadata ergonomically.

Import this to enable .with_secret(), .expose_secret(), .len(), and .is_empty(). For mutable access, see crate::ExposeSecretMut.

§Security Note

Prefer with_secret for scoped access to avoid accidental leaks through long-lived borrows. expose_secret is provided for cases where a direct reference is needed, but use with caution.

Required Associated Types§

Source

type Inner: ?Sized

The inner secret type being exposed.

This can be a sized type (like [u8; N]) or unsized (like str or [u8]).

Required Methods§

Source

fn with_secret<F, R>(&self, f: F) -> R
where F: FnOnce(&Self::Inner) -> R,

Provide scoped read-only access to the secret.

This is the preferred method for accessing secrets, as it prevents accidental leaks through long-lived borrows. The closure receives a reference to the inner secret and returns a value.

§Examples
use secure_gate::{Fixed, ExposeSecret};
let secret = Fixed::new([42u8; 4]);
let sum: u32 = secret.with_secret(|bytes| bytes.iter().map(|&b| b as u32).sum());
assert_eq!(sum, 42 * 4);
Source

fn expose_secret(&self) -> &Self::Inner

Expose the secret for read-only access.

§Security Warning

This returns a direct reference that can be accidentally leaked. Prefer with_secret for most use cases to ensure the secret is only accessed within a controlled scope.

Source

fn len(&self) -> usize

Returns the length of the secret.

Provided Methods§

Source

fn is_empty(&self) -> bool

Returns true if the secret is empty.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§