Skip to content

Commit c8c1424

Browse files
committed
Auto merge of #45673 - GuillaumeGomez:rustdoc-type-search-generic, r=QuietMisdreavus
Search over generic types in docs This is what I was talking about @QuietMisdreavus. Now we have generics. Waiting for #45617 to get merged.
2 parents 783c6ec + 0e4c829 commit c8c1424

File tree

3 files changed

+307
-131
lines changed

3 files changed

+307
-131
lines changed

src/librustdoc/html/render.rs

+29-7
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ impl ToJson for IndexItem {
361361
/// A type used for the search index.
362362
struct Type {
363363
name: Option<String>,
364+
generics: Option<Vec<String>>,
364365
}
365366

366367
impl ToJson for Type {
@@ -369,6 +370,9 @@ impl ToJson for Type {
369370
Some(ref name) => {
370371
let mut data = BTreeMap::new();
371372
data.insert("name".to_owned(), name.to_json());
373+
if let Some(ref generics) = self.generics {
374+
data.insert("generics".to_owned(), generics.to_json());
375+
}
372376
Json::Object(data)
373377
},
374378
None => Json::Null
@@ -420,7 +424,7 @@ fn init_ids() -> FxHashMap<String, usize> {
420424
"methods",
421425
"deref-methods",
422426
"implementations",
423-
].into_iter().map(|id| (String::from(*id), 1)).collect()
427+
].into_iter().map(|id| (String::from(*id), 1)).collect()
424428
}
425429

426430
/// This method resets the local table of used ID attributes. This is typically
@@ -667,7 +671,6 @@ fn concise_compared_strs(s1: &str, s2: &str) -> (String, String) {
667671
(format!("...{}", concise_str(s1)), format!("...{}", concise_str(s2)))
668672
}
669673

670-
671674
fn print_message(msg: &str, intro_msg: &mut bool, span: &Span, text: &str) {
672675
if !*intro_msg {
673676
println!("WARNING: documentation for this crate may be rendered \
@@ -3956,23 +3959,42 @@ fn get_index_search_type(item: &clean::Item) -> Option<IndexItemFunctionType> {
39563959
}
39573960

39583961
fn get_index_type(clean_type: &clean::Type) -> Type {
3959-
Type { name: get_index_type_name(clean_type).map(|s| s.to_ascii_lowercase()) }
3962+
let t = Type {
3963+
name: get_index_type_name(clean_type, true).map(|s| s.to_ascii_lowercase()),
3964+
generics: get_generics(clean_type),
3965+
};
3966+
t
39603967
}
39613968

3962-
fn get_index_type_name(clean_type: &clean::Type) -> Option<String> {
3969+
fn get_index_type_name(clean_type: &clean::Type, accept_generic: bool) -> Option<String> {
39633970
match *clean_type {
39643971
clean::ResolvedPath { ref path, .. } => {
39653972
let segments = &path.segments;
39663973
Some(segments[segments.len() - 1].name.clone())
3967-
},
3968-
clean::Generic(ref s) => Some(s.clone()),
3974+
}
3975+
clean::Generic(ref s) if accept_generic => Some(s.clone()),
39693976
clean::Primitive(ref p) => Some(format!("{:?}", p)),
3970-
clean::BorrowedRef { ref type_, .. } => get_index_type_name(type_),
3977+
clean::BorrowedRef { ref type_, .. } => get_index_type_name(type_, accept_generic),
39713978
// FIXME: add all from clean::Type.
39723979
_ => None
39733980
}
39743981
}
39753982

3983+
fn get_generics(clean_type: &clean::Type) -> Option<Vec<String>> {
3984+
clean_type.generics()
3985+
.and_then(|types| {
3986+
let r = types.iter()
3987+
.filter_map(|t| get_index_type_name(t, false))
3988+
.map(|s| s.to_ascii_lowercase())
3989+
.collect::<Vec<_>>();
3990+
if r.is_empty() {
3991+
None
3992+
} else {
3993+
Some(r)
3994+
}
3995+
})
3996+
}
3997+
39763998
pub fn cache() -> Arc<Cache> {
39773999
CACHE_KEY.with(|c| c.borrow().clone())
39784000
}

0 commit comments

Comments
 (0)