Skip to content

Commit 01d58fe

Browse files
committed
rustdoc: Implement constant documentation
At the same time, migrate statics to constants.
1 parent 1bfe450 commit 01d58fe

File tree

9 files changed

+120
-46
lines changed

9 files changed

+120
-46
lines changed

src/librustdoc/clean/mod.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ pub enum ItemEnum {
300300
ModuleItem(Module),
301301
TypedefItem(Typedef),
302302
StaticItem(Static),
303+
ConstantItem(Constant),
303304
TraitItem(Trait),
304305
ImplItem(Impl),
305306
/// `use` and `extern crate`
@@ -347,6 +348,7 @@ impl Clean<Item> for doctree::Module {
347348
self.mods.clean(cx),
348349
self.typedefs.clean(cx),
349350
self.statics.clean(cx),
351+
self.constants.clean(cx),
350352
self.traits.clean(cx),
351353
self.impls.clean(cx),
352354
self.view_items.clean(cx).into_iter()
@@ -1741,6 +1743,29 @@ impl Clean<Item> for doctree::Static {
17411743
}
17421744
}
17431745

1746+
#[deriving(Clone, Encodable, Decodable)]
1747+
pub struct Constant {
1748+
pub type_: Type,
1749+
pub expr: String,
1750+
}
1751+
1752+
impl Clean<Item> for doctree::Constant {
1753+
fn clean(&self, cx: &DocContext) -> Item {
1754+
Item {
1755+
name: Some(self.name.clean(cx)),
1756+
attrs: self.attrs.clean(cx),
1757+
source: self.whence.clean(cx),
1758+
def_id: ast_util::local_def(self.id),
1759+
visibility: self.vis.clean(cx),
1760+
stability: self.stab.clean(cx),
1761+
inner: ConstantItem(Constant {
1762+
type_: self.type_.clean(cx),
1763+
expr: self.expr.span.to_src(cx),
1764+
}),
1765+
}
1766+
}
1767+
}
1768+
17441769
#[deriving(Show, Clone, Encodable, Decodable, PartialEq)]
17451770
pub enum Mutability {
17461771
Mutable,

src/librustdoc/doctree.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub struct Module {
3030
pub id: NodeId,
3131
pub typedefs: Vec<Typedef>,
3232
pub statics: Vec<Static>,
33+
pub constants: Vec<Constant>,
3334
pub traits: Vec<Trait>,
3435
pub vis: ast::Visibility,
3536
pub stab: Option<attr::Stability>,
@@ -56,6 +57,7 @@ impl Module {
5657
mods : Vec::new(),
5758
typedefs : Vec::new(),
5859
statics : Vec::new(),
60+
constants : Vec::new(),
5961
traits : Vec::new(),
6062
impls : Vec::new(),
6163
view_items : Vec::new(),
@@ -151,6 +153,17 @@ pub struct Static {
151153
pub whence: Span,
152154
}
153155

156+
pub struct Constant {
157+
pub type_: P<ast::Ty>,
158+
pub expr: P<ast::Expr>,
159+
pub name: Ident,
160+
pub attrs: Vec<ast::Attribute>,
161+
pub vis: ast::Visibility,
162+
pub stab: Option<attr::Stability>,
163+
pub id: ast::NodeId,
164+
pub whence: Span,
165+
}
166+
154167
pub struct Trait {
155168
pub name: Ident,
156169
pub items: Vec<ast::TraitItem>, //should be TraitItem

src/librustdoc/flock.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ mod imp {
3838
pub l_sysid: libc::c_int,
3939
}
4040

41-
pub static F_WRLCK: libc::c_short = 1;
42-
pub static F_UNLCK: libc::c_short = 2;
43-
pub static F_SETLK: libc::c_int = 6;
44-
pub static F_SETLKW: libc::c_int = 7;
41+
pub const F_WRLCK: libc::c_short = 1;
42+
pub const F_UNLCK: libc::c_short = 2;
43+
pub const F_SETLK: libc::c_int = 6;
44+
pub const F_SETLKW: libc::c_int = 7;
4545
}
4646

4747
#[cfg(target_os = "freebsd")]
@@ -57,10 +57,10 @@ mod imp {
5757
pub l_sysid: libc::c_int,
5858
}
5959

60-
pub static F_UNLCK: libc::c_short = 2;
61-
pub static F_WRLCK: libc::c_short = 3;
62-
pub static F_SETLK: libc::c_int = 12;
63-
pub static F_SETLKW: libc::c_int = 13;
60+
pub const F_UNLCK: libc::c_short = 2;
61+
pub const F_WRLCK: libc::c_short = 3;
62+
pub const F_SETLK: libc::c_int = 12;
63+
pub const F_SETLKW: libc::c_int = 13;
6464
}
6565

6666
#[cfg(target_os = "dragonfly")]
@@ -78,10 +78,10 @@ mod imp {
7878
pub l_sysid: libc::c_int,
7979
}
8080

81-
pub static F_UNLCK: libc::c_short = 2;
82-
pub static F_WRLCK: libc::c_short = 3;
83-
pub static F_SETLK: libc::c_int = 8;
84-
pub static F_SETLKW: libc::c_int = 9;
81+
pub const F_UNLCK: libc::c_short = 2;
82+
pub const F_WRLCK: libc::c_short = 3;
83+
pub const F_SETLK: libc::c_int = 8;
84+
pub const F_SETLKW: libc::c_int = 9;
8585
}
8686

8787
#[cfg(any(target_os = "macos", target_os = "ios"))]
@@ -99,10 +99,10 @@ mod imp {
9999
pub l_sysid: libc::c_int,
100100
}
101101

102-
pub static F_UNLCK: libc::c_short = 2;
103-
pub static F_WRLCK: libc::c_short = 3;
104-
pub static F_SETLK: libc::c_int = 8;
105-
pub static F_SETLKW: libc::c_int = 9;
102+
pub const F_UNLCK: libc::c_short = 2;
103+
pub const F_WRLCK: libc::c_short = 3;
104+
pub const F_SETLK: libc::c_int = 8;
105+
pub const F_SETLKW: libc::c_int = 9;
106106
}
107107

108108
pub struct Lock {

src/librustdoc/html/item_type.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub enum ItemType {
3939
Macro = 15,
4040
Primitive = 16,
4141
AssociatedType = 17,
42+
Constant = 18,
4243
}
4344

4445
impl ItemType {
@@ -62,6 +63,7 @@ impl ItemType {
6263
Macro => "macro",
6364
Primitive => "primitive",
6465
AssociatedType => "associatedtype",
66+
Constant => "constant",
6567
}
6668
}
6769
}
@@ -86,6 +88,7 @@ pub fn shortty(item: &clean::Item) -> ItemType {
8688
clean::FunctionItem(..) => Function,
8789
clean::TypedefItem(..) => Typedef,
8890
clean::StaticItem(..) => Static,
91+
clean::ConstantItem(..) => Constant,
8992
clean::TraitItem(..) => Trait,
9093
clean::ImplItem(..) => Impl,
9194
clean::ViewItemItem(..) => ViewItem,

src/librustdoc/html/markdown.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,16 @@ pub struct Markdown<'a>(pub &'a str);
4848
/// table of contents.
4949
pub struct MarkdownWithToc<'a>(pub &'a str);
5050

