Skip to content

Commit 86a2e85

Browse files
committed
Separate out some IndexItem fields into a new struct and DRY
1 parent 0160933 commit 86a2e85

File tree

3 files changed

+43
-36
lines changed

3 files changed

+43
-36
lines changed

src/librustdoc/formats/cache.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ use crate::core::DocContext;
1515
use crate::fold::DocFolder;
1616
use crate::formats::Impl;
1717
use crate::formats::item_type::ItemType;
18-
use crate::html::markdown::short_markdown_summary;
19-
use crate::html::render::IndexItem;
20-
use crate::html::render::search_index::get_function_type_for_search;
18+
use crate::html::render::{IndexItem, IndexItemInfo};
2119
use crate::visit_lib::RustdocEffectiveVisibilities;
2220

2321
/// This cache is used to store information about the [`clean::Crate`] being
@@ -574,7 +572,6 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It
574572

575573
debug_assert!(!item.is_stripped());
576574

577-
let desc = short_markdown_summary(&item.doc_value(), &item.link_names(cache));
578575
// For searching purposes, a re-export is a duplicate if:
579576
//
580577
// - It's either an inline, or a true re-export
@@ -585,30 +582,25 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It
585582
_ => item_def_id,
586583
};
587584
let (impl_id, trait_parent) = cache.parent_stack_last_impl_and_trait_id();
588-
let search_type = get_function_type_for_search(
589-
item,
585+
let info = IndexItemInfo::new(
590586
tcx,
591-
clean_impl_generics(cache.parent_stack.last()).as_ref(),
592-
parent_did,
593587
cache,
588+
item,
589+
parent_did,
590+
clean_impl_generics(cache.parent_stack.last()).as_ref(),
594591
);
595-
let aliases = item.attrs.get_doc_aliases();
596-
let deprecation = item.deprecation(tcx);
597592
let index_item = IndexItem {
598593
ty: item.type_(),
599594
defid: Some(defid),
600595
name,
601596
module_path: parent_path.to_vec(),
602-
desc,
603597
parent: parent_did,
604598
parent_idx: None,
605599
trait_parent,
606600
trait_parent_idx: None,
607601
exact_module_path: None,
608602
impl_id,
609-
search_type,
610-
aliases,
611-
deprecation,
603+
info,
612604
};
613605

614606
cache.search_index.push(index_item);

src/librustdoc/html/render/mod.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use tracing::{debug, info};
6666
pub(crate) use self::context::*;
6767
pub(crate) use self::span_map::{LinkFromSrc, collect_spans_and_sources};
6868
pub(crate) use self::write_shared::*;
69-
use crate::clean::{self, ItemId, RenderedLink};
69+
use crate::clean::{self, Item, ItemId, RenderedLink};
7070
use crate::display::{Joined as _, MaybeDisplay as _};
7171
use crate::error::Error;
7272
use crate::formats::Impl;
@@ -79,7 +79,7 @@ use crate::html::format::{
7979
print_impl, print_path, print_type, print_where_clause, visibility_print_with_space,
8080
};
8181
use crate::html::markdown::{
82-
HeadingOffset, IdMap, Markdown, MarkdownItemInfo, MarkdownSummaryLine,
82+
HeadingOffset, IdMap, Markdown, MarkdownItemInfo, MarkdownSummaryLine, short_markdown_summary,
8383
};
8484
use crate::html::static_files::SCRAPE_EXAMPLES_HELP_MD;
8585
use crate::html::{highlight, sources};
@@ -124,6 +124,31 @@ enum RenderMode {
124124
// Helper structs for rendering items/sidebars and carrying along contextual
125125
// information
126126

127+
#[derive(Debug)]
128+
pub(crate) struct IndexItemInfo {
129+
pub(crate) desc: String,
130+
pub(crate) search_type: Option<IndexItemFunctionType>,
131+
pub(crate) aliases: Box<[Symbol]>,
132+
pub(crate) deprecation: Option<Deprecation>,
133+
}
134+
135+
impl IndexItemInfo {
136+
pub(crate) fn new(
137+
tcx: TyCtxt<'_>,
138+
cache: &Cache,
139+
item: &Item,
140+
parent_did: Option<DefId>,
141+
impl_generics: Option<&(clean::Type, clean::Generics)>,
142+
) -> Self {
143+
let desc = short_markdown_summary(&item.doc_value(), &item.link_names(cache));
144+
let search_type =
145+
search_index::get_function_type_for_search(item, tcx, impl_generics, parent_did, cache);
146+
let aliases = item.attrs.get_doc_aliases();
147+
let deprecation = item.deprecation(tcx);
148+
Self { desc, search_type, aliases, deprecation }
149+
}
150+
}
151+
127152
/// Struct representing one entry in the JS search index. These are all emitted
128153
/// by hand to a large JS file at the end of cache-creation.
129154
#[derive(Debug)]
@@ -132,16 +157,13 @@ pub(crate) struct IndexItem {
132157
pub(crate) defid: Option<DefId>,
133158
pub(crate) name: Symbol,
134159
pub(crate) module_path: Vec<Symbol>,
135-
pub(crate) desc: String,
136160
pub(crate) parent: Option<DefId>,
137161
pub(crate) parent_idx: Option<usize>,
138162
pub(crate) trait_parent: Option<DefId>,
139163
pub(crate) trait_parent_idx: Option<usize>,
140164
pub(crate) exact_module_path: Option<Vec<Symbol>>,
141165
pub(crate) impl_id: Option<DefId>,
142-
pub(crate) search_type: Option<IndexItemFunctionType>,
143-
pub(crate) aliases: Box<[Symbol]>,
144-
pub(crate) deprecation: Option<Deprecation>,
166+
pub(crate) info: IndexItemInfo,
145167
}
146168

147169
/// A type used for the search index.

src/librustdoc/html/render/search_index.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ use crate::error::Error;
2929
use crate::formats::cache::{Cache, OrphanImplItem};
3030
use crate::formats::item_type::ItemType;
3131
use crate::html::markdown::short_markdown_summary;
32-
use crate::html::render::{self, IndexItem, IndexItemFunctionType, RenderType, RenderTypeId};
32+
use crate::html::render::{
33+
self, IndexItem, IndexItemFunctionType, IndexItemInfo, RenderType, RenderTypeId,
34+
};
3335

3436
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
3537
pub(crate) struct SerializedSearchIndex {
@@ -1261,28 +1263,19 @@ pub(crate) fn build_index(
12611263
&cache.orphan_impl_items
12621264
{
12631265
if let Some((fqp, _)) = cache.paths.get(&parent) {
1264-
let desc = short_markdown_summary(&item.doc_value(), &item.link_names(cache));
1266+
let info = IndexItemInfo::new(tcx, cache, item, Some(parent), impl_generics.as_ref());
12651267
search_index.push(IndexItem {
12661268
ty: item.type_(),
12671269
defid: item.item_id.as_def_id(),
12681270
name: item.name.unwrap(),
12691271
module_path: fqp[..fqp.len() - 1].to_vec(),
1270-
desc,
12711272
parent: Some(parent),
12721273
parent_idx: None,
12731274
trait_parent,
12741275
trait_parent_idx: None,
12751276
exact_module_path: None,
12761277
impl_id,
1277-
search_type: get_function_type_for_search(
1278-
item,
1279-
tcx,
1280-
impl_generics.as_ref(),
1281-
Some(parent),
1282-
cache,
1283-
),
1284-
aliases: item.attrs.get_doc_aliases(),
1285-
deprecation: item.deprecation(tcx),
1278+
info,
12861279
});
12871280
}
12881281
}
@@ -1519,7 +1512,7 @@ pub(crate) fn build_index(
15191512
trait_parent: item.trait_parent_idx,
15201513
module_path,
15211514
exact_module_path,
1522-
deprecated: item.deprecation.is_some(),
1515+
deprecated: item.info.deprecation.is_some(),
15231516
associated_item_disambiguator: if let Some(impl_id) = item.impl_id
15241517
&& let Some(parent_idx) = item.parent_idx
15251518
&& associated_item_duplicates
@@ -1534,12 +1527,12 @@ pub(crate) fn build_index(
15341527
},
15351528
krate: crate_idx,
15361529
},
1537-
item.desc.to_string(),
1530+
item.info.desc.to_string(),
15381531
);
15391532

15401533
// Aliases
15411534
// -------
1542-
for alias in &item.aliases[..] {
1535+
for alias in &item.info.aliases {
15431536
serialized_index.push_alias(alias.as_str().to_string(), new_entry_id);
15441537
}
15451538

@@ -1812,7 +1805,7 @@ pub(crate) fn build_index(
18121805
_ => {}
18131806
}
18141807
}
1815-
if let Some(search_type) = &mut item.search_type {
1808+
if let Some(search_type) = &mut item.info.search_type {
18161809
let mut used_in_function_inputs = BTreeSet::new();
18171810
let mut used_in_function_output = BTreeSet::new();
18181811
for item in &mut search_type.inputs {

0 commit comments

Comments
 (0)