| 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 | 8faec77 | 2020-11-02 00:18:19 -0800 | [diff] [blame] | 6 | // Use this constructor when the item can't have a different name in Rust |
| 7 | // and C++. |
| David Tolnay | d7a3a18 | 2020-11-01 20:45:14 -0800 | [diff] [blame] | 8 | pub fn new(namespace: Namespace, ident: Ident) -> Self { |
| David Tolnay | 60e7aa6 | 2020-11-01 12:34:51 -0800 | [diff] [blame] | 9 | Self { |
| David Tolnay | 8faec77 | 2020-11-02 00:18:19 -0800 | [diff] [blame] | 10 | namespace, |
| 11 | cxx: ident.clone(), |
| 12 | rust: ident, |
| David Tolnay | 60e7aa6 | 2020-11-01 12:34:51 -0800 | [diff] [blame] | 13 | } |
| 14 | } |
| 15 | |
| David Tolnay | 8faec77 | 2020-11-02 00:18:19 -0800 | [diff] [blame] | 16 | // Use this constructor when attributes such as #[rust_name] can be used to |
| 17 | // potentially give a different name in Rust vs C++. |
| David Tolnay | d7a3a18 | 2020-11-01 20:45:14 -0800 | [diff] [blame] | 18 | pub fn new_from_differing_names( |
| 19 | namespace: Namespace, |
| 20 | cxx_ident: Ident, |
| 21 | rust_ident: Ident, |
| 22 | ) -> Self { |
| David Tolnay | 60e7aa6 | 2020-11-01 12:34:51 -0800 | [diff] [blame] | 23 | Self { |
| David Tolnay | 8faec77 | 2020-11-02 00:18:19 -0800 | [diff] [blame] | 24 | namespace, |
| 25 | cxx: cxx_ident, |
| David Tolnay | 60e7aa6 | 2020-11-01 12:34:51 -0800 | [diff] [blame] | 26 | rust: rust_ident, |
| David Tolnay | 60e7aa6 | 2020-11-01 12:34:51 -0800 | [diff] [blame] | 27 | } |
| 28 | } |
| David Tolnay | 8faec77 | 2020-11-02 00:18:19 -0800 | [diff] [blame] | 29 | |
| 30 | pub fn to_symbol(&self) -> Symbol { |
| 31 | Symbol::from_idents(self.iter_all_segments()) |
| 32 | } |
| 33 | |
| 34 | pub fn to_fully_qualified(&self) -> String { |
| David Tolnay | dee9fa3 | 2020-12-21 14:40:50 -0800 | [diff] [blame^] | 35 | let mut fully_qualified = String::new(); |
| 36 | for segment in self.iter_all_segments() { |
| 37 | fully_qualified += "::"; |
| 38 | fully_qualified += &segment.to_string(); |
| 39 | } |
| 40 | fully_qualified |
| David Tolnay | 8faec77 | 2020-11-02 00:18:19 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | fn iter_all_segments(&self) -> impl Iterator<Item = &Ident> { |
| 44 | self.namespace.iter().chain(iter::once(&self.cxx)) |
| 45 | } |
| David Tolnay | 60e7aa6 | 2020-11-01 12:34:51 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| David Tolnay | 75ea17c | 2020-12-06 21:08:34 -0800 | [diff] [blame] | 48 | impl RustName { |
| David Tolnay | 60e7aa6 | 2020-11-01 12:34:51 -0800 | [diff] [blame] | 49 | pub fn new(ident: Ident) -> Self { |
| 50 | Self { rust: ident } |
| 51 | } |
| 52 | |
| David Tolnay | 5804bb7 | 2020-12-06 21:06:15 -0800 | [diff] [blame] | 53 | pub fn from_ref(ident: &Ident) -> &Self { |
| 54 | unsafe { &*(ident as *const Ident as *const Self) } |
| 55 | } |
| 56 | |
| David Tolnay | 60e7aa6 | 2020-11-01 12:34:51 -0800 | [diff] [blame] | 57 | pub fn span(&self) -> Span { |
| 58 | self.rust.span() |
| 59 | } |
| 60 | |
| 61 | pub fn to_symbol(&self, types: &Types) -> Symbol { |
| 62 | types.resolve(self).to_symbol() |
| 63 | } |
| 64 | } |