1 use crate::syntax::{NamedType, Ty1, Type}; 2 use proc_macro2::{Ident, Span}; 3 use std::hash::{Hash, Hasher}; 4 use syn::Token; 5 6 #[derive(Copy, Clone, PartialEq, Eq, Hash)] 7 pub(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 16 #[derive(Copy, Clone)] 17 pub(crate) struct NamedImplKey<'a> { 18 #[allow(dead_code)] // only used by cxxbridge-macro, not cxx-build 19 pub begin_span: Span, 20 pub rust: &'a Ident, 21 #[allow(dead_code)] // only used by cxxbridge-macro, not cxx-build 22 pub lt_token: Option<Token![<]>, 23 #[allow(dead_code)] // only used by cxxbridge-macro, not cxx-build 24 pub gt_token: Option<Token![>]>, 25 #[allow(dead_code)] // only used by cxxbridge-macro, not cxx-build 26 pub end_span: Span, 27 } 28 29 impl Type { impl_key(&self) -> Option<ImplKey>30 pub(crate) fn impl_key(&self) -> Option<ImplKey> { 31 if let Type::RustBox(ty) = self { 32 if let Type::Ident(ident) = &ty.inner { 33 return Some(ImplKey::RustBox(NamedImplKey::new(ty, ident))); 34 } 35 } else if let Type::RustVec(ty) = self { 36 if let Type::Ident(ident) = &ty.inner { 37 return Some(ImplKey::RustVec(NamedImplKey::new(ty, ident))); 38 } 39 } else if let Type::UniquePtr(ty) = self { 40 if let Type::Ident(ident) = &ty.inner { 41 return Some(ImplKey::UniquePtr(NamedImplKey::new(ty, ident))); 42 } 43 } else if let Type::SharedPtr(ty) = self { 44 if let Type::Ident(ident) = &ty.inner { 45 return Some(ImplKey::SharedPtr(NamedImplKey::new(ty, ident))); 46 } 47 } else if let Type::WeakPtr(ty) = self { 48 if let Type::Ident(ident) = &ty.inner { 49 return Some(ImplKey::WeakPtr(NamedImplKey::new(ty, ident))); 50 } 51 } else if let Type::CxxVector(ty) = self { 52 if let Type::Ident(ident) = &ty.inner { 53 return Some(ImplKey::CxxVector(NamedImplKey::new(ty, ident))); 54 } 55 } 56 None 57 } 58 } 59 60 impl<'a> PartialEq for NamedImplKey<'a> { eq(&self, other: &Self) -> bool61 fn eq(&self, other: &Self) -> bool { 62 PartialEq::eq(self.rust, other.rust) 63 } 64 } 65 66 impl<'a> Eq for NamedImplKey<'a> {} 67 68 impl<'a> Hash for NamedImplKey<'a> { hash<H: Hasher>(&self, hasher: &mut H)69 fn hash<H: Hasher>(&self, hasher: &mut H) { 70 self.rust.hash(hasher); 71 } 72 } 73 74 impl<'a> NamedImplKey<'a> { new(outer: &Ty1, inner: &'a NamedType) -> Self75 fn new(outer: &Ty1, inner: &'a NamedType) -> Self { 76 NamedImplKey { 77 begin_span: outer.name.span(), 78 rust: &inner.rust, 79 lt_token: inner.generics.lt_token, 80 gt_token: inner.generics.gt_token, 81 end_span: outer.rangle.span, 82 } 83 } 84 } 85