Skip to content

Commit a6c2f86

Browse files
committed
Separate out some IndexItem fields into a new struct and DRY
1 parent 3142bea commit a6c2f86

3 files changed

Lines changed: 49 additions & 41 deletions

File tree

src/librustdoc/formats/cache.rs

Lines changed: 6 additions & 16 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
@@ -576,7 +574,6 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It
576574

577575
debug_assert!(!item.is_stripped());
578576

579-
let desc = short_markdown_summary(&item.doc_value(), &item.link_names(cache));
580577
// For searching purposes, a re-export is a duplicate if:
581578
//
582579
// - It's either an inline, or a true re-export
@@ -587,32 +584,25 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It
587584
_ => item_def_id,
588585
};
589586
let (impl_id, trait_parent) = cache.parent_stack_last_impl_and_trait_id();
590-
let search_type = get_function_type_for_search(
591-
item,
587+
let info = IndexItemInfo::new(
592588
tcx,
593-
clean_impl_generics(cache.parent_stack.last()).as_ref(),
594-
parent_did,
595589
cache,
590+
item,
591+
parent_did,
592+
clean_impl_generics(cache.parent_stack.last()).as_ref(),
596593
);
597-
let aliases = item.attrs.get_doc_aliases();
598-
let is_deprecated = item.is_deprecated(tcx);
599-
let is_unstable = item.is_unstable();
600594
let index_item = IndexItem {
601595
ty: item.type_(),
602596
defid: Some(defid),
603597
name,
604598
module_path: parent_path.to_vec(),
605-
desc,
606599
parent: parent_did,
607600
parent_idx: None,
608601
trait_parent,
609602
trait_parent_idx: None,
610603
exact_module_path: None,
611604
impl_id,
612-
search_type,
613-
aliases,
614-
is_deprecated,
615-
is_unstable,
605+
info,
616606
};
617607

618608
cache.search_index.push(index_item);

src/librustdoc/html/render/mod.rs

Lines changed: 30 additions & 7 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, Defaultness, ItemId, RenderedLink};
69+
use crate::clean::{self, Defaultness, Item, ItemId, RenderedLink};
7070
use crate::display::{Joined as _, MaybeDisplay as _};
7171
use crate::error::Error;
7272
use crate::formats::Impl;
@@ -79,8 +79,9 @@ use crate::html::format::{
7979
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
};
84+
use crate::html::render::search_index::get_function_type_for_search;
8485
use crate::html::static_files::SCRAPE_EXAMPLES_HELP_MD;
8586
use crate::html::{highlight, sources};
8687
use crate::scrape_examples::{CallData, CallLocation};
@@ -124,6 +125,32 @@ enum RenderMode {
124125
// Helper structs for rendering items/sidebars and carrying along contextual
125126
// information
126127

128+
#[derive(Debug)]
129+
pub(crate) struct IndexItemInfo {
130+
pub(crate) desc: String,
131+
pub(crate) search_type: Option<IndexItemFunctionType>,
132+
pub(crate) aliases: Box<[Symbol]>,
133+
pub(crate) deprecation: Option<Deprecation>,
134+
pub(crate) is_unstable: bool,
135+
}
136+
137+
impl IndexItemInfo {
138+
pub(crate) fn new(
139+
tcx: TyCtxt<'_>,
140+
cache: &Cache,
141+
item: &Item,
142+
parent_did: Option<DefId>,
143+
impl_generics: Option<&(clean::Type, clean::Generics)>,
144+
) -> Self {
145+
let desc = short_markdown_summary(&item.doc_value(), &item.link_names(cache));
146+
let search_type = get_function_type_for_search(item, tcx, impl_generics, parent_did, cache);
147+
let aliases = item.attrs.get_doc_aliases();
148+
let deprecation = item.deprecation(tcx);
149+
let is_unstable = item.is_unstable();
150+
Self { desc, search_type, aliases, deprecation, is_unstable }
151+
}
152+
}
153+
127154
/// Struct representing one entry in the JS search index. These are all emitted
128155
/// by hand to a large JS file at the end of cache-creation.
129156
#[derive(Debug)]
@@ -132,17 +159,13 @@ pub(crate) struct IndexItem {
132159
pub(crate) defid: Option<DefId>,
133160
pub(crate) name: Symbol,
134161
pub(crate) module_path: Vec<Symbol>,
135-
pub(crate) desc: String,
136162
pub(crate) parent: Option<DefId>,
137163
pub(crate) parent_idx: Option<usize>,
138164
pub(crate) trait_parent: Option<DefId>,
139165
pub(crate) trait_parent_idx: Option<usize>,
140166
pub(crate) exact_module_path: Option<Vec<Symbol>>,
141167
pub(crate) impl_id: Option<DefId>,
142-
pub(crate) search_type: Option<IndexItemFunctionType>,
143-
pub(crate) aliases: Box<[Symbol]>,
144-
pub(crate) is_deprecated: bool,
145-
pub(crate) is_unstable: bool,
168+
pub(crate) info: IndexItemInfo,
146169
}
147170

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

src/librustdoc/html/render/search_index.rs

Lines changed: 13 additions & 18 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 {
@@ -1270,29 +1272,19 @@ pub(crate) fn build_index(
12701272
&cache.orphan_impl_items
12711273
{
12721274
if let Some((fqp, _)) = cache.paths.get(&parent) {
1273-
let desc = short_markdown_summary(&item.doc_value(), &item.link_names(cache));
1275+
let info = IndexItemInfo::new(tcx, cache, item, Some(parent), impl_generics.as_ref());
12741276
search_index.push(IndexItem {
12751277
ty: item.type_(),
12761278
defid: item.item_id.as_def_id(),
12771279
name: item.name.unwrap(),
12781280
module_path: fqp[..fqp.len() - 1].to_vec(),
1279-
desc,
12801281
parent: Some(parent),
12811282
parent_idx: None,
12821283
trait_parent,
12831284
trait_parent_idx: None,
12841285
exact_module_path: None,
12851286
impl_id,
1286-
search_type: get_function_type_for_search(
1287-
item,
1288-
tcx,
1289-
impl_generics.as_ref(),
1290-
Some(parent),
1291-
cache,
1292-
),
1293-
aliases: item.attrs.get_doc_aliases(),
1294-
is_deprecated: item.is_deprecated(tcx),
1295-
is_unstable: item.is_unstable(),
1287+
info,
12961288
});
12971289
}
12981290
}
@@ -1530,8 +1522,11 @@ pub(crate) fn build_index(
15301522
trait_parent: item.trait_parent_idx,
15311523
module_path,
15321524
exact_module_path,
1533-
deprecated: item.is_deprecated,
1534-
unstable: item.is_unstable,
1525+
deprecated: item
1526+
.info
1527+
.deprecation
1528+
.is_some_and(|deprecation| deprecation.is_in_effect()),
1529+
unstable: item.info.is_unstable,
15351530
associated_item_disambiguator_or_extern_crate_url: if let Some(impl_id) =
15361531
item.impl_id
15371532
&& let Some(parent_idx) = item.parent_idx
@@ -1555,12 +1550,12 @@ pub(crate) fn build_index(
15551550
},
15561551
krate: crate_idx,
15571552
},
1558-
item.desc.to_string(),
1553+
item.info.desc.to_string(),
15591554
);
15601555

15611556
// Aliases
15621557
// -------
1563-
for alias in &item.aliases[..] {
1558+
for alias in &item.info.aliases {
15641559
serialized_index.push_alias(alias.as_str().to_string(), new_entry_id);
15651560
}
15661561

@@ -1833,7 +1828,7 @@ pub(crate) fn build_index(
18331828
_ => {}
18341829
}
18351830
}
1836-
if let Some(search_type) = &mut item.search_type {
1831+
if let Some(search_type) = &mut item.info.search_type {
18371832
let mut used_in_function_inputs = BTreeSet::new();
18381833
let mut used_in_function_output = BTreeSet::new();
18391834
for item in &mut search_type.inputs {

0 commit comments

Comments
 (0)