Skip to content

Commit

Permalink
Fixed Trace derive with generics
Browse files Browse the repository at this point in the history
  • Loading branch information
ecton committed May 3, 2024
1 parent 541ab5c commit ac2d995
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
30 changes: 26 additions & 4 deletions refuse-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use attribute_derive::FromAttr;
use manyhow::manyhow;
use proc_macro2::{Span, TokenStream};
use quote::quote;
use syn::{GenericParam, Generics, Lifetime, TraitBound};

#[manyhow]
#[proc_macro_derive(MapAs, attributes(map_as))]
Expand Down Expand Up @@ -74,11 +75,12 @@ fn field_accessor(field: &syn::Field, index: usize) -> TokenStream {
fn derive_struct_trace(
syn::ItemStruct {
ident,
generics,
mut generics,
fields,
..
}: syn::ItemStruct,
) -> TokenStream {
require_trace_for_generics(&mut generics);
let (impl_gen, type_gen, where_clause) = generics.split_for_impl();

let fields_and_types = fields
Expand Down Expand Up @@ -107,7 +109,7 @@ fn derive_struct_trace(
});

quote! {
impl<#impl_gen> refuse::Trace for #ident<#type_gen> #where_clause {
impl #impl_gen refuse::Trace for #ident #type_gen #where_clause {
const MAY_CONTAIN_REFERENCES: bool = #may_contain_refs;

fn trace(&self, tracer: &mut refuse::Tracer) {
Expand All @@ -117,14 +119,34 @@ fn derive_struct_trace(
}
}

fn require_trace_for_generics(generics: &mut Generics) {
for mut pair in generics.params.pairs_mut() {
if let GenericParam::Type(t) = pair.value_mut() {
t.bounds.push(syn::TypeParamBound::Trait(
TraitBound::from_input(quote!(refuse::Trace)).unwrap(),
));
t.bounds.push(syn::TypeParamBound::Trait(
TraitBound::from_input(quote!(::std::marker::Send)).unwrap(),
));
t.bounds.push(syn::TypeParamBound::Trait(
TraitBound::from_input(quote!(::std::marker::Sync)).unwrap(),
));
t.bounds.push(syn::TypeParamBound::Lifetime(
Lifetime::from_input(quote!('static)).unwrap(),
));
}
}
}

fn derive_enum_trace(
syn::ItemEnum {
ident: enum_name,
generics,
mut generics,
variants,
..
}: syn::ItemEnum,
) -> TokenStream {
require_trace_for_generics(&mut generics);
let (impl_gen, type_gen, where_clause) = generics.split_for_impl();

let mut all_types = HashSet::new();
Expand Down Expand Up @@ -169,7 +191,7 @@ fn derive_enum_trace(
};

quote! {
impl<#impl_gen> refuse::Trace for #enum_name<#type_gen> #where_clause {
impl #impl_gen refuse::Trace for #enum_name #type_gen #where_clause {
const MAY_CONTAIN_REFERENCES: bool = #may_contain_refs;

fn trace(&self, tracer: &mut refuse::Tracer) {
Expand Down
22 changes: 0 additions & 22 deletions refuse-pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,28 +136,6 @@ fn hash_str(str: &str) -> u64 {
hasher.finish()
}

struct StoredString(Root<PooledString>);

impl Hash for StoredString {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.0.hash.hash(state);
}
}

impl Eq for StoredString {}

impl PartialEq for StoredString {
fn eq(&self, other: &Self) -> bool {
self.0.hash == other.0.hash && self.0.string == other.0.string
}
}

impl PartialEq<&'_ str> for StoredString {
fn eq(&self, other: &&'_ str) -> bool {
&*self.0.string == *other
}
}

/// A "root" reference to a garbage collected, interned string.
///
/// This type is cheap to check equality because it ensures each unique string
Expand Down

0 comments on commit ac2d995

Please sign in to comment.