Skip to content

Add support for generics #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Unreleased

- Fixed: Structs with generics are now supported (https://github.com/heroku-buildpacks/cache_diff/pull/12)
- Fixed: Use fully qulified path to `::std::vec::Vec` (https://github.com/heroku-buildpacks/cache_diff/pull/8)

## 1.1.0
Expand Down
4 changes: 4 additions & 0 deletions cache_diff_derive/src/cache_diff_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ use syn::{DataStruct, FieldsNamed, Ident};
pub(crate) struct CacheDiffContainer {
/// The identifier of a struct e.g. `struct Metadata {version: String}` would be `Metadata`
pub(crate) identifier: Ident,
/// Info about generics, lifetimes and where clauses i.e. `struct Metadata<T> { name: T }`
pub(crate) generics: syn::Generics,
/// An optional path to a custom diff function
pub(crate) custom: Option<syn::Path>, // #[cache_diff(custom = <function>)]
/// One or more named fields
Expand All @@ -44,6 +46,7 @@ pub(crate) struct CacheDiffContainer {
impl CacheDiffContainer {
pub(crate) fn from_ast(input: &syn::DeriveInput) -> syn::Result<Self> {
let identifier = input.ident.clone();
let generics = input.generics.clone();
let mut container_custom = None;

for attribute in input
Expand Down Expand Up @@ -93,6 +96,7 @@ impl CacheDiffContainer {
} else {
Ok(CacheDiffContainer {
identifier,
generics,
custom: container_custom,
fields,
})
Expand Down
6 changes: 3 additions & 3 deletions cache_diff_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn cache_diff(item: TokenStream) -> TokenStream {
fn create_cache_diff(item: proc_macro2::TokenStream) -> syn::Result<proc_macro2::TokenStream> {
let ast: DeriveInput = syn::parse2(item).unwrap();
let container = CacheDiffContainer::from_ast(&ast)?;
let struct_identifier = &container.identifier;
let ident = &container.identifier;

let custom_diff = if let Some(ref custom_fn) = container.custom {
quote::quote! {
Expand Down Expand Up @@ -48,9 +48,9 @@ fn create_cache_diff(item: proc_macro2::TokenStream) -> syn::Result<proc_macro2:
}
});
}

let (impl_generics, type_generics, where_clause) = container.generics.split_for_impl();
Ok(quote::quote! {
impl cache_diff::CacheDiff for #struct_identifier {
impl #impl_generics ::cache_diff::CacheDiff for #ident #type_generics #where_clause {
fn diff(&self, old: &Self) -> ::std::vec::Vec<String> {
let mut differences = ::std::vec::Vec::new();
#custom_diff
Expand Down
19 changes: 19 additions & 0 deletions usage/tests/fails/generic_missing_bounds.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use cache_diff::CacheDiff;

#[derive(CacheDiff)]
struct Example<T> {
name: String,
other: T,
}

fn main() {
let now = Example::<String> {
name: "Richard".to_string(),
other: "John Jacob Jingleheimer Schmidt (his name is my name too)".to_string(),
};

let _ = now.diff(&Example::<String> {
name: "Richard".to_string(),
other: "schneems".to_string(),
});
}
30 changes: 30 additions & 0 deletions usage/tests/fails/generic_missing_bounds.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
error[E0369]: binary operation `!=` cannot be applied to type `T`
--> tests/fails/generic_missing_bounds.rs:3:10
|
3 | #[derive(CacheDiff)]
| ^^^^^^^^^
|
= note: this error originates in the derive macro `CacheDiff` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider restricting type parameter `T` with trait `PartialEq`
|
4 | struct Example<T: std::cmp::PartialEq> {
| +++++++++++++++++++++

error[E0277]: `T` doesn't implement `std::fmt::Display`
--> tests/fails/generic_missing_bounds.rs:3:10
|
3 | #[derive(CacheDiff)]
| ^^^^^^^^^ `T` cannot be formatted with the default formatter
|
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
= note: required for `&T` to implement `std::fmt::Display`
note: required by a bound in `fmt_value`
--> $WORKSPACE/cache_diff/src/lib.rs
|
| fn fmt_value<T: std::fmt::Display>(&self, value: &T) -> String {
| ^^^^^^^^^^^^^^^^^ required by this bound in `CacheDiff::fmt_value`
= note: this error originates in the derive macro `CacheDiff` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider restricting type parameter `T` with trait `Display`
|
4 | struct Example<T: std::fmt::Display> {
| +++++++++++++++++++
42 changes: 42 additions & 0 deletions usage/tests/pass/struct_with_generics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use cache_diff::CacheDiff;

#[derive(CacheDiff)]
struct Example<T>
where
T: std::fmt::Display + Eq,
{
name: String,
other: T,
}

#[derive(CacheDiff)]
struct ExampleToo<T, V>
where
T: std::fmt::Display + Eq,
V: std::fmt::Display + PartialEq,
{
one: T,
two: V,
}

fn main() {
let now = Example::<String> {
name: "Richard".to_string(),
other: "John Jacob Jingleheimer Schmidt (his name is my name too)".to_string(),
};

let _ = now.diff(&Example::<String> {
name: "Richard".to_string(),
other: "schneems".to_string(),
});

let new = ExampleToo::<String, String> {
one: "One".to_string(),
two: "Two".to_string(),
};

let _ = new.diff(&ExampleToo::<String, String> {
one: "Won".to_string(),
two: "Too".to_string(),
});
}
Loading