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
impl SecretBytes
pub fn new(bytes: Vec<u8>) -> Self
Sourcepub fn expose_str(&self) -> Result<&str, SecretsError>
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
impl Debug for SecretBytes
Auto Trait Implementations§
impl Freeze for SecretBytes
impl RefUnwindSafe for SecretBytes
impl Send for SecretBytes
impl Sync for SecretBytes
impl Unpin for SecretBytes
impl UnsafeUnpin for SecretBytes
impl UnwindSafe for SecretBytes
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
Mutably borrows from an owned value. Read more