aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r--crates/ra_hir/src/debug.rs33
1 files changed, 26 insertions, 7 deletions
diff --git a/crates/ra_hir/src/debug.rs b/crates/ra_hir/src/debug.rs
index 5a835741d..87f3180c3 100644
--- a/crates/ra_hir/src/debug.rs
+++ b/crates/ra_hir/src/debug.rs
@@ -1,4 +1,24 @@
1use std::{cell::Cell, fmt}; 1//! printf debugging infrastructure for rust-analyzer.
2//!
3//! When you print a hir type, like a module, using `eprintln!("{:?}", module)`,
4//! you usually get back a numeric ID, which doesn't tell you much:
5//! `Module(92)`.
6//!
7//! This module adds convenience `debug` methods to various types, which resolve
8//! the id to a human-readable location info:
9//!
10//! ```not_rust
11//! eprintln!("{:?}", module.debug(db));
12//! =>
13//! Module { name: collections, path: "liballoc/collections/mod.rs" }
14//! ```
15//!
16//! Note that to get this info, we might need to execute queries! So
17//!
18//! * don't use the `debug` methods for logging
19//! * when debugging, be aware that interference is possible.
20
21use std::fmt;
2 22
3use ra_db::{CrateId, FileId}; 23use ra_db::{CrateId, FileId};
4 24
@@ -50,15 +70,14 @@ impl<DB: HirDebugHelper> HirDebugDatabase for DB {
50 } 70 }
51} 71}
52 72
53fn debug_fn(f: impl FnOnce(&mut fmt::Formatter<'_>) -> fmt::Result) -> impl fmt::Debug { 73fn debug_fn(f: impl Fn(&mut fmt::Formatter<'_>) -> fmt::Result) -> impl fmt::Debug {
54 struct DebugFn<F>(Cell<Option<F>>); 74 struct DebugFn<F>(F);
55 75
56 impl<F: FnOnce(&mut fmt::Formatter<'_>) -> fmt::Result> fmt::Debug for DebugFn<F> { 76 impl<F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result> fmt::Debug for DebugFn<F> {
57 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { 77 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
58 let f = self.0.take().unwrap(); 78 (&self.0)(fmt)
59 f(fmt)
60 } 79 }
61 } 80 }
62 81
63 DebugFn(Cell::new(Some(f))) 82 DebugFn(f)
64} 83}