51-
static DEF_OUNIT: libc::size_t = 64;
52-
static HOEDOWN_EXT_NO_INTRA_EMPHASIS: libc::c_uint = 1 << 10;
53-
static HOEDOWN_EXT_TABLES: libc::c_uint = 1 << 0;
54-
static HOEDOWN_EXT_FENCED_CODE: libc::c_uint = 1 << 1;
55-
static HOEDOWN_EXT_AUTOLINK: libc::c_uint = 1 << 3;
56-
static HOEDOWN_EXT_STRIKETHROUGH: libc::c_uint = 1 << 4;
57-
static HOEDOWN_EXT_SUPERSCRIPT: libc::c_uint = 1 << 8;
58-
static HOEDOWN_EXT_FOOTNOTES: libc::c_uint = 1 << 2;
59-
60-
static HOEDOWN_EXTENSIONS: libc::c_uint =
51+
const DEF_OUNIT: libc::size_t = 64;
52+
const HOEDOWN_EXT_NO_INTRA_EMPHASIS: libc::c_uint = 1 << 10;
53+
const HOEDOWN_EXT_TABLES: libc::c_uint = 1 << 0;
54+
const HOEDOWN_EXT_FENCED_CODE: libc::c_uint = 1 << 1;
55+
const HOEDOWN_EXT_AUTOLINK: libc::c_uint = 1 << 3;
56+
const HOEDOWN_EXT_STRIKETHROUGH: libc::c_uint = 1 << 4;
57+
const HOEDOWN_EXT_SUPERSCRIPT: libc::c_uint = 1 << 8;
58+
const HOEDOWN_EXT_FOOTNOTES: libc::c_uint = 1 << 2;
59+
60+
const HOEDOWN_EXTENSIONS: libc::c_uint =
6161
HOEDOWN_EXT_NO_INTRA_EMPHASIS | HOEDOWN_EXT_TABLES |
6262
HOEDOWN_EXT_FENCED_CODE | HOEDOWN_EXT_AUTOLINK |
6363
HOEDOWN_EXT_STRIKETHROUGH | HOEDOWN_EXT_SUPERSCRIPT |

