syntax: parse const fn for free functions and inherent methods.

This commit is contained in:
Eduard Burtescu 2015-02-25 22:05:07 +02:00 committed by Niko Matsakis
parent bc6318d2be
commit af3795721c
34 changed files with 218 additions and 88 deletions

View file

@ -35,6 +35,10 @@ pub struct VisSpace(pub Option<ast::Visibility>);
/// space after it.
#[derive(Copy, Clone)]
pub struct UnsafetySpace(pub ast::Unsafety);
/// Similarly to VisSpace, this structure is used to render a function constness
/// with a space after it.
#[derive(Copy)]
pub struct ConstnessSpace(pub ast::Constness);
/// Wrapper struct for properly emitting a method declaration.
pub struct Method<'a>(pub &'a clean::SelfTy, pub &'a clean::FnDecl);
/// Similar to VisSpace, but used for mutability
@ -63,6 +67,12 @@ impl UnsafetySpace {
}
}
impl ConstnessSpace {
pub fn get(&self) -> ast::Constness {
let ConstnessSpace(v) = *self; v
}
}
impl<'a, T: fmt::Display> fmt::Display for CommaSep<'a, T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for (i, item) in self.0.iter().enumerate() {
@ -607,6 +617,15 @@ impl fmt::Display for UnsafetySpace {
}
}
impl fmt::Display for ConstnessSpace {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.get() {
ast::Constness::Const => write!(f, "const "),
ast::Constness::NotConst => Ok(())
}
}
}
impl fmt::Display for clean::Import {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {

View file

@ -63,6 +63,7 @@ use clean;
use doctree;
use fold::DocFolder;
use html::escape::Escape;
use html::format::{ConstnessSpace};
use html::format::{TyParamBounds, WhereClause, href, AbiSpace};
use html::format::{VisSpace, Method, UnsafetySpace, MutableSpace};
use html::highlight;
@ -1753,11 +1754,12 @@ fn item_static(w: &mut fmt::Formatter, it: &clean::Item,
fn item_function(w: &mut fmt::Formatter, it: &clean::Item,
f: &clean::Function) -> fmt::Result {
try!(write!(w, "<pre class='rust fn'>{vis}{unsafety}{abi}fn \
try!(write!(w, "<pre class='rust fn'>{vis}{unsafety}{abi}{constness}fn \
{name}{generics}{decl}{where_clause}</pre>",
vis = VisSpace(it.visibility),
unsafety = UnsafetySpace(f.unsafety),
abi = AbiSpace(f.abi),
constness = ConstnessSpace(f.constness),
name = it.name.as_ref().unwrap(),
generics = f.generics,
where_clause = WhereClause(&f.generics),
@ -1957,10 +1959,16 @@ fn assoc_type(w: &mut fmt::Formatter, it: &clean::Item,
fn render_assoc_item(w: &mut fmt::Formatter, meth: &clean::Item,
link: AssocItemLink) -> fmt::Result {
fn method(w: &mut fmt::Formatter, it: &clean::Item,
unsafety: ast::Unsafety, abi: abi::Abi,
g: &clean::Generics, selfty: &clean::SelfTy,
d: &clean::FnDecl, link: AssocItemLink) -> fmt::Result {
fn method(w: &mut fmt::Formatter,
it: &clean::Item,
unsafety: ast::Unsafety,
constness: ast::Constness,
abi: abi::Abi,
g: &clean::Generics,
selfty: &clean::SelfTy,
d: &clean::FnDecl,
link: AssocItemLink)
-> fmt::Result {
use syntax::abi::Abi;
let name = it.name.as_ref().unwrap();
@ -1971,12 +1979,10 @@ fn render_assoc_item(w: &mut fmt::Formatter, meth: &clean::Item,
href(did).map(|p| format!("{}{}", p.0, anchor)).unwrap_or(anchor)
}
};
write!(w, "{}{}fn <a href='{href}' class='fnname'>{name}</a>\
write!(w, "{}{}{}fn <a href='{href}' class='fnname'>{name}</a>\
{generics}{decl}{where_clause}",
match unsafety {
ast::Unsafety::Unsafe => "unsafe ",
_ => "",
},
UnsafetySpace(unsafety),
ConstnessSpace(constness),
match abi {
Abi::Rust => String::new(),
a => format!("extern {} ", a.to_string())
@ -1989,11 +1995,12 @@ fn render_assoc_item(w: &mut fmt::Formatter, meth: &clean::Item,
}
match meth.inner {
clean::TyMethodItem(ref m) => {
method(w, meth, m.unsafety, m.abi, &m.generics, &m.self_, &m.decl,
link)
method(w, meth, m.unsafety, ast::Constness::NotConst,
m.abi, &m.generics, &m.self_, &m.decl, link)
}
clean::MethodItem(ref m) => {
method(w, meth, m.unsafety, m.abi, &m.generics, &m.self_, &m.decl,
method(w, meth, m.unsafety, m.constness,
m.abi, &m.generics, &m.self_, &m.decl,
link)
}
clean::AssociatedConstItem(ref ty, ref default) => {