Rollup merge of #137886 - NotLebedev:stable-mir-91, r=oli-obk
`name()` and `trimmed_name()` for `stable_mir::crate_def::DefId` Resolves https://github.com/rust-lang/project-stable-mir/issues/91 * Added `stable_mir::crate_def::DefId::name()` and `stable_mir::crate_def::DefId::trimmed_name()` methods * Changed `CrateDef` and `DefId` `Debug` implementations to use new methods instead of copy-paste call to `Context::def_name` * Updated docs to avoid duplicating description of what `name` and `trimmed_name` do
This commit is contained in:
commit
59fe0c77bb
2 changed files with 29 additions and 18 deletions
|
@ -10,18 +10,10 @@ use crate::{Crate, Symbol, with};
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Hash, Serialize)]
|
#[derive(Clone, Copy, PartialEq, Eq, Hash, Serialize)]
|
||||||
pub struct DefId(pub(crate) usize);
|
pub struct DefId(pub(crate) usize);
|
||||||
|
|
||||||
/// A trait for retrieving information about a particular definition.
|
impl DefId {
|
||||||
///
|
/// Return fully qualified name of this definition
|
||||||
/// Implementors must provide the implementation of `def_id` which will be used to retrieve
|
pub fn name(&self) -> Symbol {
|
||||||
/// information about a crate's definition.
|
with(|cx| cx.def_name(*self, false))
|
||||||
pub trait CrateDef {
|
|
||||||
/// Retrieve the unique identifier for the current definition.
|
|
||||||
fn def_id(&self) -> DefId;
|
|
||||||
|
|
||||||
/// Return the fully qualified name of the current definition.
|
|
||||||
fn name(&self) -> Symbol {
|
|
||||||
let def_id = self.def_id();
|
|
||||||
with(|cx| cx.def_name(def_id, false))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return a trimmed name of this definition.
|
/// Return a trimmed name of this definition.
|
||||||
|
@ -34,9 +26,31 @@ pub trait CrateDef {
|
||||||
///
|
///
|
||||||
/// For example, this function may shorten `std::vec::Vec` to just `Vec`,
|
/// For example, this function may shorten `std::vec::Vec` to just `Vec`,
|
||||||
/// as long as there is no other `Vec` importable anywhere.
|
/// as long as there is no other `Vec` importable anywhere.
|
||||||
|
pub fn trimmed_name(&self) -> Symbol {
|
||||||
|
with(|cx| cx.def_name(*self, true))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A trait for retrieving information about a particular definition.
|
||||||
|
///
|
||||||
|
/// Implementors must provide the implementation of `def_id` which will be used to retrieve
|
||||||
|
/// information about a crate's definition.
|
||||||
|
pub trait CrateDef {
|
||||||
|
/// Retrieve the unique identifier for the current definition.
|
||||||
|
fn def_id(&self) -> DefId;
|
||||||
|
|
||||||
|
/// Return the fully qualified name of the current definition.
|
||||||
|
///
|
||||||
|
/// See [`DefId::name`] for more details
|
||||||
|
fn name(&self) -> Symbol {
|
||||||
|
self.def_id().name()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return a trimmed name of this definition.
|
||||||
|
///
|
||||||
|
/// See [`DefId::trimmed_name`] for more details
|
||||||
fn trimmed_name(&self) -> Symbol {
|
fn trimmed_name(&self) -> Symbol {
|
||||||
let def_id = self.def_id();
|
self.def_id().trimmed_name()
|
||||||
with(|cx| cx.def_name(def_id, true))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return information about the crate where this definition is declared.
|
/// Return information about the crate where this definition is declared.
|
||||||
|
|
|
@ -48,10 +48,7 @@ pub type CrateNum = usize;
|
||||||
|
|
||||||
impl Debug for DefId {
|
impl Debug for DefId {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
f.debug_struct("DefId")
|
f.debug_struct("DefId").field("id", &self.0).field("name", &self.name()).finish()
|
||||||
.field("id", &self.0)
|
|
||||||
.field("name", &with(|cx| cx.def_name(*self, false)))
|
|
||||||
.finish()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue