-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f85163
commit 8ce61c1
Showing
13 changed files
with
232 additions
and
56 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "occupied-capacity-derive" | ||
version = "0.5.0-pre" | ||
authors = ["Nervos Core Dev <[email protected]>"] | ||
edition = "2018" | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
proc-macro2 = "0.4" | ||
quote = "0.6" | ||
syn = "0.15" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
extern crate proc_macro; | ||
|
||
use proc_macro2::TokenStream; | ||
use quote::{quote, quote_spanned}; | ||
use syn::spanned::Spanned; | ||
use syn::{ | ||
parse_macro_input, parse_quote, Data, DeriveInput, Fields, GenericParam, Generics, Index, | ||
}; | ||
|
||
#[proc_macro_derive(OccupiedCapacity)] | ||
pub fn derive_occupied_capacity(input: proc_macro::TokenStream) -> proc_macro::TokenStream { | ||
// Parse the input tokens into a syntax tree. | ||
let input = parse_macro_input!(input as DeriveInput); | ||
|
||
// Used in the quasi-quotation below as `#name`. | ||
let name = input.ident; | ||
|
||
// Add a bound `T: OccupiedCapacity` to every type parameter T. | ||
let generics = add_trait_bounds(input.generics); | ||
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); | ||
|
||
let sum = occupied_capacity_sum(&input.data); | ||
|
||
let expanded = quote! { | ||
// The generated impl. | ||
impl #impl_generics ::occupied_capacity::OccupiedCapacity for #name #ty_generics #where_clause { | ||
fn occupied_capacity(&self) -> usize { | ||
#sum | ||
} | ||
} | ||
}; | ||
|
||
// Hand the output tokens back to the compiler. | ||
proc_macro::TokenStream::from(expanded) | ||
} | ||
|
||
// Add a bound `T: OccupiedCapacity` to every type parameter T. | ||
fn add_trait_bounds(mut generics: Generics) -> Generics { | ||
for param in &mut generics.params { | ||
if let GenericParam::Type(ref mut type_param) = *param { | ||
type_param | ||
.bounds | ||
.push(parse_quote!(::occupied_capacity::OccupiedCapacity)); | ||
} | ||
} | ||
generics | ||
} | ||
|
||
// Generate an expression to sum up the heap size of each field. | ||
fn occupied_capacity_sum(data: &Data) -> TokenStream { | ||
match *data { | ||
Data::Struct(ref data) => { | ||
match data.fields { | ||
Fields::Named(ref fields) => { | ||
// Expands to an expression like | ||
// | ||
// 0 + self.x.occupied_capacity() + self.y.occupied_capacity() + self.z.occupied_capacity() | ||
// | ||
// but using fully qualified function call syntax. | ||
// | ||
// We take some care to use the span of each `syn::Field` as | ||
// the span of the corresponding `occupied_capacity` | ||
// call. This way if one of the field types does not | ||
// implement `OccupiedCapacity` then the compiler's error message | ||
// underlines which field it is. | ||
let recurse = fields.named.iter().map(|f| { | ||
let name = &f.ident; | ||
quote_spanned! {f.span()=> | ||
::occupied_capacity::OccupiedCapacity::occupied_capacity(&self.#name) | ||
} | ||
}); | ||
quote! { | ||
0 #(+ #recurse)* | ||
} | ||
} | ||
Fields::Unnamed(ref fields) => { | ||
// Expands to an expression like | ||
// | ||
// 0 + self.0.occupied_capacity() + self.1.occupied_capacity() + self.2.occupied_capacity() | ||
let recurse = fields.unnamed.iter().enumerate().map(|(i, f)| { | ||
let index = Index::from(i); | ||
quote_spanned! {f.span()=> | ||
::occupied_capacity::OccupiedCapacity::occupied_capacity(&self.#index) | ||
} | ||
}); | ||
quote! { | ||
0 #(+ #recurse)* | ||
} | ||
} | ||
Fields::Unit => { | ||
// Unit structs cannot own more than 0 bytes of heap memory. | ||
quote!(0) | ||
} | ||
} | ||
} | ||
Data::Enum(_) | Data::Union(_) => unimplemented!(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "occupied-capacity" | ||
version = "0.5.0-pre" | ||
authors = ["Nervos Core Dev <[email protected]>"] | ||
edition = "2018" | ||
|
||
[dependencies] | ||
numext-fixed-hash = { version = "0.1", features = ["support_rand", "support_heapsize", "support_serde"] } | ||
occupied-capacity-derive = { path = "../occupied-capacity-derive" } |
Oops, something went wrong.