Skip to main content

SecretBytes

Struct SecretBytes 

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

A secret’s bytes. Zeroised on drop, prints as [redacted], and implements neither Display, Serialize nor Clone: a secret that can be formatted, serialised or copied is a secret that ends up in a log line, a JSON body, or a buffer nobody cleared.

Each absence below is paired with the same operation on a type that does allow it, so a case that stopped compiling for the wrong reason — a renamed import, a typo — would fail its pair and be noticed.

Not Display. {} is how a value reaches a log line by accident:

ⓘ
let secret = SecretBytes::new(b"hunter2".to_vec());
println!("{secret}");
let secret = SecretBytes::new(b"hunter2".to_vec());
// `Debug` is implemented, and redacts.
assert_eq!(format!("{secret:?}"), "SecretBytes([redacted])");

Not Clone. A copy is a second buffer nobody is tracking:

ⓘ
let secret = SecretBytes::new(b"hunter2".to_vec());
let _copy = secret.clone();
let secret = SecretBytes::new(b"hunter2".to_vec());
// Moving is fine: there is still exactly one buffer.
let moved = secret;
assert_eq!(moved.expose(), b"hunter2");

Not Serialize. Serialisation is how a secret reaches a JSON body:

ⓘ
fn needs_serialize<T: serde::Serialize>(_value: &T) {}
let secret = SecretBytes::new(b"hunter2".to_vec());
needs_serialize(&secret);
fn needs_serialize<T: serde::Serialize>(_value: &T) {}
// The bound itself is satisfiable — it is `SecretBytes` that refuses it.
needs_serialize(&"an ordinary string");

Implementations§

Source§

impl SecretBytes

Source

pub fn new(bytes: Vec<u8>) -> Self

Source

pub fn expose(&self) -> &[u8] ⓘ

The bytes. Named expose so a reader has to notice.

Source

pub fn expose_str(&self) -> Result<&str, SecretsError>

The bytes as text, when the secret is one (an API key).

§Errors

SecretsError::Invalid when the value is not UTF-8.

Trait Implementations§

Source§

impl Debug for SecretBytes

Source§

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

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

impl From<&str> for SecretBytes

Source§

fn from(value: &str) -> Self

Converts to this type from the input type.

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more