dupe_derive/
clone.rs

1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under both the MIT license found in the
5 * LICENSE-MIT file in the root directory of this source tree and the Apache
6 * License, Version 2.0 found in the LICENSE-APACHE file in the root directory
7 * of this source tree.
8 */
9
10use quote::quote;
11use syn::parse_macro_input;
12use syn::DeriveInput;
13
14use crate::util::duplicate_impl;
15
16pub fn derive_clone_(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
17    let input = parse_macro_input!(input as DeriveInput);
18    let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
19
20    let name = &input.ident;
21    let body = duplicate_impl(&input.data, &quote! { ::std::clone::Clone::clone });
22    let gen = quote! {
23        // Clippy wants us to use Copy if we can - we prefer to be agnostic.
24        // Add unknown_lints temporarily.
25        #[allow(unknown_lints)]
26        #[allow(clippy::incorrect_clone_impl_on_copy_type)]
27        impl #impl_generics ::std::clone::Clone for #name #ty_generics #where_clause {
28            fn clone(&self) -> Self {
29                #body
30            }
31        }
32    };
33    gen.into()
34}