]> git.lizzy.rs Git - rust.git/commitdiff
Remove *Space wrappers in favor of direct impls or functions
authorMark Rousskov <mark.simulacrum@gmail.com>
Fri, 13 Sep 2019 12:36:00 +0000 (08:36 -0400)
committerMark Rousskov <mark.simulacrum@gmail.com>
Fri, 13 Sep 2019 23:44:44 +0000 (19:44 -0400)
src/librustdoc/html/format.rs
src/librustdoc/html/render.rs

index 3c62977bd77d37f7ff045fe8bd8660daf9cf6461..fafd43cb60b69a4cb72a6e83ef8e1a71ba348746 100644 (file)
@@ -108,28 +108,6 @@ impl Buffer {
     }
 }
 
-/// 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.
@@ -638,12 +616,13 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter<'_>, use_absolute: bool) ->
         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())
             }
@@ -705,7 +684,7 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter<'_>, use_absolute: bool) ->
                 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 {
@@ -956,13 +935,13 @@ impl Function<'_> {
                         }
                         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() {
@@ -1032,14 +1011,8 @@ impl Function<'_> {
     }
 }
 
-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 "),
@@ -1060,29 +1033,33 @@ fn print_with_space(&self) -> impl fmt::Display + '_ {
     }
 }
 
-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 => "",
         }
     }
 }
@@ -1156,32 +1133,30 @@ impl clean::TypeBinding {
     }
 }
 
-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 { "&quot;" };
-        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 {
+        ""
     }
 }
 
index 32fa2daa026f7cf2f8bc39d896263956f3b2354a..0b3cf5ea1e226716e4f70de3d7696bb2c6ed1be2 100644 (file)
@@ -65,9 +65,9 @@
 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};
@@ -2573,13 +2573,13 @@ fn cmp(i1: &clean::Item, i2: &clean::Item, idx1: usize, idx2: usize) -> Ordering
                 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))
                     }
                 }
@@ -2588,7 +2588,7 @@ fn cmp(i1: &clean::Item, i2: &clean::Item, idx1: usize, idx2: usize) -> Ordering
 
             clean::ImportItem(ref import) => {
                 write!(w, "<tr><td><code>{}{}</code></td></tr>",
-                       VisSpace(&myitem.visibility), import.print());
+                       myitem.visibility.print_with_space(), import.print());
             }
 
             _ => {
@@ -2794,7 +2794,7 @@ fn item_constant(w: &mut Buffer, cx: &Context, it: &clean::Item, c: &clean::Cons
     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)
@@ -2805,8 +2805,8 @@ fn item_static(w: &mut Buffer, cx: &Context, it: &clean::Item, s: &clean::Static
     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)
@@ -2815,11 +2815,11 @@ fn item_static(w: &mut Buffer, cx: &Context, it: &clean::Item, s: &clean::Static
 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();
@@ -2828,11 +2828,11 @@ fn item_function(w: &mut Buffer, cx: &Context, it: &clean::Item, f: &clean::Func
     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 },
@@ -2913,8 +2913,8 @@ fn item_trait(
         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(),
@@ -3175,7 +3175,7 @@ fn assoc_const(w: &mut Buffer,
                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());
@@ -3240,12 +3240,12 @@ fn method(w: &mut Buffer,
         };
         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();
@@ -3259,12 +3259,12 @@ fn method(w: &mut Buffer,
         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(),
@@ -3399,7 +3399,7 @@ fn item_enum(w: &mut Buffer, cx: &Context, it: &clean::Item, e: &clean::Enum) {
         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 });
@@ -3588,7 +3588,7 @@ fn render_struct(w: &mut Buffer, it: &clean::Item,
                  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 {
@@ -3605,7 +3605,7 @@ fn render_struct(w: &mut Buffer, it: &clean::Item,
                 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;
@@ -3635,7 +3635,7 @@ fn render_struct(w: &mut Buffer, it: &clean::Item,
                         write!(w, "_")
                     }
                     clean::StructFieldItem(ref ty) => {
-                        write!(w, "{}{}", VisSpace(&field.visibility), ty.print())
+                        write!(w, "{}{}", field.visibility.print_with_space(), ty.print())
                     }
                     _ => unreachable!()
                 }
@@ -3662,7 +3662,7 @@ fn render_union(w: &mut Buffer, it: &clean::Item,
                 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 {
@@ -3674,7 +3674,7 @@ fn render_union(w: &mut Buffer, it: &clean::Item,
     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);
@@ -4186,7 +4186,7 @@ fn item_foreign_type(w: &mut Buffer, cx: &Context, it: &clean::Item) {
     write!(
         w,
         "    {}type {};\n}}</pre>",
-        VisSpace(&it.visibility),
+        it.visibility.print_with_space(),
         it.name.as_ref().unwrap(),
     );