Skip to content

Commit 283c71e

Browse files
committed
Refactor search-index types to avoid Options
1 parent 8f5a308 commit 283c71e

File tree

2 files changed

+27
-42
lines changed

2 files changed

+27
-42
lines changed

src/librustdoc/html/render/mod.rs

+11-23
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ crate struct IndexItem {
108108
/// A type used for the search index.
109109
#[derive(Debug)]
110110
crate struct RenderType {
111-
name: Option<String>,
112-
generics: Option<Vec<TypeWithKind>>,
111+
name: String,
112+
generics: Vec<TypeWithKind>,
113113
}
114114

115115
/// Full type of functions/methods in the search index.
@@ -125,19 +125,15 @@ impl Serialize for IndexItemFunctionType {
125125
S: Serializer,
126126
{
127127
// If we couldn't figure out a type, just write `null`.
128-
let has_missing = self.inputs.iter().chain(self.output.iter()).any(|i| i.ty.name.is_none());
129-
if has_missing {
130-
serializer.serialize_none()
131-
} else {
132-
let mut seq = serializer.serialize_seq(None)?;
133-
seq.serialize_element(&self.inputs)?;
134-
match self.output.as_slice() {
135-
[] => {}
136-
[one] => seq.serialize_element(one)?,
137-
all => seq.serialize_element(all)?,
138-
}
139-
seq.end()
128+
129+
let mut seq = serializer.serialize_seq(None)?;
130+
seq.serialize_element(&self.inputs)?;
131+
match self.output.as_slice() {
132+
[] => {}
133+
[one] => seq.serialize_element(one)?,
134+
all => seq.serialize_element(all)?,
140135
}
136+
seq.end()
141137
}
142138
}
143139

@@ -147,12 +143,6 @@ crate struct TypeWithKind {
147143
kind: ItemType,
148144
}
149145

150-
impl From<(RenderType, ItemType)> for TypeWithKind {
151-
fn from(x: (RenderType, ItemType)) -> TypeWithKind {
152-
TypeWithKind { ty: x.0, kind: x.1 }
153-
}
154-
}
155-
156146
impl Serialize for TypeWithKind {
157147
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
158148
where
@@ -161,9 +151,7 @@ impl Serialize for TypeWithKind {
161151
let mut seq = serializer.serialize_seq(None)?;
162152
seq.serialize_element(&self.ty.name)?;
163153
seq.serialize_element(&self.kind)?;
164-
if let Some(generics) = &self.ty.generics {
165-
seq.serialize_element(generics)?;
166-
}
154+
seq.serialize_element(&self.ty.generics)?;
167155
seq.end()
168156
}
169157
}

src/librustdoc/html/render/search_index.rs

+16-19
Original file line numberDiff line numberDiff line change
@@ -185,24 +185,19 @@ crate fn get_function_type_for_search<'tcx>(
185185
item: &clean::Item,
186186
tcx: TyCtxt<'tcx>,
187187
) -> Option<IndexItemFunctionType> {
188-
let (mut inputs, mut output) = match *item.kind {
188+
let (inputs, output) = match *item.kind {
189189
clean::FunctionItem(ref f) => get_fn_inputs_and_outputs(f, tcx),
190190
clean::MethodItem(ref m, _) => get_fn_inputs_and_outputs(m, tcx),
191191
clean::TyMethodItem(ref m) => get_fn_inputs_and_outputs(m, tcx),
192192
_ => return None,
193193
};
194194

195-
inputs.retain(|a| a.ty.name.is_some());
196-
output.retain(|a| a.ty.name.is_some());
197-
198195
Some(IndexItemFunctionType { inputs, output })
199196
}
200197

201-
fn get_index_type(clean_type: &clean::Type, generics: Vec<TypeWithKind>) -> RenderType {
202-
RenderType {
203-
name: get_index_type_name(clean_type).map(|s| s.as_str().to_ascii_lowercase()),
204-
generics: if generics.is_empty() { None } else { Some(generics) },
205-
}
198+
fn get_index_type(clean_type: &clean::Type, generics: Vec<TypeWithKind>) -> Option<RenderType> {
199+
get_index_type_name(clean_type)
200+
.map(|s| RenderType { name: s.as_str().to_ascii_lowercase(), generics })
206201
}
207202

208203
fn get_index_type_name(clean_type: &clean::Type) -> Option<Symbol> {
@@ -306,19 +301,17 @@ fn add_generics_and_bounds_as_types<'tcx>(
306301
return;
307302
}
308303
}
309-
let mut index_ty = get_index_type(&ty, generics);
310-
if index_ty.name.as_ref().map(|s| s.is_empty()).unwrap_or(true) {
311-
return;
312-
}
304+
let Some(mut index_ty) = get_index_type(&ty, generics)
305+
else { return };
313306
if is_full_generic {
314307
// We remove the name of the full generic because we have no use for it.
315-
index_ty.name = Some(String::new());
316-
res.push(TypeWithKind::from((index_ty, ItemType::Generic)));
308+
index_ty.name = String::new();
309+
res.push(TypeWithKind { ty: index_ty, kind: ItemType::Generic });
317310
} else if let Some(kind) = ty.def_id_no_primitives().map(|did| tcx.def_kind(did).into()) {
318-
res.push(TypeWithKind::from((index_ty, kind)));
311+
res.push(TypeWithKind { ty: index_ty, kind });
319312
} else if ty.is_primitive() {
320313
// This is a primitive, let's store it as such.
321-
res.push(TypeWithKind::from((index_ty, ItemType::Primitive)));
314+
res.push(TypeWithKind { ty: index_ty, kind: ItemType::Primitive });
322315
}
323316
}
324317

@@ -416,7 +409,9 @@ fn get_fn_inputs_and_outputs<'tcx>(
416409
} else {
417410
if let Some(kind) = arg.type_.def_id_no_primitives().map(|did| tcx.def_kind(did).into())
418411
{
419-
all_types.push(TypeWithKind::from((get_index_type(&arg.type_, vec![]), kind)));
412+
if let Some(ty) = get_index_type(&arg.type_, vec![]) {
413+
all_types.push(TypeWithKind { ty, kind });
414+
}
420415
}
421416
}
422417
}
@@ -429,7 +424,9 @@ fn get_fn_inputs_and_outputs<'tcx>(
429424
if let Some(kind) =
430425
return_type.def_id_no_primitives().map(|did| tcx.def_kind(did).into())
431426
{
432-
ret_types.push(TypeWithKind::from((get_index_type(return_type, vec![]), kind)));
427+
if let Some(ty) = get_index_type(return_type, vec![]) {
428+
ret_types.push(TypeWithKind { ty, kind });
429+
}
433430
}
434431
}
435432
}

0 commit comments

Comments
 (0)