File: to_animated_zero.rs

package info (click to toggle)
thunderbird 1%3A60.9.0-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,339,492 kB
  • sloc: cpp: 5,457,040; ansic: 2,360,385; python: 596,167; asm: 340,963; java: 326,296; xml: 258,830; sh: 84,445; makefile: 23,705; perl: 17,317; objc: 3,768; yacc: 1,766; ada: 1,681; lex: 1,364; pascal: 1,264; cs: 879; exp: 527; php: 436; lisp: 258; ruby: 153; awk: 152; sed: 53; csh: 27
file content (65 lines) | stat: -rw-r--r-- 2,526 bytes parent folder | download | duplicates (2)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use animate::{AnimationVariantAttrs, AnimationFieldAttrs};
use cg;
use quote;
use syn;
use synstructure;

pub fn derive(input: syn::DeriveInput) -> quote::Tokens {
    let name = &input.ident;
    let trait_path = parse_quote!(values::animated::ToAnimatedZero);
    let (impl_generics, ty_generics, mut where_clause) =
        cg::trait_parts(&input, &trait_path);

    let s = synstructure::Structure::new(&input);
    let to_body = s.each_variant(|variant| {
        let attrs = cg::parse_variant_attrs::<AnimationVariantAttrs>(&variant.ast());
        if attrs.error {
            return Some(quote! { Err(()) });
        }
        let (mapped, mapped_bindings) = cg::value(variant, "mapped");
        let bindings_pairs = variant.bindings().into_iter().zip(mapped_bindings);
        let mut computations = quote!();
        computations.append_all(bindings_pairs.map(|(binding, mapped_binding)| {
            let field_attrs = cg::parse_field_attrs::<AnimationFieldAttrs>(&binding.ast());
            if field_attrs.constant {
                if cg::is_parameterized(&binding.ast().ty, &where_clause.params, None) {
                    cg::add_predicate(
                        &mut where_clause.inner,
                        cg::where_predicate(
                            binding.ast().ty.clone(),
                            &parse_quote!(std::clone::Clone),
                            None,
                        ),
                    );
                }
                quote! {
                    let #mapped_binding = ::std::clone::Clone::clone(#binding);
                }
            } else {
                where_clause.add_trait_bound(&binding.ast().ty);
                quote! {
                    let #mapped_binding =
                        ::values::animated::ToAnimatedZero::to_animated_zero(#binding)?;
                }
            }
        }));
        computations.append_all(quote! { Ok(#mapped) });
        Some(computations)
    });

    quote! {
        impl #impl_generics ::values::animated::ToAnimatedZero for #name #ty_generics #where_clause {
            #[allow(unused_variables)]
            #[inline]
            fn to_animated_zero(&self) -> Result<Self, ()> {
                match *self {
                    #to_body
                }
            }
        }
    }
}