1
Fork 0

Refactor HiddenStructField into StrippedItem

This commit is contained in:
mitaa 2016-04-02 08:17:59 +02:00
parent b1543a1aac
commit 0ef85c1e6a
4 changed files with 76 additions and 56 deletions

View file

@ -14,7 +14,6 @@
pub use self::Type::*; pub use self::Type::*;
pub use self::PrimitiveType::*; pub use self::PrimitiveType::*;
pub use self::TypeKind::*; pub use self::TypeKind::*;
pub use self::StructField::*;
pub use self::VariantKind::*; pub use self::VariantKind::*;
pub use self::Mutability::*; pub use self::Mutability::*;
pub use self::Import::*; pub use self::Import::*;
@ -309,6 +308,15 @@ impl Item {
pub fn is_stripped(&self) -> bool { pub fn is_stripped(&self) -> bool {
match self.inner { StrippedItem(..) => true, _ => false } match self.inner { StrippedItem(..) => true, _ => false }
} }
pub fn has_stripped_fields(&self) -> Option<bool> {
match self.inner {
StructItem(ref _struct) => Some(_struct.fields_stripped),
VariantItem(Variant { kind: StructVariant(ref vstruct)} ) => {
Some(vstruct.fields_stripped)
},
_ => None,
}
}
pub fn stability_class(&self) -> String { pub fn stability_class(&self) -> String {
self.stability.as_ref().map(|ref s| { self.stability.as_ref().map(|ref s| {
@ -346,7 +354,7 @@ pub enum ItemEnum {
TyMethodItem(TyMethod), TyMethodItem(TyMethod),
/// A method with a body. /// A method with a body.
MethodItem(Method), MethodItem(Method),
StructFieldItem(StructField), StructFieldItem(Type),
VariantItem(Variant), VariantItem(Variant),
/// `fn`s from an extern block /// `fn`s from an extern block
ForeignFunctionItem(Function), ForeignFunctionItem(Function),
@ -1740,12 +1748,6 @@ impl<'tcx> Clean<Type> for ty::Ty<'tcx> {
} }
} }
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
pub enum StructField {
HiddenStructField, // inserted later by strip passes
TypedStructField(Type),
}
impl Clean<Item> for hir::StructField { impl Clean<Item> for hir::StructField {
fn clean(&self, cx: &DocContext) -> Item { fn clean(&self, cx: &DocContext) -> Item {
Item { Item {
@ -1756,7 +1758,7 @@ impl Clean<Item> for hir::StructField {
stability: get_stability(cx, cx.map.local_def_id(self.id)), stability: get_stability(cx, cx.map.local_def_id(self.id)),
deprecation: get_deprecation(cx, cx.map.local_def_id(self.id)), deprecation: get_deprecation(cx, cx.map.local_def_id(self.id)),
def_id: cx.map.local_def_id(self.id), def_id: cx.map.local_def_id(self.id),
inner: StructFieldItem(TypedStructField(self.ty.clean(cx))), inner: StructFieldItem(self.ty.clean(cx)),
} }
} }
} }
@ -1773,7 +1775,7 @@ impl<'tcx> Clean<Item> for ty::FieldDefData<'tcx, 'static> {
stability: get_stability(cx, self.did), stability: get_stability(cx, self.did),
deprecation: get_deprecation(cx, self.did), deprecation: get_deprecation(cx, self.did),
def_id: self.did, def_id: self.did,
inner: StructFieldItem(TypedStructField(self.unsubst_ty().clean(cx))), inner: StructFieldItem(self.unsubst_ty().clean(cx)),
} }
} }
} }
@ -1904,9 +1906,7 @@ impl<'tcx> Clean<Item> for ty::VariantDefData<'tcx, 'static> {
def_id: field.did, def_id: field.did,
stability: get_stability(cx, field.did), stability: get_stability(cx, field.did),
deprecation: get_deprecation(cx, field.did), deprecation: get_deprecation(cx, field.did),
inner: StructFieldItem( inner: StructFieldItem(field.unsubst_ty().clean(cx))
TypedStructField(field.unsubst_ty().clean(cx))
)
} }
}).collect() }).collect()
}) })

View file

