cxx_build/syntax/
instantiate.rs

1use crate::syntax::{NamedType, Ty1, Type};
2use proc_macro2::{Ident, Span};
3use std::hash::{Hash, Hasher};
4use syn::Token;
5
6#[derive(#[automatically_derived]
impl<'a> ::core::cmp::PartialEq for ImplKey<'a> {
    #[inline]
    fn eq(&self, other: &ImplKey<'a>) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (ImplKey::RustBox(__self_0), ImplKey::RustBox(__arg1_0)) =>
                    __self_0 == __arg1_0,
                (ImplKey::RustVec(__self_0), ImplKey::RustVec(__arg1_0)) =>
                    __self_0 == __arg1_0,
                (ImplKey::UniquePtr(__self_0), ImplKey::UniquePtr(__arg1_0))
                    => __self_0 == __arg1_0,
                (ImplKey::SharedPtr(__self_0), ImplKey::SharedPtr(__arg1_0))
                    => __self_0 == __arg1_0,
                (ImplKey::WeakPtr(__self_0), ImplKey::WeakPtr(__arg1_0)) =>
                    __self_0 == __arg1_0,
                (ImplKey::CxxVector(__self_0), ImplKey::CxxVector(__arg1_0))
                    => __self_0 == __arg1_0,
                _ => unsafe { ::core::intrinsics::unreachable() }
            }
    }
}PartialEq, #[automatically_derived]
impl<'a> ::core::cmp::Eq for ImplKey<'a> {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_receiver_is_total_eq(&self) -> () {
        let _: ::core::cmp::AssertParamIsEq<NamedImplKey<'a>>;
        let _: ::core::cmp::AssertParamIsEq<NamedImplKey<'a>>;
        let _: ::core::cmp::AssertParamIsEq<NamedImplKey<'a>>;
        let _: ::core::cmp::AssertParamIsEq<NamedImplKey<'a>>;
        let _: ::core::cmp::AssertParamIsEq<NamedImplKey<'a>>;
        let _: ::core::cmp::AssertParamIsEq<NamedImplKey<'a>>;
    }
}Eq, #[automatically_derived]
impl<'a> ::core::hash::Hash for ImplKey<'a> {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        ::core::hash::Hash::hash(&__self_discr, state);
        match self {
            ImplKey::RustBox(__self_0) =>
                ::core::hash::Hash::hash(__self_0, state),
            ImplKey::RustVec(__self_0) =>
                ::core::hash::Hash::hash(__self_0, state),
            ImplKey::UniquePtr(__self_0) =>
                ::core::hash::Hash::hash(__self_0, state),
            ImplKey::SharedPtr(__self_0) =>
                ::core::hash::Hash::hash(__self_0, state),
            ImplKey::WeakPtr(__self_0) =>
                ::core::hash::Hash::hash(__self_0, state),
            ImplKey::CxxVector(__self_0) =>
                ::core::hash::Hash::hash(__self_0, state),
        }
    }
}Hash)]
7pub(crate) enum ImplKey<'a> {
8    RustBox(NamedImplKey<'a>),
9    RustVec(NamedImplKey<'a>),
10    UniquePtr(NamedImplKey<'a>),
11    SharedPtr(NamedImplKey<'a>),
12    WeakPtr(NamedImplKey<'a>),
13    CxxVector(NamedImplKey<'a>),
14}
15
16pub(crate) struct NamedImplKey<'a> {
17    #[cfg_attr(not(proc_macro), expect(dead_code))]
18    pub begin_span: Span,
19    pub rust: &'a Ident,
20    #[cfg_attr(not(proc_macro), expect(dead_code))]
21    pub lt_token: Option<Token![<]>,
22    #[cfg_attr(not(proc_macro), expect(dead_code))]
23    pub gt_token: Option<Token![>]>,
24    #[cfg_attr(not(proc_macro), expect(dead_code))]
25    pub end_span: Span,
26}
27
28impl Type {
29    pub(crate) fn impl_key(&self) -> Option<ImplKey> {
30        if let Type::RustBox(ty) = self {
31            if let Type::Ident(ident) = &ty.inner {
32                return Some(ImplKey::RustBox(NamedImplKey::new(ty, ident)));
33            }
34        } else if let Type::RustVec(ty) = self {
35            if let Type::Ident(ident) = &ty.inner {
36                return Some(ImplKey::RustVec(NamedImplKey::new(ty, ident)));
37            }
38        } else if let Type::UniquePtr(ty) = self {
39            if let Type::Ident(ident) = &ty.inner {
40                return Some(ImplKey::UniquePtr(NamedImplKey::new(ty, ident)));
41            }
42        } else if let Type::SharedPtr(ty) = self {
43            if let Type::Ident(ident) = &ty.inner {
44                return Some(ImplKey::SharedPtr(NamedImplKey::new(ty, ident)));
45            }
46        } else if let Type::WeakPtr(ty) = self {
47            if let Type::Ident(ident) = &ty.inner {
48                return Some(ImplKey::WeakPtr(NamedImplKey::new(ty, ident)));
49            }
50        } else if let Type::CxxVector(ty) = self {
51            if let Type::Ident(ident) = &ty.inner {
52                return Some(ImplKey::CxxVector(NamedImplKey::new(ty, ident)));
53            }
54        }
55        None
56    }
57}
58
59impl<'a> PartialEq for NamedImplKey<'a> {
60    fn eq(&self, other: &Self) -> bool {
61        PartialEq::eq(self.rust, other.rust)
62    }
63}
64
65impl<'a> Eq for NamedImplKey<'a> {}
66
67impl<'a> Hash for NamedImplKey<'a> {
68    fn hash<H: Hasher>(&self, hasher: &mut H) {
69        self.rust.hash(hasher);
70    }
71}
72
73impl<'a> NamedImplKey<'a> {
74    fn new(outer: &Ty1, inner: &'a NamedType) -> Self {
75        NamedImplKey {
76            begin_span: outer.name.span(),
77            rust: &inner.rust,
78            lt_token: inner.generics.lt_token,
79            gt_token: inner.generics.gt_token,
80            end_span: outer.rangle.span,
81        }
82    }
83}