Skip to main content

Secret

Struct Secret 

Source
pub struct Secret(/* private fields */);
Expand description

A configuration value that never appears in output.

Wraps a connection string, a password, a shared access key or an API key. Both Debug and [Display] render <redacted>; the value comes out only through Secret::expose, which is deliberately awkward to type and easy to grep for.

§Why both Debug and Display

Deriving Debug on a configuration struct is the normal thing to do, and the resulting line ends up in a log eventually. Masking Debug alone is the common half-measure: it leaves format!("{secret}") and every Display-based logging macro wide open. This type masks both.

§Examples

use env_reader::Secret;

#[derive(Debug)]
struct Config {
    endpoint: String,
    access_key: Secret,
}

let config = Config {
    endpoint: "sb://example".into(),
    access_key: Secret::new("hunter2"),
};

let printed = format!("{config:?}");
assert!(printed.contains("sb://example"));
assert!(!printed.contains("hunter2"));
assert_eq!(config.access_key.expose(), "hunter2");

§PartialEq compares in ordinary time

It is derived, so a comparison is not constant-time. That is right for what this type is for — comparing two configurations, usually in a test — and wrong for checking a presented credential against a stored one. Do not use it for the latter.

Implementations§

Source§

impl Secret

Source

pub fn new(value: impl Into<String>) -> Self

Wrap a value.

§Examples
use env_reader::Secret;

assert_eq!(Secret::new("hunter2").to_string(), "<redacted>");
Source

pub fn expose(&self) -> &str

The value itself.

Named to be conspicuous at a call site and in a code review: every use of it is a place a secret leaves this type.

§Examples
use env_reader::Secret;

assert_eq!(Secret::new("hunter2").expose(), "hunter2");
Source

pub fn into_exposed(self) -> String

The value itself, consuming this.

For handing ownership to a client library that wants a String.

Source

pub fn is_empty(&self) -> bool

Whether the wrapped value is empty.

Lets a caller branch on “is this configured” without exposing it.

§Examples
use env_reader::Secret;

assert!(Secret::new("").is_empty());
assert!(!Secret::new("hunter2").is_empty());
Source

pub fn len(&self) -> usize

How long the wrapped value is, in bytes.

The one measurement this type will report. Enough to tell a truncated key from an absent one in a log line, and nothing more.

Trait Implementations§

Source§

impl Clone for Secret

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Secret

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Secret

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Secret

Source§

impl From<&str> for Secret

Source§

fn from(value: &str) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Secret

Source§

fn from(value: String) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Secret

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Secret

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.