pub struct Fixed<T>(pub T);Expand description
A zero-cost, stack-allocated wrapper for sensitive data.
Fixed<T> stores its value directly in the struct (no heap allocation).
It behaves exactly like T thanks to Deref/DerefMut, but:
- Prints as
[REDACTED]in debug output - Provides
.expose_secret()as the explicit, loud way to access the secret - Works perfectly with
fixed_alias!for beautiful type aliases
§Examples
use secure_gate::{Fixed, fixed_alias};
// Define a beautiful type alias (this is the recommended pattern)
fixed_alias!(Aes256Key, 32);
// Generate a random key and convert it directly
let raw_key = [42u8; 32]; // In real code: use rand::Rng::gen()
let key: Aes256Key = raw_key.into();
// Access the bytes
let bytes: &[u8] = key.expose_secret();
assert_eq!(bytes.len(), 32);Tuple Fields§
§0: TImplementations§
Source§impl<const N: usize> Fixed<[u8; N]>
Convert a byte slice into a fixed-size secret.
impl<const N: usize> Fixed<[u8; N]>
Convert a byte slice into a fixed-size secret.
Panics if the slice length doesn’t match exactly.
§Panics
Panics with “slice length mismatch” if bytes.len() != N.
pub fn from_slice(bytes: &[u8]) -> Self
Source§impl<T> Fixed<T>
impl<T> Fixed<T>
Sourcepub fn expose_secret(&self) -> &T
pub fn expose_secret(&self) -> &T
Access the secret value immutably.
This is the canonical way to read the secret — loud and clear.
§Examples
use secure_gate::{Fixed, fixed_alias};
fixed_alias!(Aes256Key, 32);
let key: Aes256Key = [1u8; 32].into();
let bytes: &[u8] = key.expose_secret();
assert_eq!(bytes[0], 1);Sourcepub fn expose_secret_mut(&mut self) -> &mut T
pub fn expose_secret_mut(&mut self) -> &mut T
Access the secret value mutably.
Use this for in-place operations like key derivation.
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consume the wrapper and return the inner value.
This is useful when you need to pass the secret to a function that takes ownership.
Trait Implementations§
Source§impl<const N: usize> AsMut<[u8]> for Fixed<[u8; N]>
Mutably borrow as a byte slice — e.g. for key scheduling.
impl<const N: usize> AsMut<[u8]> for Fixed<[u8; N]>
Mutably borrow as a byte slice — e.g. for key scheduling.
Source§impl<const N: usize> AsRef<[u8]> for Fixed<[u8; N]>
Borrow as a byte slice — useful for crypto APIs.
impl<const N: usize> AsRef<[u8]> for Fixed<[u8; N]>
Borrow as a byte slice — useful for crypto APIs.
Source§impl<T> Debug for Fixed<T>
All Fixed<T> values print as [REDACTED] to prevent accidental leakage.
impl<T> Debug for Fixed<T>
All Fixed<T> values print as [REDACTED] to prevent accidental leakage.
Source§impl<const N: usize> From<[u8; N]> for Fixed<[u8; N]>
Convert a raw array into a fixed-size secret.
impl<const N: usize> From<[u8; N]> for Fixed<[u8; N]>
Convert a raw array into a fixed-size secret.
This enables the beautiful let key: Aes256Key = rng.gen().into(); pattern.
Source§impl<T: Zeroize> From<Fixed<T>> for FixedZeroizing<T>
Available on crate feature zeroize only.
impl<T: Zeroize> From<Fixed<T>> for FixedZeroizing<T>
zeroize only.impl<const N: usize> Copy for Fixed<[u8; N]>
Copy is implemented for small fixed-size byte arrays.
impl<T: Zeroize> ZeroizeOnDrop for Fixed<T>
zeroize only.