dyck_derive/
lib.rs

1use proc_macro::TokenStream;
2use quote::quote;
3use syn::{parse_macro_input, DeriveInput};
4
5/// Derives the `DyckToken` trait for the given enum.
6///
7/// This derive macro automatically implements the `DyckToken` trait and its required
8/// traits (`Clone`, `Copy`, `Eq`, `PartialEq`, and `Hash`) for the enum it is applied to.
9///
10/// For usage and more information, see the main dyck crate documentation.
11#[proc_macro_derive(DyckToken)]
12pub fn derive_token(input: TokenStream) -> TokenStream {
13    let input = parse_macro_input!(input as DeriveInput);
14    let name = input.ident;
15
16    let expanded = quote! {
17        impl DyckToken for #name {}
18        impl Clone for #name {
19            fn clone(&self) -> Self {
20                *self
21            }
22        }
23        impl Copy for #name {}
24        impl Eq for #name {}
25        impl PartialEq for #name {
26            fn eq(&self, other: &Self) -> bool {
27                std::mem::discriminant(self) == std::mem::discriminant(other)
28            }
29        }
30        impl std::hash::Hash for #name {
31            fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
32                std::mem::discriminant(self).hash(state);
33            }
34        }
35    };
36
37    TokenStream::from(expanded)
38}