Skip to content

fix(loupe-derive) Handle struct/enum with inlined generics. #14

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 2 commits into from
Mar 23, 2021
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
10 changes: 4 additions & 6 deletions crates/loupe-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ fn derive_memory_usage_for_struct(
data: &DataStruct,
generics: &Generics,
) -> TokenStream {
let lifetimes_and_generics = &generics.params;
let where_clause = &generics.where_clause;
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

let sum = join_fold(
// Check all fields of the `struct`.
Expand Down Expand Up @@ -134,7 +133,7 @@ fn derive_memory_usage_for_struct(
// Implement the `MemoryUsage` trait for `struct_name`.
(quote! {
#[allow(dead_code)]
impl < #lifetimes_and_generics > loupe::MemoryUsage for #struct_name < #lifetimes_and_generics >
impl #impl_generics loupe::MemoryUsage for #struct_name #ty_generics
#where_clause
{
fn size_of_val(&self, visited: &mut loupe::MemoryUsageTracker) -> usize {
Expand All @@ -150,8 +149,7 @@ fn derive_memory_usage_for_enum(
data: &DataEnum,
generics: &Generics,
) -> TokenStream {
let lifetimes_and_generics = &generics.params;
let where_clause = &generics.where_clause;
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

let match_arms = join_fold(
data.variants
Expand Down Expand Up @@ -296,7 +294,7 @@ fn derive_memory_usage_for_enum(
// Implement the `MemoryUsage` trait for `enum_name`.
(quote! {
#[allow(dead_code)]
impl < #lifetimes_and_generics > loupe::MemoryUsage for #enum_name < #lifetimes_and_generics >
impl #impl_generics loupe::MemoryUsage for #enum_name #ty_generics
#where_clause
{
fn size_of_val(&self, visited: &mut loupe::MemoryUsageTracker) -> usize {
Expand Down
40 changes: 39 additions & 1 deletion crates/loupe/tests/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn test_tuple() {
}

#[test]
fn test_struct_generic() {
fn test_struct_with_generic() {
#[derive(MemoryUsage)]
struct Generic<T>
where
Expand All @@ -71,6 +71,17 @@ fn test_struct_generic() {
assert_size_of_val_eq!(16, Generic { x: 1i64, y: 2i64 });
}

#[test]
fn test_struct_with_inlined_generic() {
#[derive(MemoryUsage)]
struct Generic<T: MemoryUsage> {
x: T,
y: T,
}

assert_size_of_val_eq!(16, Generic { x: 1i64, y: 2i64 });
}

#[test]
fn test_struct_empty() {
#[derive(MemoryUsage)]
Expand Down Expand Up @@ -128,6 +139,33 @@ fn test_enum() {
);
}

#[test]
fn test_enum_with_generic() {
#[derive(MemoryUsage)]
enum Generic<T>
where
T: MemoryUsage,
{
A(T),
B(T),
}

assert_size_of_val_eq!(16, Generic::<i64>::A(1));
assert_size_of_val_eq!(16, Generic::<i64>::B(2));
}

#[test]
fn test_enum_with_inlined_generic() {
#[derive(MemoryUsage)]
enum Generic<T: MemoryUsage> {
A(T),
B(T),
}

assert_size_of_val_eq!(16, Generic::<i64>::A(1));
assert_size_of_val_eq!(16, Generic::<i64>::B(2));
}

#[test]
fn test_enum_variant_ignored() {
#[derive(MemoryUsage)]
Expand Down