| 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 { |
| 35 | format!("::{}", self.join("::")) |
| 36 | } |
| 37 | |
| 38 | fn iter_all_segments(&self) -> impl Iterator<Item = &Ident> { |
| 39 | self.namespace.iter().chain(iter::once(&self.cxx)) |
| 40 | } |
| 41 | |
| 42 | fn join(&self, sep: &str) -> String { |
| 43 | self.iter_all_segments() |
| 44 | .map(|s| s.to_string()) |
| 45 | .collect::<Vec<_>>() |
| 46 | .join(sep) |
| 47 | } |
| David Tolnay | 60e7aa6 | 2020-11-01 12:34:51 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| David Tolnay | 75ea17c | 2020-12-06 21:08:34 -0800 | [diff] [blame] | 50 | impl RustName { |
| David Tolnay | 60e7aa6 | 2020-11-01 12:34:51 -0800 | [diff] [blame] | 51 | pub fn new(ident: Ident) -> Self { |
| 52 | Self { rust: ident } |
| 53 | } |
| 54 | |
| David Tolnay | 5804bb7 | 2020-12-06 21:06:15 -0800 | [diff] [blame] | 55 | pub fn from_ref(ident: &Ident) -> &Self { |
| 56 | unsafe { &*(ident as *const Ident as *const Self) } |
| 57 | } |
| 58 | |
| David Tolnay | 60e7aa6 | 2020-11-01 12:34:51 -0800 | [diff] [blame] | 59 | pub fn span(&self) -> Span { |
| 60 | self.rust.span() |
| 61 | } |
| 62 | |
| 63 | pub fn to_symbol(&self, types: &Types) -> Symbol { |
| 64 | types.resolve(self).to_symbol() |
| 65 | } |
| 66 | } |