handle_trait_derive 1.1.0

Derive macro for the Handle trait
Documentation
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};

/// Derive macro for the Handle trait.
///
/// This macro automatically implements the `Handle` trait for types that implement `Clone`.
/// The `Handle` trait provides a `handle()` method that is semantically equivalent to `clone()`,
/// but makes the intent clearer: creating another handle to the same underlying resource.
///
/// # Examples
///
/// ```ignore
/// use handle_trait::Handle;
///
/// #[derive(Clone, Handle)]
/// struct MyHandle {
///     inner: std::sync::Arc<i32>,
/// }
///
/// let h1 = MyHandle { inner: std::sync::Arc::new(42) };
/// let h2 = h1.handle();
/// ```
///
/// # Requirements
///
/// The type must implement `Clone`. If it doesn't, you'll get a compile error.
#[proc_macro_derive(Handle)]
pub fn derive_handle(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);

    let name = input.ident;
    let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();

    let expanded = quote! {
        // Use the trait from wherever it was imported
        impl #impl_generics Handle for #name #ty_generics #where_clause {}
    };

    TokenStream::from(expanded)
}