diff --git a/patchable-macro/Cargo.toml b/patchable-macro/Cargo.toml index d81914b..df16f71 100644 --- a/patchable-macro/Cargo.toml +++ b/patchable-macro/Cargo.toml @@ -15,8 +15,9 @@ readme.workspace = true proc-macro = true [features] -impl_from = [] serde = [] +cloneable = [] +impl_from = [] [dependencies] proc-macro2.workspace = true diff --git a/patchable-macro/src/context.rs b/patchable-macro/src/context.rs index 4b534dd..00750ff 100644 --- a/patchable-macro/src/context.rs +++ b/patchable-macro/src/context.rs @@ -19,6 +19,7 @@ use syn::{ }; pub const IS_SERDE_ENABLED: bool = cfg!(feature = "serde"); +pub const IS_CLONEABLE_ENABLED: bool = cfg!(feature = "cloneable"); static PATCHABLE: &str = "patchable"; @@ -156,17 +157,17 @@ impl<'a> MacroContext<'a> { quote! {;}, ); let patch_name = &self.patch_struct_name; - let derives = if IS_SERDE_ENABLED { - quote! { - #[derive(::core::clone::Clone, ::core::cmp::PartialEq, ::serde::Deserialize)] - } - } else { - quote! { - #[derive(::core::clone::Clone, ::core::cmp::PartialEq)] - } - }; + let mut derives_list = Vec::with_capacity(3); + if IS_CLONEABLE_ENABLED { + derives_list.push(quote! { ::core::clone::Clone }); + } + derives_list.push(quote! { ::core::cmp::PartialEq }); + if IS_SERDE_ENABLED { + derives_list.push(quote! { ::serde::Deserialize }); + } + quote! { - #derives + #[derive(#(#derives_list),*)] pub struct #patch_name #body } } @@ -341,10 +342,16 @@ impl<'a> MacroContext<'a> { let t = ¶m.ident; match self.preserved_types.get(t) { Some(TypeUsage::Patchable) => { - bounded_types.push(quote! { #t: #bound + ::core::clone::Clone }); + if IS_CLONEABLE_ENABLED { + bounded_types.push(quote! { #t: #bound + ::core::clone::Clone }); + } else { + bounded_types.push(quote! { #t: #bound }); + } } Some(TypeUsage::NotPatchable) => { - bounded_types.push(quote! { #t: ::core::clone::Clone }); + if IS_CLONEABLE_ENABLED { + bounded_types.push(quote! { #t: ::core::clone::Clone }); + } } None => {} } diff --git a/patchable/Cargo.toml b/patchable/Cargo.toml index ca29ed5..1fa3c94 100644 --- a/patchable/Cargo.toml +++ b/patchable/Cargo.toml @@ -21,8 +21,9 @@ serde_json.workspace = true anyhow.workspace = true [features] -default = ["serde"] +default = ["serde", "cloneable"] serde = ["patchable-macro/serde"] +cloneable = ["patchable-macro/cloneable"] impl_from = ["patchable-macro/impl_from"] [dev-dependencies] diff --git a/patchable/src/lib.rs b/patchable/src/lib.rs index 3fab46e..11ca72e 100644 --- a/patchable/src/lib.rs +++ b/patchable/src/lib.rs @@ -94,7 +94,7 @@ pub use patchable_macro::{Patch, Patchable, patchable_model}; /// Declares the associated patch type. pub trait Patchable: Sized { /// The type of patch associated with this structure. - type Patch: Clone; + type Patch; } /// A type that can be updated using its companion patch.