Skip to content

Commit

Permalink
Simplify #[derive(Dupe)] generated code
Browse files Browse the repository at this point in the history
Summary:
Put `assert_dupe` into crate rather than generating it for each type where `Dupe` is derived.

This should make compilation tiny bit faster.

Reviewed By: JakobDegen

Differential Revision: D56122611

fbshipit-source-id: ea97858d38d3af75114ca570e6cbad41d4c80467
  • Loading branch information
stepancheg authored and facebook-github-bot committed Apr 15, 2024
1 parent 83245b3 commit 3337cc7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 19 deletions.
15 changes: 15 additions & 0 deletions gazebo/dupe/src/__macro_refs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under both the MIT license found in the
* LICENSE-MIT file in the root directory of this source tree and the Apache
* License, Version 2.0 found in the LICENSE-APACHE file in the root directory
* of this source tree.
*/

#![doc(hidden)]

use crate::Dupe;

#[inline]
pub const fn assert_dupe<T: Dupe + ?Sized>() {}
1 change: 1 addition & 0 deletions gazebo/dupe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

//! A cheap version of [`Clone`].

pub mod __macro_refs;
pub(crate) mod iter;
pub(crate) mod option;

Expand Down
14 changes: 12 additions & 2 deletions gazebo/dupe_derive/src/dupe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
* of this source tree.
*/

use proc_macro2::TokenStream;
use quote::quote;
use syn::parse_macro_input;
use syn::parse_quote;
use syn::DeriveInput;
use syn::Ident;
use syn::Type;
use syn::TypeParamBound;

use crate::util::add_trait_bounds;
use crate::util::check_each_field_impls;
use crate::util::extract_all_field_tys;

pub fn derive_dupe(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
Expand Down Expand Up @@ -49,7 +50,7 @@ fn derive_dupe_explicit(
return e.into_compile_error().into();
}
};
let check_each_field_dupe = check_each_field_impls(all_fields, parse_quote!(dupe::Dupe));
let check_each_field_dupe = check_each_field_dupe(all_fields);

let check_func_name = Ident::new(
&format!("__implicit_dupe_check_for_fields_of_{}", name),
Expand All @@ -69,3 +70,12 @@ fn derive_dupe_explicit(

gen.into()
}

fn check_each_field_dupe<'a>(tys: impl IntoIterator<Item = &'a Type>) -> TokenStream {
let tys = tys.into_iter();
quote! {
#(
dupe::__macro_refs::assert_dupe::<#tys>();
)*
}
}
17 changes: 0 additions & 17 deletions gazebo/dupe_derive/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,20 +174,3 @@ fn extract_all_field_tys_variant<'a>(data: &'a Variant) -> Box<dyn Iterator<Item
fn extract_all_field_tys_enum<'a>(data: &'a DataEnum) -> Box<dyn Iterator<Item = &'a Type> + 'a> {
Box::new(data.variants.iter().flat_map(extract_all_field_tys_variant))
}

pub(crate) fn check_each_field_impls<'a>(
iter: impl IntoIterator<Item = &'a Type>,
trait_required: Type,
) -> TokenStream {
let checks = iter.into_iter().map(|ty| {
quote! {
assert_impl_all::<#ty>();
}
});

quote! {
fn assert_impl_all<T: ?Sized + #trait_required>() {}

#(#checks)*
}
}

0 comments on commit 3337cc7

Please sign in to comment.