blob: ec93c3dfe13c51c2e451f22f5b104cee1973a4b4 [file] [log] [blame]
David Tolnayc2a4c782020-12-21 15:55:31 -08001use crate::syntax::{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 Tolnay8faec772020-11-02 00:18:19 -08006 pub fn to_symbol(&self) -> Symbol {
7 Symbol::from_idents(self.iter_all_segments())
8 }
9
10 pub fn to_fully_qualified(&self) -> String {
David Tolnaydee9fa32020-12-21 14:40:50 -080011 let mut fully_qualified = String::new();
12 for segment in self.iter_all_segments() {
13 fully_qualified += "::";
14 fully_qualified += &segment.to_string();
15 }
16 fully_qualified
David Tolnay8faec772020-11-02 00:18:19 -080017 }
18
19 fn iter_all_segments(&self) -> impl Iterator<Item = &Ident> {
20 self.namespace.iter().chain(iter::once(&self.cxx))
21 }
David Tolnay60e7aa62020-11-01 12:34:51 -080022}
23
David Tolnay75ea17c2020-12-06 21:08:34 -080024impl RustName {
David Tolnay7cd9fec2020-12-21 15:03:18 -080025 pub fn new(rust: Ident) -> Self {
26 RustName { rust }
David Tolnay60e7aa62020-11-01 12:34:51 -080027 }
28
David Tolnay7cd9fec2020-12-21 15:03:18 -080029 pub fn from_ref(rust: &Ident) -> &Self {
30 unsafe { &*(rust as *const Ident as *const Self) }
David Tolnay5804bb72020-12-06 21:06:15 -080031 }
32
David Tolnay60e7aa62020-11-01 12:34:51 -080033 pub fn span(&self) -> Span {
34 self.rust.span()
35 }
36
37 pub fn to_symbol(&self, types: &Types) -> Symbol {
38 types.resolve(self).to_symbol()
39 }
40}