Skip to content

Commit aada846

Browse files
committed
Make the discord_emoji proc macro more succinct
1 parent 2873033 commit aada846

1 file changed

Lines changed: 8 additions & 12 deletions

File tree

  • serenity_discord_bot_derive/src

serenity_discord_bot_derive/src/lib.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,29 +42,26 @@ pub fn derive_discord_emoji(input: TokenStream) -> TokenStream {
4242
});
4343

4444
let name = derive_enum.ident;
45-
let variants_names = derive_enum
45+
// need to use &Vec<T> because `into_iter` takes ownership of the vector (when we can get away with just
46+
// references) and [`quote::quote_token_with_context`] binds the variable names with into_iter,
47+
// which is expanded from the fourth arm of the [`quote`] macro:
48+
//
49+
// https://docs.rs/quote/latest/src/quote/lib.rs.html#894
50+
let variants_names = &derive_enum
4651
.data
4752
.variants
4853
.iter()
4954
.map(|v| &v.ident)
5055
.collect::<Vec<_>>();
5156

52-
let get_id = {
53-
let variants_names = variants_names.clone();
57+
let get_id_and_variant = {
5458
quote! {
5559
impl #name {
5660
pub fn get_id(&self) -> &'static str {
5761
match self {
5862
#(#name::#variants_names => #variants_ids,)*
5963
}
6064
}
61-
}
62-
}
63-
};
64-
let get_variant_str = {
65-
let variants_names = variants_names.clone();
66-
quote! {
67-
impl #name {
6865
pub fn get_variant_str(&self) -> &'static str {
6966
match self {
7067
#(#name::#variants_names => {
@@ -78,8 +75,7 @@ pub fn derive_discord_emoji(input: TokenStream) -> TokenStream {
7875

7976
quote! {
8077
#display_impl
81-
#get_id
82-
#get_variant_str
78+
#get_id_and_variant
8379
}
8480
.into()
8581
}

0 commit comments

Comments
 (0)