rustdoc: Display tuple structs correctly

The fields of tuple structs recently gained the ability to have privacy
associated with them, but rustdoc was not updated accodingly. This moves the
struct field filtering to the rendering phase in order to preserve the ordering
of struct fields to allow tuple structs to have their private fields printed as
underscores.

Closes #13594
This commit is contained in:
Alex Crichton 2014-04-19 22:24:52 -07:00
parent ba25fecfef
commit 9d546d60c1
3 changed files with 51 additions and 23 deletions

View File

@ -713,25 +713,24 @@ impl Clean<Type> for ast::Ty {
}
#[deriving(Clone, Encodable, Decodable)]
pub struct StructField {
pub type_: Type,
pub enum StructField {
HiddenStructField,
TypedStructField(Type),
}
impl Clean<Item> for ast::StructField {
fn clean(&self) -> Item {
let (name, vis) = match self.node.kind {
ast::NamedField(id, vis) => (Some(id), Some(vis)),
_ => (None, None)
ast::NamedField(id, vis) => (Some(id), vis),
ast::UnnamedField(vis) => (None, vis)
};
Item {
name: name.clean(),
attrs: self.node.attrs.clean().move_iter().collect(),
source: self.span.clean(),
visibility: vis,
visibility: Some(vis),
id: self.node.id,
inner: StructFieldItem(StructField {
type_: self.node.ty.clean(),
}),
inner: StructFieldItem(TypedStructField(self.node.ty.clean())),
}
}
}

View File

@ -1345,16 +1345,22 @@ fn item_struct(w: &mut Writer, it: &clean::Item,
Some(&s.generics),
s.struct_type,
s.fields.as_slice(),
s.fields_stripped,
"",
true));
try!(write!(w, "</pre>"));
try!(document(w, it));
let mut fields = s.fields.iter().filter(|f| {
match f.inner {
clean::StructFieldItem(clean::HiddenStructField) => false,
clean::StructFieldItem(clean::TypedStructField(..)) => true,
_ => false,
}
}).peekable();
match s.struct_type {
doctree::Plain if s.fields.len() > 0 => {
doctree::Plain if fields.peek().is_some() => {
try!(write!(w, "<h2 class='fields'>Fields</h2>\n<table>"));
for field in s.fields.iter() {
for field in fields {
try!(write!(w, "<tr><td id='structfield.{name}'>\
<code>{name}</code></td><td>",
name = field.name.get_ref().as_slice()));
@ -1400,7 +1406,6 @@ fn item_enum(w: &mut Writer, it: &clean::Item, e: &clean::Enum) -> fmt::Result {
None,
s.struct_type,
s.fields.as_slice(),
s.fields_stripped,
" ",
false));
}
@ -1429,9 +1434,18 @@ fn item_enum(w: &mut Writer, it: &clean::Item, e: &clean::Enum) -> fmt::Result {
clean::VariantItem(ref var) => {
match var.kind {
clean::StructVariant(ref s) => {
let mut fields = s.fields.iter().filter(|f| {
match f.inner {
clean::StructFieldItem(ref t) => match *t {
clean::HiddenStructField => false,
clean::TypedStructField(..) => true,
},
_ => false,
}
});
try!(write!(w, "<h3 class='fields'>Fields</h3>\n
<table>"));
for field in s.fields.iter() {
for field in fields {
try!(write!(w, "<tr><td \
id='variant.{v}.field.{f}'>\
<code>{f}</code></td><td>",
@ -1460,7 +1474,6 @@ fn render_struct(w: &mut Writer, it: &clean::Item,
g: Option<&clean::Generics>,
ty: doctree::StructType,
fields: &[clean::Item],
fields_stripped: bool,
tab: &str,
structhead: bool) -> fmt::Result {
try!(write!(w, "{}{}{}",
@ -1474,17 +1487,21 @@ fn render_struct(w: &mut Writer, it: &clean::Item,
match ty {
doctree::Plain => {
try!(write!(w, " \\{\n{}", tab));
let mut fields_stripped = false;
for field in fields.iter() {
match field.inner {
clean::StructFieldItem(ref ty) => {
clean::StructFieldItem(clean::HiddenStructField) => {
fields_stripped = true;
}
clean::StructFieldItem(clean::TypedStructField(ref ty)) => {
try!(write!(w, " {}{}: {},\n{}",
VisSpace(field.visibility),
field.name.get_ref().as_slice(),
ty.type_,
*ty,
tab));
}
_ => unreachable!()
}
_ => unreachable!(),
};
}
if fields_stripped {
@ -1499,8 +1516,11 @@ fn render_struct(w: &mut Writer, it: &clean::Item,
try!(write!(w, ", "));
}
match field.inner {
clean::StructFieldItem(ref field) => {
try!(write!(w, "{}", field.type_));
clean::StructFieldItem(clean::HiddenStructField) => {
try!(write!(w, "_"))
}
clean::StructFieldItem(clean::TypedStructField(ref ty)) => {
try!(write!(w, "{}{}", VisSpace(field.visibility), *ty))
}
_ => unreachable!()
}

View File

@ -129,12 +129,21 @@ impl<'a> fold::DocFolder for Stripper<'a> {
}
}
clean::ViewItemItem(..) | clean::StructFieldItem(..) => {
clean::ViewItemItem(..) => {
if i.visibility != Some(ast::Public) {
return None
}
}
clean::StructFieldItem(..) => {
if i.visibility != Some(ast::Public) {
return Some(clean::Item {
inner: clean::StructFieldItem(clean::HiddenStructField),
..i
})
}
}
// handled below
clean::ModuleItem(..) => {}