Skip to content

Commit c50906d

Browse files
DJMcNabestebank
andcommitted
Support the Default Field Values Feature
This is exposed as a version of the structures with a `WithDefault` suffix, as the simpler solution has been ruled out. Co-Authored-By: =?UTF-8?q?Esteban=20K=C3=BCber?= <esteban@kuber.com.ar>
1 parent 8cfa32a commit c50906d

File tree

18 files changed

+4244
-101
lines changed

18 files changed

+4244
-101
lines changed

examples/heapsize/heapsize_derive/src/lib.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ use proc_macro2::TokenStream;
22
use quote::{quote, quote_spanned};
33
use syn::spanned::Spanned;
44
use syn::{
5-
parse_macro_input, parse_quote, Data, DeriveInput, Fields, GenericParam, Generics, Index,
5+
parse_macro_input, parse_quote, DataWithDefault, DeriveInputWithDefault, FieldsWithDefault,
6+
GenericParam, Generics, Index,
67
};
78

89
#[proc_macro_derive(HeapSize)]
910
pub fn derive_heap_size(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
1011
// Parse the input tokens into a syntax tree.
11-
let input = parse_macro_input!(input as DeriveInput);
12+
let input = parse_macro_input!(input as DeriveInputWithDefault);
1213

1314
// Used in the quasi-quotation below as `#name`.
1415
let name = input.ident;
@@ -44,11 +45,11 @@ fn add_trait_bounds(mut generics: Generics) -> Generics {
4445
}
4546

4647
// Generate an expression to sum up the heap size of each field.
47-
fn heap_size_sum(data: &Data) -> TokenStream {
48+
fn heap_size_sum(data: &DataWithDefault) -> TokenStream {
4849
match *data {
49-
Data::Struct(ref data) => {
50+
DataWithDefault::Struct(ref data) => {
5051
match data.fields {
51-
Fields::Named(ref fields) => {
52+
FieldsWithDefault::Named(ref fields) => {
5253
// Expands to an expression like
5354
//
5455
// 0 + self.x.heap_size() + self.y.heap_size() + self.z.heap_size()
@@ -71,7 +72,7 @@ fn heap_size_sum(data: &Data) -> TokenStream {
7172
0 #(+ #recurse)*
7273
}
7374
}
74-
Fields::Unnamed(ref fields) => {
75+
FieldsWithDefault::Unnamed(ref fields) => {
7576
// Expands to an expression like
7677
//
7778
// 0 + self.0.heap_size() + self.1.heap_size() + self.2.heap_size()
@@ -85,12 +86,12 @@ fn heap_size_sum(data: &Data) -> TokenStream {
8586
0 #(+ #recurse)*
8687
}
8788
}
88-
Fields::Unit => {
89+
FieldsWithDefault::Unit => {
8990
// Unit structs cannot own more than 0 bytes of heap memory.
9091
quote!(0)
9192
}
9293
}
9394
}
94-
Data::Enum(_) | Data::Union(_) => unimplemented!(),
95+
DataWithDefault::Enum(_) | DataWithDefault::Union(_) => unimplemented!(),
9596
}
9697
}

0 commit comments

Comments
 (0)