clean::Enum {
generics: clean_ty_generics(cx, cx.tcx.generics_of(did), predicates),
- variants_stripped: false,
variants: cx.tcx.adt_def(did).variants().iter().map(|v| v.clean(cx)).collect(),
}
}
struct_type: variant.ctor_kind,
generics: clean_ty_generics(cx, cx.tcx.generics_of(did), predicates),
fields: variant.fields.iter().map(|x| x.clean(cx)).collect(),
- fields_stripped: false,
}
}
let generics = clean_ty_generics(cx, cx.tcx.generics_of(did), predicates);
let fields = variant.fields.iter().map(|x| x.clean(cx)).collect();
- clean::Union { generics, fields, fields_stripped: false }
+ clean::Union { generics, fields }
}
fn build_type_alias(cx: &mut DocContext<'_>, did: DefId) -> clean::Typedef {
VariantStruct {
struct_type: CtorKind::from_hir(self),
fields: self.fields().iter().map(|x| x.clean(cx)).collect(),
- fields_stripped: false,
}
}
}
}
CtorKind::Fictive => Variant::Struct(VariantStruct {
struct_type: CtorKind::Fictive,
- fields_stripped: false,
fields: self.fields.iter().map(|field| field.clean(cx)).collect(),
}),
};
ItemKind::Enum(ref def, ref generics) => EnumItem(Enum {
variants: def.variants.iter().map(|v| v.clean(cx)).collect(),
generics: generics.clean(cx),
- variants_stripped: false,
}),
ItemKind::TraitAlias(ref generics, bounds) => TraitAliasItem(TraitAlias {
generics: generics.clean(cx),
ItemKind::Union(ref variant_data, ref generics) => UnionItem(Union {
generics: generics.clean(cx),
fields: variant_data.fields().iter().map(|x| x.clean(cx)).collect(),
- fields_stripped: false,
}),
ItemKind::Struct(ref variant_data, ref generics) => StructItem(Struct {
struct_type: CtorKind::from_hir(variant_data),
generics: generics.clean(cx),
fields: variant_data.fields().iter().map(|x| x.clean(cx)).collect(),
- fields_stripped: false,
}),
ItemKind::Impl(ref impl_) => return clean_impl(impl_, item.hir_id(), cx),
// proc macros can have a name set by attributes
_ => false,
}
}
- pub(crate) fn has_stripped_fields(&self) -> Option<bool> {
+ pub(crate) fn has_stripped_entries(&self) -> Option<bool> {
match *self.kind {
- StructItem(ref _struct) => Some(_struct.fields_stripped),
- UnionItem(ref union) => Some(union.fields_stripped),
- VariantItem(Variant::Struct(ref vstruct)) => Some(vstruct.fields_stripped),
+ StructItem(ref struct_) => Some(struct_.has_stripped_entries()),
+ UnionItem(ref union_) => Some(union_.has_stripped_entries()),
+ EnumItem(ref enum_) => Some(enum_.has_stripped_entries()),
+ VariantItem(ref v) => v.has_stripped_entries(),
_ => None,
}
}
pub(crate) struct_type: CtorKind,
pub(crate) generics: Generics,
pub(crate) fields: Vec<Item>,
- pub(crate) fields_stripped: bool,
+}
+
+impl Struct {
+ pub(crate) fn has_stripped_entries(&self) -> bool {
+ self.fields.iter().any(|f| f.is_stripped())
+ }
}
#[derive(Clone, Debug)]
pub(crate) struct Union {
pub(crate) generics: Generics,
pub(crate) fields: Vec<Item>,
- pub(crate) fields_stripped: bool,
+}
+
+impl Union {
+ pub(crate) fn has_stripped_entries(&self) -> bool {
+ self.fields.iter().any(|f| f.is_stripped())
+ }
}
/// This is a more limited form of the standard Struct, different in that
pub(crate) struct VariantStruct {
pub(crate) struct_type: CtorKind,
pub(crate) fields: Vec<Item>,
- pub(crate) fields_stripped: bool,
+}
+
+impl VariantStruct {
+ pub(crate) fn has_stripped_entries(&self) -> bool {
+ self.fields.iter().any(|f| f.is_stripped())
+ }
}
#[derive(Clone, Debug)]
pub(crate) struct Enum {
pub(crate) variants: IndexVec<VariantIdx, Item>,
pub(crate) generics: Generics,
- pub(crate) variants_stripped: bool,
+}
+
+impl Enum {
+ pub(crate) fn has_stripped_entries(&self) -> bool {
+ self.variants.iter().any(|f| f.is_stripped())
+ }
+
+ pub(crate) fn variants(&self) -> impl Iterator<Item = &Item> {
+ self.variants.iter().filter(|v| !v.is_stripped())
+ }
}
#[derive(Clone, Debug)]
Struct(VariantStruct),
}
+impl Variant {
+ pub(crate) fn has_stripped_entries(&self) -> Option<bool> {
+ match *self {
+ Self::Struct(ref struct_) => Some(struct_.has_stripped_entries()),
+ Self::CLike | Self::Tuple(_) => None,
+ }
+ }
+}
+
/// Small wrapper around [`rustc_span::Span`] that adds helper methods
/// and enforces calling [`rustc_span::Span::source_callsite()`].
#[derive(Copy, Clone, Debug)]
StrippedItem(..) => unreachable!(),
ModuleItem(i) => ModuleItem(self.fold_mod(i)),
StructItem(mut i) => {
- let num_fields = i.fields.len();
i.fields = i.fields.into_iter().filter_map(|x| self.fold_item(x)).collect();
- if !i.fields_stripped {
- i.fields_stripped =
- num_fields != i.fields.len() || i.fields.iter().any(|f| f.is_stripped());
- }
StructItem(i)
}
UnionItem(mut i) => {
- let num_fields = i.fields.len();
i.fields = i.fields.into_iter().filter_map(|x| self.fold_item(x)).collect();
- if !i.fields_stripped {
- i.fields_stripped =
- num_fields != i.fields.len() || i.fields.iter().any(|f| f.is_stripped());
- }
UnionItem(i)
}
EnumItem(mut i) => {
- let num_variants = i.variants.len();
i.variants = i.variants.into_iter().filter_map(|x| self.fold_item(x)).collect();
- if !i.variants_stripped {
- i.variants_stripped = num_variants != i.variants.len()
- || i.variants.iter().any(|f| f.is_stripped());
- }
EnumItem(i)
}
TraitItem(mut i) => {
}
VariantItem(i) => match i {
Variant::Struct(mut j) => {
- let num_fields = j.fields.len();
j.fields = j.fields.into_iter().filter_map(|x| self.fold_item(x)).collect();
- if !j.fields_stripped {
- j.fields_stripped = num_fields != j.fields.len()
- || j.fields.iter().any(|f| f.is_stripped());
- }
VariantItem(Variant::Struct(j))
}
Variant::Tuple(fields) => {
let mut sidebar = Buffer::new();
let mut variants = e
- .variants
- .iter()
+ .variants()
.filter_map(|v| {
v.name
.as_ref()
}
fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum) {
+ let count_variants = e.variants().count();
wrap_into_docblock(w, |w| {
wrap_item(w, "enum", |w| {
render_attributes_in_pre(w, it, "");
e.generics.print(cx),
print_where_clause(&e.generics, cx, 0, true),
);
- if e.variants.is_empty() && !e.variants_stripped {
+ let variants_stripped = e.has_stripped_entries();
+ if count_variants == 0 && !variants_stripped {
w.write_str(" {}");
} else {
w.write_str(" {\n");
- let count_variants = e.variants.len();
let toggle = should_hide_fields(count_variants);
if toggle {
toggle_open(w, format_args!("{} variants", count_variants));
}
- for v in &e.variants {
+ for v in e.variants() {
w.write_str(" ");
let name = v.name.unwrap();
match *v.kind {
w.write_str(",\n");
}
- if e.variants_stripped {
+ if variants_stripped {
w.write_str(" // some variants omitted\n");
}
if toggle {
document(w, cx, it, None, HeadingOffset::H2);
- if !e.variants.is_empty() {
+ if count_variants != 0 {
write!(
w,
"<h2 id=\"variants\" class=\"variants small-section-header\">\
document_non_exhaustive_header(it)
);
document_non_exhaustive(w, it);
- for variant in &e.variants {
+ for variant in e.variants() {
let id = cx.derive_id(format!("{}.{}", ItemType::Variant, variant.name.unwrap()));
write!(
w,
}
}
- if it.has_stripped_fields().unwrap() {
+ if it.has_stripped_entries().unwrap() {
write!(w, " /* private fields */\n{}", tab);
}
if toggle {
}
if has_visible_fields {
- if it.has_stripped_fields().unwrap() {
+ if it.has_stripped_entries().unwrap() {
write!(w, "\n{} /* private fields */", tab);
}
write!(w, "\n{}", tab);
- } else if it.has_stripped_fields().unwrap() {
+ } else if it.has_stripped_entries().unwrap() {
write!(w, " /* private fields */ ");
}
if toggle {
impl FromWithTcx<clean::Struct> for Struct {
fn from_tcx(struct_: clean::Struct, tcx: TyCtxt<'_>) -> Self {
- let clean::Struct { struct_type, generics, fields, fields_stripped } = struct_;
+ let fields_stripped = struct_.has_stripped_entries();
+ let clean::Struct { struct_type, generics, fields } = struct_;
Struct {
struct_type: from_ctor_kind(struct_type),
generics: generics.into_tcx(tcx),
}
impl FromWithTcx<clean::Union> for Union {
- fn from_tcx(struct_: clean::Union, tcx: TyCtxt<'_>) -> Self {
- let clean::Union { generics, fields, fields_stripped } = struct_;
+ fn from_tcx(union_: clean::Union, tcx: TyCtxt<'_>) -> Self {
+ let fields_stripped = union_.has_stripped_entries();
+ let clean::Union { generics, fields } = union_;
Union {
generics: generics.into_tcx(tcx),
fields_stripped,
impl FromWithTcx<clean::Enum> for Enum {
fn from_tcx(enum_: clean::Enum, tcx: TyCtxt<'_>) -> Self {
- let clean::Enum { variants, generics, variants_stripped } = enum_;
+ let variants_stripped = enum_.has_stripped_entries();
+ let clean::Enum { variants, generics } = enum_;
Enum {
generics: generics.into_tcx(tcx),
variants_stripped,
impl FromWithTcx<clean::VariantStruct> for Struct {
fn from_tcx(struct_: clean::VariantStruct, _tcx: TyCtxt<'_>) -> Self {
- let clean::VariantStruct { struct_type, fields, fields_stripped } = struct_;
+ let fields_stripped = struct_.has_stripped_entries();
+ let clean::VariantStruct { struct_type, fields } = struct_;
Struct {
struct_type: from_ctor_kind(struct_type),
generics: Default::default(),
debug!("strip_hidden: stripping {:?} {:?}", i.type_(), i.name);
// use a dedicated hidden item for given item type if any
match *i.kind {
- clean::StructFieldItem(..) | clean::ModuleItem(..) => {
+ clean::StructFieldItem(..) | clean::ModuleItem(..) | clean::VariantItem(..) => {
// We need to recurse into stripped modules to
// strip things like impl methods but when doing so
// we must not add any items to the `retained` set.