src/librustdoc/html/render.rs

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,6 +1471,8 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context,
14711471
(_, &clean::StructItem(..)) => Greater,
14721472
(&clean::EnumItem(..), _) => Less,
14731473
(_, &clean::EnumItem(..)) => Greater,
1474+
(&clean::ConstantItem(..), _) => Less,
1475+
(_, &clean::ConstantItem(..)) => Greater,
14741476
(&clean::StaticItem(..), _) => Less,
14751477
(_, &clean::StaticItem(..)) => Greater,
14761478
(&clean::ForeignFunctionItem(..), _) => Less,
@@ -1507,6 +1509,7 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context,
15071509
clean::FunctionItem(..) => ("functions", "Functions"),
15081510
clean::TypedefItem(..) => ("types", "Type Definitions"),
15091511
clean::StaticItem(..) => ("statics", "Statics"),
1512+
clean::ConstantItem(..) => ("constants", "Constants"),
15101513
clean::TraitItem(..) => ("traits", "Traits"),
15111514
clean::ImplItem(..) => ("impls", "Implementations"),
15121515
clean::ViewItemItem(..) => ("reexports", "Reexports"),
@@ -1526,28 +1529,28 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context,
15261529
id = short, name = name));
15271530
}
15281531

1529-
match myitem.inner {
1530-
clean::StaticItem(ref s) | clean::ForeignStaticItem(ref s) => {
1531-
struct Initializer<'a>(&'a str, Item<'a>);
1532-
impl<'a> fmt::Show for Initializer<'a> {
1533-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1534-
let Initializer(s, item) = *self;
1535-
if s.len() == 0 { return Ok(()); }
1536-
try!(write!(f, "<code> = </code>"));
1537-
if s.contains("\n") {
1538-
match item.href() {
1539-
Some(url) => {
1540-
write!(f, "<a href='{}'>[definition]</a>",
1541-
url)
1542-
}
1543-
None => Ok(()),
1544-
}
1545-
} else {
1546-
write!(f, "<code>{}</code>", s.as_slice())
1532+
struct Initializer<'a>(&'a str, Item<'a>);
1533+
impl<'a> fmt::Show for Initializer<'a> {
1534+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1535+
let Initializer(s, item) = *self;
1536+
if s.len() == 0 { return Ok(()); }
1537+
try!(write!(f, "<code> = </code>"));
1538+
if s.contains("\n") {
1539+
match item.href() {
1540+
Some(url) => {
1541+
write!(f, "<a href='{}'>[definition]</a>",
1542+
url)
15471543
}
1544+
None => Ok(()),
15481545
}
1546+
} else {
1547+
write!(f, "<code>{}</code>", s.as_slice())
15491548
}
1549+
}
1550+
}
15501551

1552+
match myitem.inner {
1553+
clean::StaticItem(ref s) | clean::ForeignStaticItem(ref s) => {
15511554
try!(write!(w, "
15521555
<tr>
15531556
<td>{}<code>{}static {}{}: {}</code>{}</td>
@@ -1562,6 +1565,20 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context,
15621565
Initializer(s.expr.as_slice(), Item { cx: cx, item: myitem }),
15631566
Markdown(blank(myitem.doc_value()))));
15641567
}
1568+
clean::ConstantItem(ref s) => {
1569+
try!(write!(w, "
1570+
<tr>
1571+
<td>{}<code>{}const {}: {}</code>{}</td>
1572+
<td class='docblock'>{}&nbsp;</td>
1573+
</tr>
1574+
",
1575+
ConciseStability(&myitem.stability),
1576+
VisSpace(myitem.visibility),
1577+
*myitem.name.get_ref(),
1578+
s.type_,
1579+
Initializer(s.expr.as_slice(), Item { cx: cx, item: myitem }),
1580+
Markdown(blank(myitem.doc_value()))));
1581+
}
15651582

15661583
clean::ViewItemItem(ref item) => {
15671584
match item.inner {

src/librustdoc/html/static/main.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,9 @@
569569
"ffi",
570570
"ffs",
571571
"macro",
572-
"primitive"];
572+
"primitive",
573+
"associatedtype",
574+
"constant"];
573575

574576
function itemTypeFromName(typename) {
575577
for (var i = 0; i < itemTypes.length; ++i) {

src/librustdoc/passes.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ impl<'a> fold::DocFolder for Stripper<'a> {
134134
clean::StructItem(..) | clean::EnumItem(..) |
135135
clean::TraitItem(..) | clean::FunctionItem(..) |
136136
clean::VariantItem(..) | clean::MethodItem(..) |
137-
clean::ForeignFunctionItem(..) | clean::ForeignStaticItem(..) => {
137+
clean::ForeignFunctionItem(..) | clean::ForeignStaticItem(..) |
138+
clean::ConstantItem(..) => {
138139
if ast_util::is_local(i.def_id) &&
139140
!self.exported_items.contains(&i.def_id.node) {
140141
return None;

src/librustdoc/visit_ast.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,19 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
308308
};
309309
om.statics.push(s);
310310
},
311+
ast::ItemConst(ref ty, ref exp) => {
312+
let s = Constant {
313+
type_: ty.clone(),
314+
expr: exp.clone(),
315+
id: item.id,
316+
name: name,
317+
attrs: item.attrs.clone(),
318+
whence: item.span,
319+
vis: item.vis,
320+
stab: self.stability(item.id),
321+
};
322+
om.constants.push(s);
323+
},
311324
ast::ItemTrait(ref gen, _, ref b, ref items) => {
312325
let t = Trait {
313326
name: name,

0 commit comments

Comments
 (0)