aboutsummaryrefslogtreecommitdiff
path: root/mingling_macros/src/lib.rs
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-08-17 04:33:54 +0800
committer魏曹先生 <1992414357@qq.com>2026-08-17 04:33:54 +0800
commit7b901b44cab92b15a673a801de6785c309b73ddd (patch)
treefce888b65fc8283107bcba4325e7ca1ef443f4e6 /mingling_macros/src/lib.rs
parentaa251efb87b561f62266628f06ad341253fbbdc5 (diff)
feat(macros): add Wrap derive macro0.5.0-rd
Add `#[derive(Wrap)]` to treat a struct as its inner type, generating `From`, `Deref`, and `DerefMut` implementations. Supports single-field structs or multi-field structs with a `#[wrap]`-marked field.
Diffstat (limited to 'mingling_macros/src/lib.rs')
-rw-r--r--mingling_macros/src/lib.rs47
1 files changed, 46 insertions, 1 deletions
diff --git a/mingling_macros/src/lib.rs b/mingling_macros/src/lib.rs
index 607ba9f..7da7db3 100644
--- a/mingling_macros/src/lib.rs
+++ b/mingling_macros/src/lib.rs
@@ -33,7 +33,7 @@ use attr::dispatcher_clap;
#[cfg(feature = "extras")]
use attr::program_setup;
use attr::{chain, help, metadata, renderer};
-use derive::{enum_tag, grouped};
+use derive::{enum_tag, grouped, wrap};
#[cfg(feature = "extras")]
use func::entry;
#[cfg(feature = "extras")]
@@ -1547,6 +1547,51 @@ pub fn r_append(input: TokenStream) -> TokenStream {
func::r_append::r_append(input)
}
+/// Derive macro for treating a struct as its inner (wrapped) type.
+///
+/// The `#[derive(Wrap)]` macro generates:
+/// - `From<Inner> for Self` — construct the wrapper from the inner value
+/// - `From<Self> for Inner` — unwrap back to the inner value (i.e. `Into<Inner>`)
+/// - `Deref` / `DerefMut` — delegate all methods to the inner value
+///
+/// # Inner field selection
+///
+/// - Tuple struct with one field → that field is the inner type
+/// - Named struct with one field → that field is the inner type
+/// - Named struct with multiple fields → mark exactly one field with `#[wrap]`;
+/// the remaining fields are initialized with `Default::default()` when
+/// constructing via `From<Inner>`
+///
+/// # Example
+///
+/// ```rust,ignore
+/// use mingling::macros::Wrap;
+///
+/// #[derive(Wrap)]
+/// struct Name(String);
+///
+/// #[derive(Wrap)]
+/// struct Greeting {
+/// name: String,
+/// }
+///
+/// #[derive(Wrap)]
+/// struct Task {
+/// #[wrap]
+/// content: String,
+/// done: bool,
+/// }
+///
+/// let name = Name::from("Mingling".to_string());
+/// // `Deref` forwards methods to the inner `String`
+/// assert_eq!(name.len(), 8);
+/// let inner: String = name.into();
+/// ```
+#[proc_macro_derive(Wrap, attributes(wrap))]
+pub fn derive_wrap(input: TokenStream) -> TokenStream {
+ wrap::derive_wrap(input)
+}
+
/// Derive macro for automatically implementing the `Grouped` trait on a struct.
///
/// The `#[derive(Grouped)]` macro: