1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright 2016-2017 The Servo Project Developers.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> 
// This file may not be copied, modified, or distributed
// except according to those terms.

//! A crate for deriving the MallocSizeOf trait.

extern crate quote;
#[macro_use]
extern crate syn;
#[macro_use]
extern crate synstructure;

#[cfg(not(test))]
decl_derive!([MallocSizeOf, attributes(ignore_malloc_size_of, with_malloc_size_of_func)] => malloc_size_of_derive);

fn malloc_size_of_derive(s: synstructure::Structure) -> quote::Tokens {
    let match_body = s.each(|binding| {
        let ignore = binding
            .ast()
            .attrs
            .iter()
            .any(|attr| match attr.interpret_meta().unwrap() {
                syn::Meta::Word(ref ident) | syn::Meta::List(syn::MetaList { ref ident, .. })
                    if ident == "ignore_malloc_size_of" =>
                {
                    panic!(
                        "#[ignore_malloc_size_of] should have an explanation, \
                         e.g. #[ignore_malloc_size_of = \"because reasons\"]"
                    );
                }
                syn::Meta::NameValue(syn::MetaNameValue { ref ident, .. })
                    if ident == "ignore_malloc_size_of" =>
                {
                    true
                },
                _ => false,
            });
        let with_function : Option<syn::Path> = binding
            .ast()
            .attrs
            .iter()
            .filter_map(|attr| match attr.interpret_meta().unwrap() {
                syn::Meta::Word(ref ident) | syn::Meta::List(syn::MetaList { ref ident, .. })
                    if ident == "with_malloc_size_of_func" =>
                {
                    panic!(
                        "#[with_malloc_size_of_func] must have a function name as argument, \
                         e.g. #[with_malloc_size_of_func = \"util::measure_btreemap\"]"
                    );
                }
                syn::Meta::NameValue(syn::MetaNameValue { ref ident, ref lit, .. })
                    if ident == "with_malloc_size_of_func"  =>
                {
                    if let syn::Lit::Str(ref lit) = lit {
                        // try to interpret the string as path
                        let as_path = lit.parse::<syn::Path>();
                        match as_path {
                            Ok(as_path) => Some(as_path),
                            Err(_err) => {
                                panic!("The argument of #[with_malloc_size_of_func = \"...\"] must be the path to a function which is in scope.");
                            }
                        }
                    } else {
                        panic!(
                        "#[with_malloc_size_of_func] must have a function name as argument and this must be a string, \
                         e.g. #[with_malloc_size_of_func = \"util::measure_btreemap\"]"
                    );
                    }
                },
                _ => None,
            })
            .next();
        if ignore {
            None
        } else if let Some(with_function) = with_function {
            Some(quote! {
                sum += #with_function(#binding, ops);
            })
        } else if let syn::Type::Array(..) = binding.ast().ty {
            Some(quote! {
                for item in #binding.iter() {
                    sum += ::malloc_size_of::MallocSizeOf::size_of(item, ops);
                }
            })
        } else {
            Some(quote! {
                sum += ::malloc_size_of::MallocSizeOf::size_of(#binding, ops);
            })
        }
    });

    let ast = s.ast();
    let name = &ast.ident;
    let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
    let mut where_clause = where_clause.unwrap_or(&parse_quote!(where)).clone();
    for param in ast.generics.type_params() {
        let ident = param.ident;
        where_clause
            .predicates
            .push(parse_quote!(#ident: ::malloc_size_of::MallocSizeOf));
    }

    let tokens = quote! {
        impl #impl_generics ::malloc_size_of::MallocSizeOf for #name #ty_generics #where_clause {
            #[inline]
            #[allow(unused_variables, unused_mut, unreachable_code)]
            fn size_of(&self, ops: &mut ::malloc_size_of::MallocSizeOfOps) -> usize {
                let mut sum = 0;
                match *self {
                    #match_body
                }
                sum
            }
        }
    };

    tokens
}

#[test]
fn test_struct() {
    let source = syn::parse_str(
        "struct Foo<T> { bar: Bar, baz: T, #[ignore_malloc_size_of = \"\"] z: Arc<T> }",
    ).unwrap();
    let source = synstructure::Structure::new(&source);

    let expanded = malloc_size_of_derive(source).to_string();
    let mut no_space = expanded.replace(" ", "");
    macro_rules! match_count {
        ($e: expr, $count: expr) => {
            assert_eq!(
                no_space.matches(&$e.replace(" ", "")).count(),
                $count,
                "counting occurences of {:?} in {:?} (whitespace-insensitive)",
                $e,
                expanded
            )
        };
    }
    match_count!("struct", 0);
    match_count!("ignore_malloc_size_of", 0);
    match_count!("impl<T> ::malloc_size_of::MallocSizeOf for Foo<T> where T: ::malloc_size_of::MallocSizeOf {", 1);
    match_count!("sum += ::malloc_size_of::MallocSizeOf::size_of(", 2);

    let source = syn::parse_str("struct Bar([Baz; 3]);").unwrap();
    let source = synstructure::Structure::new(&source);
    let expanded = malloc_size_of_derive(source).to_string();
    no_space = expanded.replace(" ", "");
    match_count!("for item in", 1);
}

#[should_panic(expected = "should have an explanation")]
#[test]
fn test_no_reason() {
    let input = syn::parse_str("struct A { #[ignore_malloc_size_of] b: C }").unwrap();
    malloc_size_of_derive(synstructure::Structure::new(&input));
}


#[test]
fn test_with_function() {
    let source = syn::parse_str(
        "struct Foo { bar: Bar, #[with_malloc_size_of_func = \"col::anothermod::custom_func\"] baz: Baz,
        #[with_malloc_size_of_func = \"my_func\"] 
        baz2: Baz}",
    ).unwrap();

    let source = synstructure::Structure::new(&source);

    let expanded = malloc_size_of_derive(source).to_string();

    let no_space = expanded.replace(" ", "");
    macro_rules! match_count {
        ($e: expr, $count: expr) => {
            assert_eq!(
                no_space.matches(&$e.replace(" ", "")).count(),
                $count,
                "counting occurences of {:?} in {:?} (whitespace-insensitive)",
                $e,
                expanded
            )
        };
    }
    match_count!("struct", 0);
    match_count!("ignore_malloc_size_of", 0);
    match_count!("impl::malloc_size_of::MallocSizeOf for Foo {", 1);
    match_count!("sum += ::malloc_size_of::MallocSizeOf::size_of(", 1);
    match_count!("sum += col::anothermod::custom_func(", 1);
    match_count!("sum += my_func(", 1);
}