}
}
-/// Helper to render an optional visibility with a space after it (if the
-/// visibility is preset)
-#[derive(Copy, Clone)]
-pub struct VisSpace<'a>(pub &'a clean::Visibility);
-/// Similarly to VisSpace, this structure is used to render a function style with a
-/// space after it.
-#[derive(Copy, Clone)]
-pub struct UnsafetySpace(pub hir::Unsafety);
-/// Similarly to VisSpace, this structure is used to render a function constness
-/// with a space after it.
-#[derive(Copy, Clone)]
-pub struct ConstnessSpace(pub hir::Constness);
-/// Similarly to VisSpace, this structure is used to render a function asyncness
-/// with a space after it.
-#[derive(Copy, Clone)]
-pub struct AsyncSpace(pub hir::IsAsync);
-/// Similar to VisSpace, but used for mutability
-#[derive(Copy, Clone)]
-pub struct MutableSpace(pub clean::Mutability);
-pub struct AbiSpace(pub Abi);
-pub struct DefaultSpace(pub bool);
-
/// Wrapper struct for properly emitting a function or method declaration.
pub struct Function<'a> {
/// The declaration to emit.
clean::BareFunction(ref decl) => {
if f.alternate() {
write!(f, "{}{:#}fn{:#}{:#}",
- UnsafetySpace(decl.unsafety),
- AbiSpace(decl.abi),
+ decl.unsafety.print_with_space(),
+ print_abi_with_space(decl.abi),
decl.print_generic_params(),
decl.decl.print())
} else {
- write!(f, "{}{}", UnsafetySpace(decl.unsafety), AbiSpace(decl.abi))?;
+ write!(f, "{}{}",
+ decl.unsafety.print_with_space(), print_abi_with_space(decl.abi))?;
primitive_link(f, PrimitiveType::Fn, "fn")?;
write!(f, "{}{}", decl.print_generic_params(), decl.decl.print())
}
Some(l) => format!("{} ", l.print()),
_ => String::new()
};
- let m = MutableSpace(mutability);
+ let m = mutability.print_with_space();
let amp = if f.alternate() {
"&".to_string()
} else {
}
clean::SelfBorrowed(Some(ref lt), mtbl) => {
args.push_str(
- &format!("{}{} {}self", amp, lt.print(), MutableSpace(mtbl)));
+ &format!("{}{} {}self", amp, lt.print(), mtbl.print_with_space()));
args_plain.push_str(
- &format!("&{} {}self", lt.print(), MutableSpace(mtbl)));
+ &format!("&{} {}self", lt.print(), mtbl.print_with_space()));
}
clean::SelfBorrowed(None, mtbl) => {
- args.push_str(&format!("{}{}self", amp, MutableSpace(mtbl)));
- args_plain.push_str(&format!("&{}self", MutableSpace(mtbl)));
+ args.push_str(&format!("{}{}self", amp, mtbl.print_with_space()));
+ args_plain.push_str(&format!("&{}self", mtbl.print_with_space()));
}
clean::SelfExplicit(ref typ) => {
if f.alternate() {
}
}
-impl<'a> fmt::Display for VisSpace<'a> {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- fmt::Display::fmt(&self.0.print_with_space(), f)
- }
-}
-
impl clean::Visibility {
- fn print_with_space(&self) -> impl fmt::Display + '_ {
+ crate fn print_with_space(&self) -> impl fmt::Display + '_ {
display_fn(move |f| {
match *self {
clean::Public => f.write_str("pub "),
}
}
-impl fmt::Display for UnsafetySpace {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- match self.0 {
- hir::Unsafety::Unsafe => write!(f, "unsafe "),
- hir::Unsafety::Normal => Ok(())
+crate trait PrintWithSpace {
+ fn print_with_space(&self) -> &str;
+}
+
+impl PrintWithSpace for hir::Unsafety {
+ fn print_with_space(&self) -> &str {
+ match self {
+ hir::Unsafety::Unsafe => "unsafe ",
+ hir::Unsafety::Normal => ""
}
}
}
-impl fmt::Display for ConstnessSpace {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- match self.0 {
- hir::Constness::Const => write!(f, "const "),
- hir::Constness::NotConst => Ok(())
+impl PrintWithSpace for hir::Constness {
+ fn print_with_space(&self) -> &str {
+ match self {
+ hir::Constness::Const => "const ",
+ hir::Constness::NotConst => ""
}
}
}
-impl fmt::Display for AsyncSpace {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- match self.0 {
- hir::IsAsync::Async => write!(f, "async "),
- hir::IsAsync::NotAsync => Ok(()),
+impl PrintWithSpace for hir::IsAsync {
+ fn print_with_space(&self) -> &str {
+ match self {
+ hir::IsAsync::Async => "async ",
+ hir::IsAsync::NotAsync => "",
}
}
}
}
}
-impl fmt::Display for MutableSpace {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- match *self {
- MutableSpace(clean::Immutable) => Ok(()),
- MutableSpace(clean::Mutable) => write!(f, "mut "),
+impl clean::Mutability {
+ crate fn print_with_space(&self) -> &str {
+ match self {
+ clean::Immutable => "",
+ clean::Mutable => "mut ",
}
}
}
-impl fmt::Display for AbiSpace {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+crate fn print_abi_with_space(abi: Abi) -> impl fmt::Display {
+ display_fn(move |f| {
let quot = if f.alternate() { "\"" } else { """ };
- match self.0 {
+ match abi {
Abi::Rust => Ok(()),
abi => write!(f, "extern {0}{1}{0} ", quot, abi.name()),
}
- }
+ })
}
-impl fmt::Display for DefaultSpace {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- if self.0 {
- write!(f, "default ")
- } else {
- Ok(())
- }
+crate fn print_default_space<'a>(v: bool) -> &'a str {
+ if v {
+ "default "
+ } else {
+ ""
}
}
use crate::doctree;
use crate::fold::DocFolder;
use crate::html::escape::Escape;
-use crate::html::format::{Buffer, AsyncSpace, ConstnessSpace};
-use crate::html::format::{print_generic_bounds, WhereClause, href, AbiSpace, DefaultSpace};
-use crate::html::format::{VisSpace, Function, UnsafetySpace, MutableSpace};
+use crate::html::format::{Buffer, PrintWithSpace, print_abi_with_space};
+use crate::html::format::{print_generic_bounds, WhereClause, href, print_default_space};
+use crate::html::format::{Function};
use crate::html::format::fmt_impl_for_trait_page;
use crate::html::item_type::ItemType;
use crate::html::markdown::{self, Markdown, MarkdownHtml, MarkdownSummaryLine, ErrorCodes, IdMap};
match *src {
Some(ref src) => {
write!(w, "<tr><td><code>{}extern crate {} as {};",
- VisSpace(&myitem.visibility),
+ myitem.visibility.print_with_space(),
anchor(myitem.def_id, src),
name)
}
None => {
write!(w, "<tr><td><code>{}extern crate {};",
- VisSpace(&myitem.visibility),
+ myitem.visibility.print_with_space(),
anchor(myitem.def_id, name))
}
}
clean::ImportItem(ref import) => {
write!(w, "<tr><td><code>{}{}</code></td></tr>",
- VisSpace(&myitem.visibility), import.print());
+ myitem.visibility.print_with_space(), import.print());
}
_ => {
render_attributes(w, it, false);
write!(w, "{vis}const \
{name}: {typ}</pre>",
- vis = VisSpace(&it.visibility),
+ vis = it.visibility.print_with_space(),
name = it.name.as_ref().unwrap(),
typ = c.type_.print());
document(w, cx, it)
render_attributes(w, it, false);
write!(w, "{vis}static {mutability}\
{name}: {typ}</pre>",
- vis = VisSpace(&it.visibility),
- mutability = MutableSpace(s.mutability),
+ vis = it.visibility.print_with_space(),
+ mutability = s.mutability.print_with_space(),
name = it.name.as_ref().unwrap(),
typ = s.type_.print());
document(w, cx, it)
fn item_function(w: &mut Buffer, cx: &Context, it: &clean::Item, f: &clean::Function) {
let header_len = format!(
"{}{}{}{}{:#}fn {}{:#}",
- VisSpace(&it.visibility),
- ConstnessSpace(f.header.constness),
- UnsafetySpace(f.header.unsafety),
- AsyncSpace(f.header.asyncness),
- AbiSpace(f.header.abi),
+ it.visibility.print_with_space(),
+ f.header.constness.print_with_space(),
+ f.header.unsafety.print_with_space(),
+ f.header.asyncness.print_with_space(),
+ print_abi_with_space(f.header.abi),
it.name.as_ref().unwrap(),
f.generics.print()
).len();
write!(w,
"{vis}{constness}{unsafety}{asyncness}{abi}fn \
{name}{generics}{decl}{where_clause}</pre>",
- vis = VisSpace(&it.visibility),
- constness = ConstnessSpace(f.header.constness),
- unsafety = UnsafetySpace(f.header.unsafety),
- asyncness = AsyncSpace(f.header.asyncness),
- abi = AbiSpace(f.header.abi),
+ vis = it.visibility.print_with_space(),
+ constness = f.header.constness.print_with_space(),
+ unsafety = f.header.unsafety.print_with_space(),
+ asyncness = f.header.asyncness.print_with_space(),
+ abi = print_abi_with_space(f.header.abi),
name = it.name.as_ref().unwrap(),
generics = f.generics.print(),
where_clause = WhereClause { gens: &f.generics, indent: 0, end_newline: true },
write!(w, "<pre class='rust trait'>");
render_attributes(w, it, true);
write!(w, "{}{}{}trait {}{}{}",
- VisSpace(&it.visibility),
- UnsafetySpace(t.unsafety),
+ it.visibility.print_with_space(),
+ t.unsafety.print_with_space(),
if t.is_auto { "auto " } else { "" },
it.name.as_ref().unwrap(),
t.generics.print(),
extra: &str) {
write!(w, "{}{}const <a href='{}' class=\"constant\"><b>{}</b></a>: {}",
extra,
- VisSpace(&it.visibility),
+ it.visibility.print_with_space(),
naive_assoc_href(it, link),
it.name.as_ref().unwrap(),
ty.print());
};
let mut header_len = format!(
"{}{}{}{}{}{:#}fn {}{:#}",
- VisSpace(&meth.visibility),
- ConstnessSpace(header.constness),
- UnsafetySpace(header.unsafety),
- AsyncSpace(header.asyncness),
- DefaultSpace(meth.is_default()),
- AbiSpace(header.abi),
+ meth.visibility.print_with_space(),
+ header.constness.print_with_space(),
+ header.unsafety.print_with_space(),
+ header.asyncness.print_with_space(),
+ print_default_space(meth.is_default()),
+ print_abi_with_space(header.abi),
name,
g.print()
).len();
write!(w, "{}{}{}{}{}{}{}fn <a href='{href}' class='fnname'>{name}</a>\
{generics}{decl}{where_clause}",
if parent == ItemType::Trait { " " } else { "" },
- VisSpace(&meth.visibility),
- ConstnessSpace(header.constness),
- UnsafetySpace(header.unsafety),
- AsyncSpace(header.asyncness),
- DefaultSpace(meth.is_default()),
- AbiSpace(header.abi),
+ meth.visibility.print_with_space(),
+ header.constness.print_with_space(),
+ header.unsafety.print_with_space(),
+ header.asyncness.print_with_space(),
+ print_default_space(meth.is_default()),
+ print_abi_with_space(header.abi),
href = href,
name = name,
generics = g.print(),
write!(w, "<pre class='rust enum'>");
render_attributes(w, it, true);
write!(w, "{}enum {}{}{}",
- VisSpace(&it.visibility),
+ it.visibility.print_with_space(),
it.name.as_ref().unwrap(),
e.generics.print(),
WhereClause { gens: &e.generics, indent: 0, end_newline: true });
tab: &str,
structhead: bool) {
write!(w, "{}{}{}",
- VisSpace(&it.visibility),
+ it.visibility.print_with_space(),
if structhead {"struct "} else {""},
it.name.as_ref().unwrap());
if let Some(g) = g {
if let clean::StructFieldItem(ref ty) = field.inner {
write!(w, "\n{} {}{}: {},",
tab,
- VisSpace(&field.visibility),
+ field.visibility.print_with_space(),
field.name.as_ref().unwrap(),
ty.print());
has_visible_fields = true;
write!(w, "_")
}
clean::StructFieldItem(ref ty) => {
- write!(w, "{}{}", VisSpace(&field.visibility), ty.print())
+ write!(w, "{}{}", field.visibility.print_with_space(), ty.print())
}
_ => unreachable!()
}
tab: &str,
structhead: bool) {
write!(w, "{}{}{}",
- VisSpace(&it.visibility),
+ it.visibility.print_with_space(),
if structhead {"union "} else {""},
it.name.as_ref().unwrap());
if let Some(g) = g {
for field in fields {
if let clean::StructFieldItem(ref ty) = field.inner {
write!(w, " {}{}: {},\n{}",
- VisSpace(&field.visibility),
+ field.visibility.print_with_space(),
field.name.as_ref().unwrap(),
ty.print(),
tab);
write!(
w,
" {}type {};\n}}</pre>",
- VisSpace(&it.visibility),
+ it.visibility.print_with_space(),
it.name.as_ref().unwrap(),
);