@ -1020,13 +1020,9 @@ impl DocFolder for Cache {
} }
_ => ((None, Some(&*self.stack)), false) _ => ((None, Some(&*self.stack)), false)
}; };
let hidden_field = match item.inner {
clean::StructFieldItem(clean::HiddenStructField) => true,
_ => false
};
match parent { match parent {
(parent, Some(path)) if is_method || (!self.stripped_mod && !hidden_field) => { (parent, Some(path)) if is_method || (!self.stripped_mod) => {
// Needed to determine `self` type. // Needed to determine `self` type.
let parent_basename = self.parent_stack.first().and_then(|parent| { let parent_basename = self.parent_stack.first().and_then(|parent| {
match self.paths.get(parent) { match self.paths.get(parent) {
@ -1051,7 +1047,7 @@ impl DocFolder for Cache {
}); });
} }
} }
(Some(parent), None) if is_method || (!self.stripped_mod && !hidden_field)=> { (Some(parent), None) if is_method || (!self.stripped_mod)=> {
if parent.is_local() { if parent.is_local() {
// We have a parent, but we don't know where they're // We have a parent, but we don't know where they're
// defined yet. Wait for later to index this item. // defined yet. Wait for later to index this item.
@ -2165,8 +2161,7 @@ fn item_struct(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
document(w, cx, it)?; document(w, cx, it)?;
let mut fields = s.fields.iter().filter(|f| { let mut fields = s.fields.iter().filter(|f| {
match f.inner { match f.inner {
clean::StructFieldItem(clean::HiddenStructField) => false, clean::StructFieldItem(..) => true,
clean::StructFieldItem(clean::TypedStructField(..)) => true,
_ => false, _ => false,
} }
}).peekable(); }).peekable();
@ -2256,7 +2251,7 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
if let clean::VariantItem( Variant { kind: StructVariant(ref s) } ) = variant.inner { if let clean::VariantItem( Variant { kind: StructVariant(ref s) } ) = variant.inner {
let fields = s.fields.iter().filter(|f| { let fields = s.fields.iter().filter(|f| {
match f.inner { match f.inner {
clean::StructFieldItem(clean::TypedStructField(..)) => true, clean::StructFieldItem(..) => true,
_ => false, _ => false,
} }
}); });
@ -2315,24 +2310,17 @@ fn render_struct(w: &mut fmt::Formatter, it: &clean::Item,
match ty { match ty {
doctree::Plain => { doctree::Plain => {
write!(w, " {{\n{}", tab)?; write!(w, " {{\n{}", tab)?;
let mut fields_stripped = false;
for field in fields { for field in fields {
match field.inner { if let clean::StructFieldItem(ref ty) = field.inner {
clean::StructFieldItem(clean::HiddenStructField) => { write!(w, " {}{}: {},\n{}",
fields_stripped = true; VisSpace(field.visibility),
} field.name.as_ref().unwrap(),
clean::StructFieldItem(clean::TypedStructField(ref ty)) => { *ty,
write!(w, " {}{}: {},\n{}", tab)?;
VisSpace(field.visibility), }
field.name.as_ref().unwrap(),
*ty,
tab)?;
}
_ => unreachable!(),
};
} }
if fields_stripped { if it.has_stripped_fields().unwrap() {
write!(w, " // some fields omitted\n{}", tab)?; write!(w, " // some fields omitted\n{}", tab)?;
} }
write!(w, "}}")?; write!(w, "}}")?;
@ -2344,10 +2332,10 @@ fn render_struct(w: &mut fmt::Formatter, it: &clean::Item,
write!(w, ", ")?; write!(w, ", ")?;
} }
match field.inner { match field.inner {
clean::StructFieldItem(clean::HiddenStructField) => { clean::StrippedItem(box clean::StructFieldItem(..)) => {
write!(w, "_")? write!(w, "_")?
} }
clean::StructFieldItem(clean::TypedStructField(ref ty)) => { clean::StructFieldItem(ref ty) => {
write!(w, "{}{}", VisSpace(field.visibility), *ty)? write!(w, "{}{}", VisSpace(field.visibility), *ty)?
} }
_ => unreachable!() _ => unreachable!()

View file

@ -40,13 +40,9 @@ pub fn strip_hidden(krate: clean::Crate) -> plugins::PluginResult {
// use a dedicated hidden item for given item type if any // use a dedicated hidden item for given item type if any
match i.inner { match i.inner {
clean::StructFieldItem(..) => { clean::StructFieldItem(..) | clean::ModuleItem(..) => {
return Some(clean::Item { return Strip(i).fold()
inner: clean::StructFieldItem(clean::HiddenStructField),
..i
});
} }
clean::ModuleItem(..) => return Strip(i).fold(),
_ => return None, _ => return None,
} }
} }
@ -130,7 +126,8 @@ impl<'a> fold::DocFolder for Stripper<'a> {
clean::StructItem(..) | clean::EnumItem(..) | clean::StructItem(..) | clean::EnumItem(..) |
clean::TraitItem(..) | clean::FunctionItem(..) | clean::TraitItem(..) | clean::FunctionItem(..) |
clean::VariantItem(..) | clean::MethodItem(..) | clean::VariantItem(..) | clean::MethodItem(..) |
clean::ForeignFunctionItem(..) | clean::ForeignStaticItem(..) => { clean::ForeignFunctionItem(..) | clean::ForeignStaticItem(..) |
clean::ConstantItem(..) => {
if i.def_id.is_local() { if i.def_id.is_local() {
if !self.access_levels.is_exported(i.def_id) { if !self.access_levels.is_exported(i.def_id) {
return None; return None;
@ -138,18 +135,9 @@ impl<'a> fold::DocFolder for Stripper<'a> {
} }
} }
clean::ConstantItem(..) => {
if i.def_id.is_local() && !self.access_levels.is_exported(i.def_id) {
return None;
}
}
clean::StructFieldItem(..) => { clean::StructFieldItem(..) => {
if i.visibility != Some(hir::Public) { if i.visibility != Some(hir::Public) {
return Some(clean::Item { return Strip(i).fold();
inner: clean::StructFieldItem(clean::HiddenStructField),
..i
})
} }
} }

View file

@ -0,0 +1,44 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// @has structfields/struct.Foo.html
pub struct Foo {
// @has - //pre "pub a: ()"
pub a: (),
// @has - //pre "// some fields omitted"
// @!has - //pre "b: ()"
b: (),
// @!has - //pre "c: usize"
#[doc(hidden)]
c: usize,
// @has - //pre "pub d: usize"
pub d: usize,
}
// @has structfields/struct.Bar.html
pub struct Bar {
// @has - //pre "pub a: ()"
pub a: (),
// @!has - //pre "// some fields omitted"
}
// @has structfields/enum.Qux.html
pub enum Qux {
Quz {
// @has - //pre "a: ()"
a: (),
// @!has - //pre "b: ()"
#[doc(hidden)]
b: (),
// @has - //pre "c: usize"
c: usize,
// @has - //pre "// some fields omitted"
},
}