How to implement a procedural macro for such a enum?
#[derive(Copy, Clone, Debug, MyProcMacro)]
enum Enum<W, C, I, F> {
A(W),
B(C),
C(I),
D(F)
}
I have tried to use syn::Generics
but it does not compile and produces invalid code. This is a trait I want to implement:
pub trait MyTrait<S> {
fn change(&mut self, new_obj: S) -> bool;
}
And implementation:
#[proc_macro_derive(MyProcMacro)]
pub fn my_proc_macro(input: TokenStream) -> TokenStream {
// Construct a string representation of the type definition
let s = input.to_string();
// Parse the string representation
let ast = syn::parse_derive_input(&s).unwrap();
// Build the impl
let gen = impl_macro(&ast);
// Return the generated impl
gen.parse().unwrap()
}
fn impl_macro(ast: &syn::DeriveInput) -> Tokens {
let name = &ast.ident;
let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
quote! {
impl #impl_generics mycrate::MyTrait<#name #ty_generics> for #name #ty_generics #where_clause {
fn change(&mut self, new_obj: #name #ty_generics) -> bool {
true
}
}
}
It gives this code:
impl < W , C , I , F > mycrate :: MyTrait < Enum < W , C , I , F > > for Enum < W , C , I , F > {
fn change ( & mut self , new_obj : Enum < W , C , I , F > ) -> bool {
true
}
}
I think it should be like that:
impl MyTrait<Enum<u64, u64, u64, u64>> for Enum<u64, u64, u64, u64> {
fn change(&mut self, new_obj: Enum<u64, u64, u64, u64>) {
true
}
}
As I understand we can't obtain information about needed types from procedural macro context, am I correct? I guess that is why I could not find such information in syn
crate.
If I leave the code I wrote untouched I get this error:
error[E0382]: use of moved value: `new_obj`
--> src/main.rs:28:30
|
28 | #[derive(Copy, Clone, Debug, MyProcMacro)]
| ^^^^^^^^^^^ value moved here in previous iteration of loop
|
= note: move occurs because `new_obj` has type `Enum<W, C, I, F>`, which does not implement the `Copy` trait
The error looks odd to me because this enum
definitely derives Copy
trait.
UPD:
Based on @Matthieu M.'s comment I was able to compile it successfully by adding Copy
requirement to each enum type:
enum CupState<W: Copy, C: Copy, I: Copy, F: Copy> { ... }
However, I am still looking for a better solution which does not require user code manipulations.
If you require the self-type of the derive to implement Copy
, you can have the derive macro add a bound T: Copy
to every type parameter in the generated impl block.
extern crate proc_macro;
use self::proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, parse_quote, DeriveInput, GenericParam, Generics};
#[proc_macro_derive(MyProcMacro)]
pub fn my_proc_macro(input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as DeriveInput);
let name = &ast.ident;
let generics = add_trait_bounds(ast.generics);
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
TokenStream::from(quote! {
impl #impl_generics MyTrait<Self> for #name #ty_generics #where_clause {
fn change(&mut self, new_obj: Self) -> bool {
true
}
}
})
}
// Add a bound `T: Copy` to every type parameter T.
fn add_trait_bounds(mut generics: Generics) -> Generics {
for param in &mut generics.params {
if let GenericParam::Type(ref mut type_param) = *param {
type_param.bounds.push(parse_quote!(Copy));
}
}
generics
}
Invoking the macro:
use my_proc_macro::MyProcMacro;
pub trait MyTrait<S> {
fn change(&mut self, new_obj: S) -> bool;
}
#[derive(Copy, Clone, Debug, MyProcMacro)]
enum Enum<W, C, I, F> {
A(W),
B(C),
C(I),
D(F),
}
Using cargo expand we can confirm that the generated code has Copy
bounds:
impl<W: Copy, C: Copy, I: Copy, F: Copy> MyTrait<Self> for Enum<W, C, I, F> {
fn change(&mut self, new_obj: Self) -> bool {
true
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.