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

@ -652,7 +652,7 @@ pub enum Type {
Proc(~ClosureDecl), Proc(~ClosureDecl),
/// extern "ABI" fn /// extern "ABI" fn
BareFunction(~BareFunctionDecl), BareFunction(~BareFunctionDecl),
Tuple(Vec<Type> ), Tuple(Vec<Type>),
Vector(~Type), Vector(~Type),
FixedVector(~Type, ~str), FixedVector(~Type, ~str),
String, String,
@ -713,25 +713,24 @@ impl Clean<Type> for ast::Ty {
} }
#[deriving(Clone, Encodable, Decodable)] #[deriving(Clone, Encodable, Decodable)]
pub struct StructField { pub enum StructField {
pub type_: Type, HiddenStructField,
TypedStructField(Type),
} }
impl Clean<Item> for ast::StructField { impl Clean<Item> for ast::StructField {
fn clean(&self) -> Item { fn clean(&self) -> Item {
let (name, vis) = match self.node.kind { let (name, vis) = match self.node.kind {
ast::NamedField(id, vis) => (Some(id), Some(vis)), ast::NamedField(id, vis) => (Some(id), vis),
_ => (None, None) ast::UnnamedField(vis) => (None, vis)
}; };
Item { Item {
name: name.clean(), name: name.clean(),
attrs: self.node.attrs.clean().move_iter().collect(), attrs: self.node.attrs.clean().move_iter().collect(),
source: self.span.clean(), source: self.span.clean(),
visibility: vis, visibility: Some(vis),
id: self.node.id, id: self.node.id,
inner: StructFieldItem(StructField { inner: StructFieldItem(TypedStructField(self.node.ty.clean())),
type_: self.node.ty.clean(),
}),
} }
} }
} }
@ -837,7 +836,7 @@ impl Clean<Item> for doctree::Variant {
#[deriving(Clone, Encodable, Decodable)] #[deriving(Clone, Encodable, Decodable)]
pub enum VariantKind { pub enum VariantKind {
CLikeVariant, CLikeVariant,
TupleVariant(Vec<Type> ), TupleVariant(Vec<Type>),
StructVariant(VariantStruct), StructVariant(VariantStruct),
} }

View File

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