aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_ty/src/display.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-03-16 16:42:58 +0000
committerGitHub <[email protected]>2020-03-16 16:42:58 +0000
commitadcc89137d3feea8f19fad461bbde6f4bce048e5 (patch)
tree160af959553ce57fdfcbc0a6c79bafcc3611aeea /crates/ra_hir_ty/src/display.rs
parent648df02953a6ebf87a5876668eceba208687e8a7 (diff)
parent9faea2364dee4fbc9391ad233c570b70256ef002 (diff)
Merge #3584
3584: Switch to dynamic dispatch r=matklad a=matklad Benches are in https://github.com/rust-analyzer/rust-analyzer/issues/1987#issuecomment-598807185 TL;DR: * 33% faster release build * slightly worse/same perf * no changes for debug build * slightly smaller binary cc @flodiebold I genuinely don't know if it is a good idea or not. Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir_ty/src/display.rs')
-rw-r--r--crates/ra_hir_ty/src/display.rs50
1 files changed, 23 insertions, 27 deletions
diff --git a/crates/ra_hir_ty/src/display.rs b/crates/ra_hir_ty/src/display.rs
index 14e089cf4..a6ef44a31 100644
--- a/crates/ra_hir_ty/src/display.rs
+++ b/crates/ra_hir_ty/src/display.rs
@@ -9,8 +9,8 @@ use crate::{
9use hir_def::{generics::TypeParamProvenance, AdtId, AssocContainerId, Lookup}; 9use hir_def::{generics::TypeParamProvenance, AdtId, AssocContainerId, Lookup};
10use hir_expand::name::Name; 10use hir_expand::name::Name;
11 11
12pub struct HirFormatter<'a, 'b, DB> { 12pub struct HirFormatter<'a, 'b> {
13 pub db: &'a DB, 13 pub db: &'a dyn HirDatabase,
14 fmt: &'a mut fmt::Formatter<'b>, 14 fmt: &'a mut fmt::Formatter<'b>,
15 buf: String, 15 buf: String,
16 curr_size: usize, 16 curr_size: usize,
@@ -19,20 +19,20 @@ pub struct HirFormatter<'a, 'b, DB> {
19} 19}
20 20
21pub trait HirDisplay { 21pub trait HirDisplay {
22 fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result; 22 fn hir_fmt(&self, f: &mut HirFormatter) -> fmt::Result;
23 23
24 fn display<'a, DB>(&'a self, db: &'a DB) -> HirDisplayWrapper<'a, DB, Self> 24 fn display<'a>(&'a self, db: &'a dyn HirDatabase) -> HirDisplayWrapper<'a, Self>
25 where 25 where
26 Self: Sized, 26 Self: Sized,
27 { 27 {
28 HirDisplayWrapper(db, self, None, false) 28 HirDisplayWrapper(db, self, None, false)
29 } 29 }
30 30
31 fn display_truncated<'a, DB>( 31 fn display_truncated<'a>(
32 &'a self, 32 &'a self,
33 db: &'a DB, 33 db: &'a dyn HirDatabase,
34 max_size: Option<usize>, 34 max_size: Option<usize>,
35 ) -> HirDisplayWrapper<'a, DB, Self> 35 ) -> HirDisplayWrapper<'a, Self>
36 where 36 where
37 Self: Sized, 37 Self: Sized,
38 { 38 {
@@ -40,10 +40,7 @@ pub trait HirDisplay {
40 } 40 }
41} 41}
42 42
43impl<'a, 'b, DB> HirFormatter<'a, 'b, DB> 43impl<'a, 'b> HirFormatter<'a, 'b> {
44where
45 DB: HirDatabase,
46{
47 pub fn write_joined<T: HirDisplay>( 44 pub fn write_joined<T: HirDisplay>(
48 &mut self, 45 &mut self,
49 iter: impl IntoIterator<Item = T>, 46 iter: impl IntoIterator<Item = T>,
@@ -84,11 +81,10 @@ where
84 } 81 }
85} 82}
86 83
87pub struct HirDisplayWrapper<'a, DB, T>(&'a DB, &'a T, Option<usize>, bool); 84pub struct HirDisplayWrapper<'a, T>(&'a dyn HirDatabase, &'a T, Option<usize>, bool);
88 85
89impl<'a, DB, T> fmt::Display for HirDisplayWrapper<'a, DB, T> 86impl<'a, T> fmt::Display for HirDisplayWrapper<'a, T>
90where 87where
91 DB: HirDatabase,
92 T: HirDisplay, 88 T: HirDisplay,
93{ 89{
94 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 90 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -106,13 +102,13 @@ where
106const TYPE_HINT_TRUNCATION: &str = "…"; 102const TYPE_HINT_TRUNCATION: &str = "…";
107 103
108impl HirDisplay for &Ty { 104impl HirDisplay for &Ty {
109 fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result { 105 fn hir_fmt(&self, f: &mut HirFormatter) -> fmt::Result {
110 HirDisplay::hir_fmt(*self, f) 106 HirDisplay::hir_fmt(*self, f)
111 } 107 }
112} 108}
113 109
114impl HirDisplay for ApplicationTy { 110impl HirDisplay for ApplicationTy {
115 fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result { 111 fn hir_fmt(&self, f: &mut HirFormatter) -> fmt::Result {
116 if f.should_truncate() { 112 if f.should_truncate() {
117 return write!(f, "{}", TYPE_HINT_TRUNCATION); 113 return write!(f, "{}", TYPE_HINT_TRUNCATION);
118 } 114 }
@@ -178,7 +174,7 @@ impl HirDisplay for ApplicationTy {
178 } 174 }
179 } 175 }
180 if self.parameters.len() > 0 { 176 if self.parameters.len() > 0 {
181 let generics = generics(f.db, def.into()); 177 let generics = generics(f.db.upcast(), def.into());
182 let (parent_params, self_param, type_params, _impl_trait_params) = 178 let (parent_params, self_param, type_params, _impl_trait_params) =
183 generics.provenance_split(); 179 generics.provenance_split();
184 let total_len = parent_params + self_param + type_params; 180 let total_len = parent_params + self_param + type_params;
@@ -238,7 +234,7 @@ impl HirDisplay for ApplicationTy {
238 } 234 }
239 } 235 }
240 TypeCtor::AssociatedType(type_alias) => { 236 TypeCtor::AssociatedType(type_alias) => {
241 let trait_ = match type_alias.lookup(f.db).container { 237 let trait_ = match type_alias.lookup(f.db.upcast()).container {
242 AssocContainerId::TraitId(it) => it, 238 AssocContainerId::TraitId(it) => it,
243 _ => panic!("not an associated type"), 239 _ => panic!("not an associated type"),
244 }; 240 };
@@ -272,7 +268,7 @@ impl HirDisplay for ApplicationTy {
272} 268}
273 269
274impl HirDisplay for ProjectionTy { 270impl HirDisplay for ProjectionTy {
275 fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result { 271 fn hir_fmt(&self, f: &mut HirFormatter) -> fmt::Result {
276 if f.should_truncate() { 272 if f.should_truncate() {
277 return write!(f, "{}", TYPE_HINT_TRUNCATION); 273 return write!(f, "{}", TYPE_HINT_TRUNCATION);
278 } 274 }
@@ -290,7 +286,7 @@ impl HirDisplay for ProjectionTy {
290} 286}
291 287
292impl HirDisplay for Ty { 288impl HirDisplay for Ty {
293 fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result { 289 fn hir_fmt(&self, f: &mut HirFormatter) -> fmt::Result {
294 if f.should_truncate() { 290 if f.should_truncate() {
295 return write!(f, "{}", TYPE_HINT_TRUNCATION); 291 return write!(f, "{}", TYPE_HINT_TRUNCATION);
296 } 292 }
@@ -299,7 +295,7 @@ impl HirDisplay for Ty {
299 Ty::Apply(a_ty) => a_ty.hir_fmt(f)?, 295 Ty::Apply(a_ty) => a_ty.hir_fmt(f)?,
300 Ty::Projection(p_ty) => p_ty.hir_fmt(f)?, 296 Ty::Projection(p_ty) => p_ty.hir_fmt(f)?,
301 Ty::Placeholder(id) => { 297 Ty::Placeholder(id) => {
302 let generics = generics(f.db, id.parent); 298 let generics = generics(f.db.upcast(), id.parent);
303 let param_data = &generics.params.types[id.local_id]; 299 let param_data = &generics.params.types[id.local_id];
304 match param_data.provenance { 300 match param_data.provenance {
305 TypeParamProvenance::TypeParamList | TypeParamProvenance::TraitSelf => { 301 TypeParamProvenance::TypeParamList | TypeParamProvenance::TraitSelf => {
@@ -334,7 +330,7 @@ impl HirDisplay for Ty {
334 330
335fn write_bounds_like_dyn_trait( 331fn write_bounds_like_dyn_trait(
336 predicates: &[GenericPredicate], 332 predicates: &[GenericPredicate],
337 f: &mut HirFormatter<impl HirDatabase>, 333 f: &mut HirFormatter,
338) -> fmt::Result { 334) -> fmt::Result {
339 // Note: This code is written to produce nice results (i.e. 335 // Note: This code is written to produce nice results (i.e.
340 // corresponding to surface Rust) for types that can occur in 336 // corresponding to surface Rust) for types that can occur in
@@ -398,7 +394,7 @@ fn write_bounds_like_dyn_trait(
398} 394}
399 395
400impl TraitRef { 396impl TraitRef {
401 fn hir_fmt_ext(&self, f: &mut HirFormatter<impl HirDatabase>, use_as: bool) -> fmt::Result { 397 fn hir_fmt_ext(&self, f: &mut HirFormatter, use_as: bool) -> fmt::Result {
402 if f.should_truncate() { 398 if f.should_truncate() {
403 return write!(f, "{}", TYPE_HINT_TRUNCATION); 399 return write!(f, "{}", TYPE_HINT_TRUNCATION);
404 } 400 }
@@ -420,19 +416,19 @@ impl TraitRef {
420} 416}
421 417
422impl HirDisplay for TraitRef { 418impl HirDisplay for TraitRef {
423 fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result { 419 fn hir_fmt(&self, f: &mut HirFormatter) -> fmt::Result {
424 self.hir_fmt_ext(f, false) 420 self.hir_fmt_ext(f, false)
425 } 421 }
426} 422}
427 423
428impl HirDisplay for &GenericPredicate { 424impl HirDisplay for &GenericPredicate {
429 fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result { 425 fn hir_fmt(&self, f: &mut HirFormatter) -> fmt::Result {
430 HirDisplay::hir_fmt(*self, f) 426 HirDisplay::hir_fmt(*self, f)
431 } 427 }
432} 428}
433 429
434impl HirDisplay for GenericPredicate { 430impl HirDisplay for GenericPredicate {
435 fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result { 431 fn hir_fmt(&self, f: &mut HirFormatter) -> fmt::Result {
436 if f.should_truncate() { 432 if f.should_truncate() {
437 return write!(f, "{}", TYPE_HINT_TRUNCATION); 433 return write!(f, "{}", TYPE_HINT_TRUNCATION);
438 } 434 }
@@ -456,7 +452,7 @@ impl HirDisplay for GenericPredicate {
456} 452}
457 453
458impl HirDisplay for Obligation { 454impl HirDisplay for Obligation {
459 fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result { 455 fn hir_fmt(&self, f: &mut HirFormatter) -> fmt::Result {
460 match self { 456 match self {
461 Obligation::Trait(tr) => write!(f, "Implements({})", tr.display(f.db)), 457 Obligation::Trait(tr) => write!(f, "Implements({})", tr.display(f.db)),
462 Obligation::Projection(proj) => write!( 458 Obligation::Projection(proj) => write!(