Skip to content

Commit

Permalink
chore(versionable): add automatically_derived attribute
Browse files Browse the repository at this point in the history
For the generated code
  • Loading branch information
nsarlin-zama committed Jun 28, 2024
1 parent e2089f1 commit f444677
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
2 changes: 2 additions & 0 deletions utils/tfhe-versionable-derive/src/associated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub(crate) fn generate_from_trait_impl(
) -> syn::Result<ItemImpl> {
let from_variable = Ident::new(from_variable_name, Span::call_site());
Ok(parse_quote! {
#[automatically_derived]
impl #impl_generics From<#src> for #dest #where_clause {
fn from(#from_variable: #src) -> Self {
#constructor
Expand Down Expand Up @@ -55,6 +56,7 @@ pub(crate) fn generate_try_from_trait_impl(
) -> syn::Result<ItemImpl> {
let from_variable = Ident::new(from_variable_name, Span::call_site());
Ok(parse_quote! {
#[automatically_derived]
impl #impl_generics TryFrom<#src> for #dest #where_clause {
type Error = #error;
fn try_from(#from_variable: #src) -> Result<Self, Self::Error> {
Expand Down
1 change: 1 addition & 0 deletions utils/tfhe-versionable-derive/src/dispatch_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ impl AssociatedType for DispatchType {
ident: self.ident(),
generics: self.generics()?,
variants: variants?,
attrs: vec![parse_quote! { #[automatically_derived] }],
..self.orig_type.clone()
}
.into())
Expand Down
13 changes: 12 additions & 1 deletion utils/tfhe-versionable-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ fn impl_version_trait(input: &DeriveInput) -> proc_macro2::TokenStream {
quote! {
const _: () = {
#version_types

#[automatically_derived]
#version_impl
};
}
Expand Down Expand Up @@ -116,6 +118,8 @@ pub fn derive_versions_dispatch(input: TokenStream) -> TokenStream {
quote! {
const _: () = {
#dispatch_types

#[automatically_derived]
#dispatch_impl
};
}
Expand Down Expand Up @@ -238,6 +242,7 @@ pub fn derive_versionize(input: TokenStream) -> TokenStream {
quote! {
#version_trait_impl

#[automatically_derived]
impl #versionize_impl_generics #versionize_trait for #input_ident #ty_generics
#versionize_where_clause
{
Expand All @@ -253,12 +258,13 @@ pub fn derive_versionize(input: TokenStream) -> TokenStream {
#versionize_body
}

type VersionedOwned =
type VersionedOwned =
<#dispatch_enum_path #dispatch_generics as
#dispatch_trait<#dispatch_target>>::Owned #owned_where_clause;

}

#[automatically_derived]
impl #unversionize_impl_generics #unversionize_trait for #input_ident #ty_generics
#unversionize_where_clause
{
Expand All @@ -267,6 +273,7 @@ pub fn derive_versionize(input: TokenStream) -> TokenStream {
}
}

#[automatically_derived]
impl #versionize_vec_impl_generics #versionize_vec_trait for #input_ident #ty_generics
#versionize_vec_where_clause
{
Expand All @@ -283,6 +290,7 @@ pub fn derive_versionize(input: TokenStream) -> TokenStream {
}
}

#[automatically_derived]
impl #unversionize_vec_impl_generics #unversionize_vec_trait for #input_ident #ty_generics
#unversionize_vec_where_clause {
fn unversionize_vec(versioned: Self::VersionedVec) -> Result<Vec<Self>, #unversionize_error> {
Expand Down Expand Up @@ -318,6 +326,7 @@ pub fn derive_not_versioned(input: TokenStream) -> TokenStream {
let lifetime = Lifetime::new(LIFETIME_NAME, Span::call_site());

quote! {
#[automatically_derived]
impl #impl_generics #versionize_trait for #input_ident #ty_generics #where_clause {
type Versioned<#lifetime> = &#lifetime Self;
type VersionedOwned = Self;
Expand All @@ -331,12 +340,14 @@ pub fn derive_not_versioned(input: TokenStream) -> TokenStream {
}
}

#[automatically_derived]
impl #impl_generics #unversionize_trait for #input_ident #ty_generics #where_clause {
fn unversionize(versioned: Self::VersionedOwned) -> Result<Self, #unversionize_error> {
Ok(versioned)
}
}

#[automatically_derived]
impl NotVersioned for #input_ident #ty_generics #where_clause {}

}
Expand Down
12 changes: 6 additions & 6 deletions utils/tfhe-versionable-derive/src/version_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::token::Comma;
use syn::{
parse_quote, Data, DataEnum, DataStruct, DataUnion, DeriveInput, Field, Fields, FieldsNamed,
FieldsUnnamed, Generics, Ident, Item, ItemEnum, ItemImpl, ItemStruct, ItemUnion, Lifetime,
Path, Type, Variant,
parse_quote, Attribute, Data, DataEnum, DataStruct, DataUnion, DeriveInput, Field, Fields,
FieldsNamed, FieldsUnnamed, Generics, Ident, Item, ItemEnum, ItemImpl, ItemStruct, ItemUnion,
Lifetime, Path, Type, Variant,
};

use crate::associated::{
Expand Down Expand Up @@ -230,7 +230,7 @@ impl VersionType {
fields,
ident: self.ident(),
vis: self.orig_type.vis.clone(),
attrs: Vec::new(),
attrs: vec![parse_quote! { #[automatically_derived] }],
generics: self.type_generics()?,
struct_token: stru.struct_token,
semi_token: stru.semi_token,
Expand All @@ -257,7 +257,7 @@ impl VersionType {
let versioned_enu = ItemEnum {
ident: self.ident(),
vis: self.orig_type.vis.clone(),
attrs: Vec::new(),
attrs: vec![parse_quote! { #[automatically_derived] }],
generics: self.type_generics()?,
enum_token: enu.enum_token,
brace_token: enu.brace_token,
Expand All @@ -275,7 +275,7 @@ impl VersionType {
fields,
ident: self.ident(),
vis: self.orig_type.vis.clone(),
attrs: Vec::new(),
attrs: vec![parse_quote! { #[automatically_derived] }],
generics: self.type_generics()?,
union_token: uni.union_token,
};
Expand Down

0 comments on commit f444677

Please sign in to comment.