| David Tolnay | c2a4c78 | 2020-12-21 15:55:31 -0800 | [diff] [blame] | 1 | use crate::syntax::{Pair, RustName, Symbol, Types}; |
| David Tolnay | 60e7aa6 | 2020-11-01 12:34:51 -0800 | [diff] [blame] | 2 | use proc_macro2::{Ident, Span}; |
| David Tolnay | 8faec77 | 2020-11-02 00:18:19 -0800 | [diff] [blame] | 3 | use std::iter; |
| David Tolnay | 60e7aa6 | 2020-11-01 12:34:51 -0800 | [diff] [blame] | 4 | |
| 5 | impl Pair { |
| David Tolnay | 8faec77 | 2020-11-02 00:18:19 -0800 | [diff] [blame] | 6 | 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 Tolnay | dee9fa3 | 2020-12-21 14:40:50 -0800 | [diff] [blame] | 11 | 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 Tolnay | 8faec77 | 2020-11-02 00:18:19 -0800 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | fn iter_all_segments(&self) -> impl Iterator<Item = &Ident> { |
| 20 | self.namespace.iter().chain(iter::once(&self.cxx)) |
| 21 | } |
| David Tolnay | 60e7aa6 | 2020-11-01 12:34:51 -0800 | [diff] [blame] | 22 | } |
| 23 | |
| David Tolnay | 75ea17c | 2020-12-06 21:08:34 -0800 | [diff] [blame] | 24 | impl RustName { |
| David Tolnay | 7cd9fec | 2020-12-21 15:03:18 -0800 | [diff] [blame] | 25 | pub fn new(rust: Ident) -> Self { |
| 26 | RustName { rust } |
| David Tolnay | 60e7aa6 | 2020-11-01 12:34:51 -0800 | [diff] [blame] | 27 | } |
| 28 | |
| David Tolnay | 7cd9fec | 2020-12-21 15:03:18 -0800 | [diff] [blame] | 29 | pub fn from_ref(rust: &Ident) -> &Self { |
| 30 | unsafe { &*(rust as *const Ident as *const Self) } |
| David Tolnay | 5804bb7 | 2020-12-06 21:06:15 -0800 | [diff] [blame] | 31 | } |
| 32 | |
| David Tolnay | 60e7aa6 | 2020-11-01 12:34:51 -0800 | [diff] [blame] | 33 | 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 | } |