| David Tolnay | 75ea17c | 2020-12-06 21:08:34 -0800 | [diff] [blame] | 1 | use crate::syntax::{Namespace, 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 | 20cc2d3 | 2020-12-21 15:01:07 -0800 | [diff] [blame] | 6 | pub fn new(namespace: Namespace, cxx: Ident, rust: Ident) -> Self { |
| David Tolnay | 7cd9fec | 2020-12-21 15:03:18 -0800 | [diff] [blame^] | 7 | Pair { |
| David Tolnay | 8faec77 | 2020-11-02 00:18:19 -0800 | [diff] [blame] | 8 | namespace, |
| David Tolnay | 20cc2d3 | 2020-12-21 15:01:07 -0800 | [diff] [blame] | 9 | cxx, |
| 10 | rust, |
| David Tolnay | 60e7aa6 | 2020-11-01 12:34:51 -0800 | [diff] [blame] | 11 | } |
| 12 | } |
| David Tolnay | 8faec77 | 2020-11-02 00:18:19 -0800 | [diff] [blame] | 13 | |
| 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 Tolnay | dee9fa3 | 2020-12-21 14:40:50 -0800 | [diff] [blame] | 19 | 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 Tolnay | 8faec77 | 2020-11-02 00:18:19 -0800 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | fn iter_all_segments(&self) -> impl Iterator<Item = &Ident> { |
| 28 | self.namespace.iter().chain(iter::once(&self.cxx)) |
| 29 | } |
| David Tolnay | 60e7aa6 | 2020-11-01 12:34:51 -0800 | [diff] [blame] | 30 | } |
| 31 | |
| David Tolnay | 75ea17c | 2020-12-06 21:08:34 -0800 | [diff] [blame] | 32 | impl RustName { |
| David Tolnay | 7cd9fec | 2020-12-21 15:03:18 -0800 | [diff] [blame^] | 33 | pub fn new(rust: Ident) -> Self { |
| 34 | RustName { rust } |
| David Tolnay | 60e7aa6 | 2020-11-01 12:34:51 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| David Tolnay | 7cd9fec | 2020-12-21 15:03:18 -0800 | [diff] [blame^] | 37 | pub fn from_ref(rust: &Ident) -> &Self { |
| 38 | unsafe { &*(rust as *const Ident as *const Self) } |
| David Tolnay | 5804bb7 | 2020-12-06 21:06:15 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| David Tolnay | 60e7aa6 | 2020-11-01 12:34:51 -0800 | [diff] [blame] | 41 | 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 | } |