Skip to content

Commit 1ae875f

Browse files
committedFeb 18, 2023
Improve code readability
1 parent 3adc081 commit 1ae875f

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed
 

‎src/librustdoc/json/conversions.rs

+19-8
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl JsonRenderer<'_> {
3838
Some(UrlFragment::UserWritten(_)) | None => *page_id,
3939
};
4040

41-
(link.clone(), id_from_item_inner(id.into(), self.tcx, None, None))
41+
(link.clone(), id_from_item_default(id.into(), self.tcx))
4242
})
4343
.collect();
4444
let docs = item.attrs.collapsed_doc_value();
@@ -108,7 +108,7 @@ impl JsonRenderer<'_> {
108108
Some(ty::Visibility::Public) => Visibility::Public,
109109
Some(ty::Visibility::Restricted(did)) if did.is_crate_root() => Visibility::Crate,
110110
Some(ty::Visibility::Restricted(did)) => Visibility::Restricted {
111-
parent: id_from_item_inner(did.into(), self.tcx, None, None),
111+
parent: id_from_item_default(did.into(), self.tcx),
112112
path: self.tcx.def_path(did).to_string_no_crate_verbose(),
113113
},
114114
}
@@ -205,14 +205,25 @@ impl FromWithTcx<clean::TypeBindingKind> for TypeBindingKind {
205205
}
206206
}
207207

208+
#[inline]
209+
pub(crate) fn id_from_item_default(item_id: ItemId, tcx: TyCtxt<'_>) -> Id {
210+
id_from_item_inner(item_id, tcx, None, None)
211+
}
212+
208213
/// It generates an ID as follows:
209214
///
210-
/// `CRATE_ID:ITEM_ID[:NAME_ID]` (if there is no name, NAME_ID is not generated).
215+
/// `CRATE_ID:ITEM_ID[:NAME_ID][-EXTRA]`:
216+
/// * If there is no `name`, `NAME_ID` is not generated.
217+
/// * If there is no `extra`, `EXTRA` is not generated.
218+
///
219+
/// * `name` is the item's name if available (it's not for impl blocks for example).
220+
/// * `extra` is used for reexports: it contains the ID of the reexported item. It is used to allow
221+
/// to have items with the same name but different types to both appear in the generated JSON.
211222
pub(crate) fn id_from_item_inner(
212223
item_id: ItemId,
213224
tcx: TyCtxt<'_>,
214-
extra: Option<&Id>,
215225
name: Option<Symbol>,
226+
extra: Option<&Id>,
216227
) -> Id {
217228
struct DisplayDefId<'a, 'b>(DefId, TyCtxt<'a>, Option<&'b Id>, Option<Symbol>);
218229

@@ -275,9 +286,9 @@ pub(crate) fn id_from_item(item: &clean::Item, tcx: TyCtxt<'_>) -> Id {
275286
clean::ItemKind::ImportItem(ref import) => {
276287
let extra =
277288
import.source.did.map(ItemId::from).map(|i| id_from_item_inner(i, tcx, None, None));
278-
id_from_item_inner(item.item_id, tcx, extra.as_ref(), item.name)
289+
id_from_item_inner(item.item_id, tcx, item.name, extra.as_ref())
279290
}
280-
_ => id_from_item_inner(item.item_id, tcx, None, item.name),
291+
_ => id_from_item_inner(item.item_id, tcx, item.name, None),
281292
}
282293
}
283294

@@ -551,7 +562,7 @@ impl FromWithTcx<clean::Path> for Path {
551562
fn from_tcx(path: clean::Path, tcx: TyCtxt<'_>) -> Path {
552563
Path {
553564
name: path.whole_name(),
554-
id: id_from_item_inner(path.def_id().into(), tcx, None, None),
565+
id: id_from_item_default(path.def_id().into(), tcx),
555566
args: path.segments.last().map(|args| Box::new(args.clone().args.into_tcx(tcx))),
556567
}
557568
}
@@ -728,7 +739,7 @@ impl FromWithTcx<clean::Import> for Import {
728739
Import {
729740
source: import.source.path.whole_name(),
730741
name,
731-
id: import.source.did.map(ItemId::from).map(|i| id_from_item_inner(i, tcx, None, None)),
742+
id: import.source.did.map(ItemId::from).map(|i| id_from_item_default(i, tcx)),
732743
glob,
733744
}
734745
}

‎src/librustdoc/json/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use crate::docfs::PathError;
2828
use crate::error::Error;
2929
use crate::formats::cache::Cache;
3030
use crate::formats::FormatRenderer;
31-
use crate::json::conversions::{id_from_item, id_from_item_inner, IntoWithTcx};
31+
use crate::json::conversions::{id_from_item, id_from_item_default, IntoWithTcx};
3232
use crate::{clean, try_err};
3333

3434
#[derive(Clone)]
@@ -243,7 +243,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
243243
.chain(&self.cache.external_paths)
244244
.map(|(&k, &(ref path, kind))| {
245245
(
246-
id_from_item_inner(k.into(), self.tcx, None, None),
246+
id_from_item_default(k.into(), self.tcx),
247247
types::ItemSummary {
248248
crate_id: k.krate.as_u32(),
249249
path: path.iter().map(|s| s.to_string()).collect(),

0 commit comments

Comments
 (0)