aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_ty/src/utils.rs
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2020-01-31 15:52:43 +0000
committerFlorian Diebold <[email protected]>2020-02-07 17:28:10 +0000
commited25cf70d5e0df9c7a33deb503ea14c2d97bd7a7 (patch)
treebc6e8620244d64beed8f1a9ce9059e5dfd2a0fbe /crates/ra_hir_ty/src/utils.rs
parentf8b7b64bce772f21124b4790538ca97418cc23ca (diff)
Change Ty::Param to contain param ID
Diffstat (limited to 'crates/ra_hir_ty/src/utils.rs')
-rw-r--r--crates/ra_hir_ty/src/utils.rs30
1 files changed, 11 insertions, 19 deletions
diff --git a/crates/ra_hir_ty/src/utils.rs b/crates/ra_hir_ty/src/utils.rs
index 77b7de729..8fa1838bd 100644
--- a/crates/ra_hir_ty/src/utils.rs
+++ b/crates/ra_hir_ty/src/utils.rs
@@ -99,23 +99,19 @@ pub(crate) struct Generics {
99} 99}
100 100
101impl Generics { 101impl Generics {
102 pub(crate) fn iter<'a>(&'a self) -> impl Iterator<Item = (u32, &'a TypeParamData)> + 'a { 102 pub(crate) fn iter<'a>(&'a self) -> impl Iterator<Item = (TypeParamId, &'a TypeParamData)> + 'a {
103 self.parent_generics 103 self.parent_generics
104 .as_ref() 104 .as_ref()
105 .into_iter() 105 .into_iter()
106 .flat_map(|it| it.params.types.iter()) 106 .flat_map(|it| it.params.types.iter().map(move |(local_id, p)| (TypeParamId { parent: it.def, local_id }, p)))
107 .chain(self.params.types.iter()) 107 .chain(self.params.types.iter().map(move |(local_id, p)| (TypeParamId { parent: self.def, local_id }, p)))
108 .enumerate()
109 .map(|(i, (_local_id, p))| (i as u32, p))
110 } 108 }
111 109
112 pub(crate) fn iter_parent<'a>(&'a self) -> impl Iterator<Item = (u32, &'a TypeParamData)> + 'a { 110 pub(crate) fn iter_parent<'a>(&'a self) -> impl Iterator<Item = (TypeParamId, &'a TypeParamData)> + 'a {
113 self.parent_generics 111 self.parent_generics
114 .as_ref() 112 .as_ref()
115 .into_iter() 113 .into_iter()
116 .flat_map(|it| it.params.types.iter()) 114 .flat_map(|it| it.params.types.iter().map(move |(local_id, p)| (TypeParamId { parent: it.def, local_id }, p)))
117 .enumerate()
118 .map(|(i, (_local_id, p))| (i as u32, p))
119 } 115 }
120 116
121 pub(crate) fn len(&self) -> usize { 117 pub(crate) fn len(&self) -> usize {
@@ -137,16 +133,11 @@ impl Generics {
137 (self_params, list_params, impl_trait_params) 133 (self_params, list_params, impl_trait_params)
138 } 134 }
139 135
140 pub(crate) fn param_idx(&self, param: TypeParamId) -> u32 { 136 pub(crate) fn param_idx(&self, param: TypeParamId) -> Option<u32> {
141 self.find_param(param).0 137 Some(self.find_param(param)?.0)
142 } 138 }
143 139
144 pub(crate) fn param_name(&self, param: TypeParamId) -> Name { 140 fn find_param(&self, param: TypeParamId) -> Option<(u32, &TypeParamData)> {
145 // FIXME make this return Option
146 self.find_param(param).1.name.clone().unwrap_or_else(Name::missing)
147 }
148
149 fn find_param(&self, param: TypeParamId) -> (u32, &TypeParamData) {
150 if param.parent == self.def { 141 if param.parent == self.def {
151 let (idx, (_local_id, data)) = self 142 let (idx, (_local_id, data)) = self
152 .params 143 .params
@@ -156,9 +147,10 @@ impl Generics {
156 .find(|(_, (idx, _))| *idx == param.local_id) 147 .find(|(_, (idx, _))| *idx == param.local_id)
157 .unwrap(); 148 .unwrap();
158 let (_total, parent_len, _child) = self.len_split(); 149 let (_total, parent_len, _child) = self.len_split();
159 return ((parent_len + idx) as u32, data); 150 Some(((parent_len + idx) as u32, data))
151 } else {
152 self.parent_generics.as_ref().and_then(|g| g.find_param(param))
160 } 153 }
161 self.parent_generics.as_ref().unwrap().find_param(param)
162 } 154 }
163} 155}
164 156