From e5a8093dd497518c177d3c22404d80da44905336 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 9 Sep 2019 12:39:59 +0300 Subject: document module --- crates/ra_hir/src/debug.rs | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) (limited to 'crates/ra_hir/src/debug.rs') 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 @@ -use std::{cell::Cell, fmt}; +//! printf debugging infrastructure for rust-analyzer. +//! +//! When you print a hir type, like a module, using `eprintln!("{:?}", module)`, +//! you usually get back a numeric ID, which doesn't tell you much: +//! `Module(92)`. +//! +//! This module adds convenience `debug` methods to various types, which resolve +//! the id to a human-readable location info: +//! +//! ```not_rust +//! eprintln!("{:?}", module.debug(db)); +//! => +//! Module { name: collections, path: "liballoc/collections/mod.rs" } +//! ``` +//! +//! Note that to get this info, we might need to execute queries! So +//! +//! * don't use the `debug` methods for logging +//! * when debugging, be aware that interference is possible. + +use std::fmt; use ra_db::{CrateId, FileId}; @@ -50,15 +70,14 @@ impl HirDebugDatabase for DB { } } -fn debug_fn(f: impl FnOnce(&mut fmt::Formatter<'_>) -> fmt::Result) -> impl fmt::Debug { - struct DebugFn(Cell>); +fn debug_fn(f: impl Fn(&mut fmt::Formatter<'_>) -> fmt::Result) -> impl fmt::Debug { + struct DebugFn(F); - impl) -> fmt::Result> fmt::Debug for DebugFn { + impl) -> fmt::Result> fmt::Debug for DebugFn { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - let f = self.0.take().unwrap(); - f(fmt) + (&self.0)(fmt) } } - DebugFn(Cell::new(Some(f))) + DebugFn(f) } -- cgit v1.2.3