| David Tolnay | fedc63b | 2021-03-27 00:19:13 -0400 | [diff] [blame] | 1 | use crate::syntax::instantiate::NamedImplKey; |
| David Tolnay | 1e5fe23 | 2021-01-01 18:11:40 -0800 | [diff] [blame] | 2 | use crate::syntax::{Lifetimes, NamedType, Pair, Types}; |
| David Tolnay | 3abed47 | 2020-12-31 23:34:53 -0800 | [diff] [blame] | 3 | use proc_macro2::Ident; |
| 4 | |
| David Tolnay | 1e5fe23 | 2021-01-01 18:11:40 -0800 | [diff] [blame] | 5 | #[derive(Copy, Clone)] |
| 6 | pub struct Resolution<'a> { |
| 7 | pub name: &'a Pair, |
| 8 | pub generics: &'a Lifetimes, |
| 9 | } |
| 10 | |
| David Tolnay | 3abed47 | 2020-12-31 23:34:53 -0800 | [diff] [blame] | 11 | impl<'a> Types<'a> { |
| David Tolnay | 1e5fe23 | 2021-01-01 18:11:40 -0800 | [diff] [blame] | 12 | pub fn resolve(&self, ident: &impl UnresolvedName) -> Resolution<'a> { |
| David Tolnay | 5f5602c | 2021-03-28 03:58:06 -0400 | [diff] [blame] | 13 | let ident = ident.ident(); |
| David Tolnay | 06ef96f | 2021-03-28 04:06:48 -0400 | [diff] [blame] | 14 | match self.try_resolve(ident) { |
| 15 | Some(resolution) => resolution, |
| David Tolnay | 5f5602c | 2021-03-28 03:58:06 -0400 | [diff] [blame] | 16 | None => panic!("Unable to resolve type `{}`", ident), |
| 17 | } |
| David Tolnay | 3abed47 | 2020-12-31 23:34:53 -0800 | [diff] [blame] | 18 | } |
| David Tolnay | 06ef96f | 2021-03-28 04:06:48 -0400 | [diff] [blame] | 19 | |
| 20 | pub fn try_resolve(&self, ident: &impl UnresolvedName) -> Option<Resolution<'a>> { |
| 21 | let ident = ident.ident(); |
| 22 | self.resolutions.get(ident).copied() |
| 23 | } |
| David Tolnay | 3abed47 | 2020-12-31 23:34:53 -0800 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | pub trait UnresolvedName { |
| 27 | fn ident(&self) -> &Ident; |
| 28 | } |
| 29 | |
| 30 | impl UnresolvedName for Ident { |
| 31 | fn ident(&self) -> &Ident { |
| 32 | self |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | impl UnresolvedName for NamedType { |
| 37 | fn ident(&self) -> &Ident { |
| 38 | &self.rust |
| 39 | } |
| 40 | } |
| David Tolnay | fedc63b | 2021-03-27 00:19:13 -0400 | [diff] [blame] | 41 | |
| 42 | impl<'a> UnresolvedName for NamedImplKey<'a> { |
| 43 | fn ident(&self) -> &Ident { |
| 44 | self.rust |
| 45 | } |
| 46 | } |