blob: 09169cb3dd9d8c64b43a18a536fded21dbfec890 [file] [log] [blame]
David Tolnay75ea17c2020-12-06 21:08:34 -08001use crate::syntax::{Namespace, Pair, RustName, Symbol, Types};
David Tolnay60e7aa62020-11-01 12:34:51 -08002use proc_macro2::{Ident, Span};
David Tolnay8faec772020-11-02 00:18:19 -08003use std::iter;
David Tolnay60e7aa62020-11-01 12:34:51 -08004
5impl Pair {
David Tolnay20cc2d32020-12-21 15:01:07 -08006 pub fn new(namespace: Namespace, cxx: Ident, rust: Ident) -> Self {
David Tolnay7cd9fec2020-12-21 15:03:18 -08007 Pair {
David Tolnay8faec772020-11-02 00:18:19 -08008 namespace,
David Tolnay20cc2d32020-12-21 15:01:07 -08009 cxx,
10 rust,
David Tolnay60e7aa62020-11-01 12:34:51 -080011 }
12 }
David Tolnay8faec772020-11-02 00:18:19 -080013
14 pub fn to_symbol(&self) -> Symbol {
15 Symbol::from_idents(self.iter_all_segments())
16 }
17
18 pub fn to_fully_qualified(&self) -> String {
David Tolnaydee9fa32020-12-21 14:40:50 -080019 let mut fully_qualified = String::new();
20 for segment in self.iter_all_segments() {
21 fully_qualified += "::";
22 fully_qualified += &segment.to_string();
23 }
24 fully_qualified
David Tolnay8faec772020-11-02 00:18:19 -080025 }
26
27 fn iter_all_segments(&self) -> impl Iterator<Item = &Ident> {
28 self.namespace.iter().chain(iter::once(&self.cxx))
29 }
David Tolnay60e7aa62020-11-01 12:34:51 -080030}
31
David Tolnay75ea17c2020-12-06 21:08:34 -080032impl RustName {
David Tolnay7cd9fec2020-12-21 15:03:18 -080033 pub fn new(rust: Ident) -> Self {
34 RustName { rust }
David Tolnay60e7aa62020-11-01 12:34:51 -080035 }
36
David Tolnay7cd9fec2020-12-21 15:03:18 -080037 pub fn from_ref(rust: &Ident) -> &Self {
38 unsafe { &*(rust as *const Ident as *const Self) }
David Tolnay5804bb72020-12-06 21:06:15 -080039 }
40
David Tolnay60e7aa62020-11-01 12:34:51 -080041 pub fn span(&self) -> Span {
42 self.rust.span()
43 }
44
45 pub fn to_symbol(&self, types: &Types) -> Symbol {
46 types.resolve(self).to_symbol()
47 }
48}