cxx_build/syntax/
instantiate.rs1use crate::syntax::{NamedType, Ty1, Type};
2use proc_macro2::{Ident, Span};
3use std::hash::{Hash, Hasher};
4use syn::Token;
5
6#[derive(PartialEq, Eq, 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}