Skip to main content

secure_gate/macros/
dynamic_generic_alias.rs

1/// Creates a generic type alias `Name<T>` for [`Dynamic<T>`](crate::Dynamic).
2///
3/// Useful when you need a single reusable name for heap-allocated secrets of varying types.
4///
5/// *Requires feature `alloc`.*
6///
7/// # Syntax
8///
9/// ```text
10/// dynamic_generic_alias!(pub Name, "doc string"); // public with custom doc
11/// dynamic_generic_alias!(pub(crate) Name);        // crate-visible, auto-generated doc
12/// ```
13///
14/// # Examples
15///
16/// ```rust
17/// use secure_gate::{dynamic_generic_alias, RevealSecret};
18///
19/// dynamic_generic_alias!(pub SecureBox, "Generic heap-allocated secret wrapper.");
20///
21/// let key: SecureBox<Vec<u8>> = vec![0u8; 32].into();
22/// key.with_secret(|b| assert_eq!(b.len(), 32));
23/// ```
24///
25/// # Implementation Notes
26///
27/// Macro-generated generic aliases lack runtime size checks. Validate expected
28/// inner types and sizes in unit tests. As with `dynamic_alias!`, zero-sized or
29/// empty inner types are permitted; validate non-zero size in tests.
30///
31/// The inner type `T` must implement `Zeroize` — this is enforced by `Dynamic<T>`,
32/// not the macro.
33///
34/// # See also
35///
36/// - [`dynamic_alias!`](crate::dynamic_alias) — preferred when the inner type is known
37/// - [`fixed_generic_alias!`](crate::fixed_generic_alias) — stack-allocated alternative
38#[cfg(feature = "alloc")]
39#[macro_export]
40macro_rules! dynamic_generic_alias {
41    ($vis:vis $name:ident, $doc:literal) => {
42        #[doc = $doc]
43        $vis type $name<T> = $crate::Dynamic<T>;
44    };
45    ($vis:vis $name:ident) => {
46        #[doc = "Generic secure heap wrapper"]
47        $vis type $name<T> = $crate::Dynamic<T>;
48    };
49}