diff options
Diffstat (limited to 'src/librustdoc/html/render/mod.rs')
| -rw-r--r-- | src/librustdoc/html/render/mod.rs | 81 |
1 files changed, 44 insertions, 37 deletions
diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 3f426ee93e7..b01afa02b8b 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -110,63 +110,71 @@ pub(crate) struct IndexItem { /// A type used for the search index. #[derive(Debug)] pub(crate) struct RenderType { - name: Option<String>, - generics: Option<Vec<TypeWithKind>>, + id: Option<RenderTypeId>, + generics: Option<Vec<RenderType>>, } -/// Full type of functions/methods in the search index. -#[derive(Debug)] -pub(crate) struct IndexItemFunctionType { - inputs: Vec<TypeWithKind>, - output: Vec<TypeWithKind>, -} - -impl Serialize for IndexItemFunctionType { +impl Serialize for RenderType { fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer, { - // If we couldn't figure out a type, just write `null`. - let has_missing = self.inputs.iter().chain(self.output.iter()).any(|i| i.ty.name.is_none()); - if has_missing { - serializer.serialize_none() - } else { + let id = match &self.id { + // 0 is a sentinel, everything else is one-indexed + None => 0, + Some(RenderTypeId::Index(idx)) => idx + 1, + _ => panic!("must convert render types to indexes before serializing"), + }; + if let Some(generics) = &self.generics { let mut seq = serializer.serialize_seq(None)?; - seq.serialize_element(&self.inputs)?; - match self.output.as_slice() { - [] => {} - [one] => seq.serialize_element(one)?, - all => seq.serialize_element(all)?, - } + seq.serialize_element(&id)?; + seq.serialize_element(generics)?; seq.end() + } else { + id.serialize(serializer) } } } -#[derive(Debug)] -pub(crate) struct TypeWithKind { - ty: RenderType, - kind: ItemType, +#[derive(Clone, Debug)] +pub(crate) enum RenderTypeId { + DefId(DefId), + Primitive(clean::PrimitiveType), + Index(usize), } -impl From<(RenderType, ItemType)> for TypeWithKind { - fn from(x: (RenderType, ItemType)) -> TypeWithKind { - TypeWithKind { ty: x.0, kind: x.1 } - } +/// Full type of functions/methods in the search index. +#[derive(Debug)] +pub(crate) struct IndexItemFunctionType { + inputs: Vec<RenderType>, + output: Vec<RenderType>, } -impl Serialize for TypeWithKind { +impl Serialize for IndexItemFunctionType { fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer, { - let mut seq = serializer.serialize_seq(None)?; - seq.serialize_element(&self.ty.name)?; - seq.serialize_element(&self.kind)?; - if let Some(generics) = &self.ty.generics { - seq.serialize_element(generics)?; + // If we couldn't figure out a type, just write `0`. + let has_missing = self + .inputs + .iter() + .chain(self.output.iter()) + .any(|i| i.id.is_none() && i.generics.is_none()); + if has_missing { + 0.serialize(serializer) + } else { + let mut seq = serializer.serialize_seq(None)?; + match &self.inputs[..] { + [one] if one.generics.is_none() => seq.serialize_element(one)?, + _ => seq.serialize_element(&self.inputs)?, + } + match &self.output[..] { + [one] if one.generics.is_none() => seq.serialize_element(one)?, + _ => seq.serialize_element(&self.output)?, + } + seq.end() } - seq.end() } } @@ -2517,7 +2525,6 @@ fn item_ty_to_section(ty: ItemType) -> ItemSection { ItemType::ProcAttribute => ItemSection::AttributeMacros, ItemType::ProcDerive => ItemSection::DeriveMacros, ItemType::TraitAlias => ItemSection::TraitAliases, - ItemType::Generic => unreachable!(), } } |
