# variance.rs - source
Source of the Rust file `/home/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/ghost-0.1.2/src/variance.rs`.
Keywords: rust, rustlang, rust-lang

     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

    use proc_macro2::TokenStream;
    use quote::quote;
    use syn::{Attribute, LifetimeDef, TypeParam};

    enum Variance {
        Covariant,
        Contravariant,
        Invariant,
    }

    pub trait HasVarianceAttribute {
        fn attrs(&mut self) -> &mut Vec<Attribute>;
    }

    impl HasVarianceAttribute for TypeParam {
        fn attrs(&mut self) -> &mut Vec<Attribute> {
            &mut self.attrs
        }
    }

    impl HasVarianceAttribute for LifetimeDef {
        fn attrs(&mut self) -> &mut Vec<Attribute> {
            &mut self.attrs
        }
    }

    pub fn apply(param: &mut dyn HasVarianceAttribute, base: TokenStream) -> TokenStream {
        let mut variance = Variance::Covariant;

        let attrs = param.attrs();
        *attrs = attrs
            .drain(..)
            .filter(|attr| {
                if attr.path.is_ident("contra") && attr.tokens.is_empty() {
                    variance = Variance::Contravariant;
                    false
                } else if attr.path.is_ident("invariant") && attr.tokens.is_empty() {
                    variance = Variance::Invariant;
                    false
                } else {
                    true
                }
            })
            .collect();

        let phantom = quote!(core::marker::PhantomData<#base>);
        match variance {
            Variance::Covariant => base,
            Variance::Contravariant => quote!(fn(#phantom)),
            Variance::Invariant => quote!(fn(#phantom) -> #phantom),
        }
    }