macro_rules! hidden_type {
    ($name:ident, $type:ty) => { ... };
}
Expand description

This is a macro that produces a hidden type from an underlying data type.

It is a thin wrapper around Hidden and retains its useful properties:

  • The data is not subject to Debug or Display output, which are masked.
  • The data can only be accessed by (immutable or mutable) reference.
  • The data zeroizes when dropped, and can also be manually zeroized.
  • Cloning is safe.

The macro is a useful way to generate a hidden type that is subject to the compiler’s type guarantees. This can be useful if you need multiple hidden types that use the same underlying data type, but shouldn’t be confused for each other.

Note that it may not be safe to dereference the hidden data if its type implements Copy. If the type does not implement Copy, you should be fine. If it does, avoid dereferencing.

// Define a hidden type with a `[u8; 32]` data type
hidden_type!(MyHiddenType, [u8; 32]);

// Instantiate using existing data
let mut example = MyHiddenType::from([1u8; 32]);

// Access the data by immutable reference
assert_eq!(example.reveal(), &[1u8; 32]);

// Access the data by mutable reference
let example_mut_ref = example.reveal_mut();
*example_mut_ref = [42u8; 32];
assert_eq!(example.reveal(), &[42u8; 32]);

// Clone the data safely
let mut example_clone = example.clone();

// Zeroize the data manually
example_clone.zeroize();
assert_eq!(example_clone.reveal(), &[0u8; 32]);