]> git.lizzy.rs Git - rust.git/blobdiff - src/librustdoc/html/format.rs
Sort auto trait and blanket implementations display
[rust.git] / src / librustdoc / html / format.rs
index dcd32192ff3842f54cec4a421bb3f2f039a74b6d..fd620d467de48dd05a785dc17e6a46deb488c321 100644 (file)
@@ -63,6 +63,13 @@ impl Buffer {
         }
     }
 
+    crate fn new() -> Buffer {
+        Buffer {
+            for_html: false,
+            buffer: String::new(),
+        }
+    }
+
     crate fn is_empty(&self) -> bool {
         self.buffer.is_empty()
     }
@@ -99,10 +106,6 @@ impl Buffer {
         self.into_inner()
     }
 
-    crate fn with_formatter<T: FnOnce(&mut fmt::Formatter<'_>) -> fmt::Result>(&mut self, t: T) {
-        self.from_display(display_fn(move |f| (t)(f)));
-    }
-
     crate fn from_display<T: std::fmt::Display>(&mut self, t: T) {
         if self.for_html {
             write!(self, "{}", t);
@@ -110,31 +113,11 @@ impl Buffer {
             write!(self, "{:#}", t);
         }
     }
-}
 
-/// 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 Option<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);
-/// Wrapper struct for emitting type parameter bounds.
-pub struct GenericBounds<'a>(pub &'a [clean::GenericBound]);
-pub struct AbiSpace(pub Abi);
-pub struct DefaultSpace(pub bool);
+    crate fn is_for_html(&self) -> bool {
+        self.for_html
+    }
+}
 
 /// Wrapper struct for properly emitting a function or method declaration.
 pub struct Function<'a> {
@@ -161,102 +144,89 @@ pub struct WhereClause<'a>{
     pub end_newline: bool,
 }
 
-impl<'a> VisSpace<'a> {
-    pub fn get(self) -> &'a Option<clean::Visibility> {
-        let VisSpace(v) = self; v
-    }
-}
-
-impl UnsafetySpace {
-    pub fn get(&self) -> hir::Unsafety {
-        let UnsafetySpace(v) = *self; v
-    }
-}
-
-impl ConstnessSpace {
-    pub fn get(&self) -> hir::Constness {
-        let ConstnessSpace(v) = *self; v
-    }
-}
-
-fn comma_sep<T: fmt::Display>(items: &[T]) -> impl fmt::Display + '_ {
+fn comma_sep<T: fmt::Display>(items: impl Iterator<Item=T>) -> impl fmt::Display {
     display_fn(move |f| {
-        for (i, item) in items.iter().enumerate() {
+        for (i, item) in items.enumerate() {
             if i != 0 { write!(f, ", ")?; }
-            fmt::Display::fmt(item, f)?;
+            fmt::Display::fmt(&item, f)?;
         }
         Ok(())
     })
 }
 
-impl<'a> fmt::Display for GenericBounds<'a> {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+crate fn print_generic_bounds(bounds: &[clean::GenericBound]) -> impl fmt::Display + '_ {
+    display_fn(move |f| {
         let mut bounds_dup = FxHashSet::default();
-        let &GenericBounds(bounds) = self;
 
-        for (i, bound) in bounds.iter().filter(|b| bounds_dup.insert(b.to_string())).enumerate() {
+        for (i, bound) in bounds.iter().filter(|b| {
+            bounds_dup.insert(b.print().to_string())
+        }).enumerate() {
             if i > 0 {
                 f.write_str(" + ")?;
             }
-            fmt::Display::fmt(bound, f)?;
+            fmt::Display::fmt(&bound.print(), f)?;
         }
         Ok(())
-    }
+    })
 }
 
-impl fmt::Display for clean::GenericParamDef {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        match self.kind {
-            clean::GenericParamDefKind::Lifetime => write!(f, "{}", self.name),
-            clean::GenericParamDefKind::Type { ref bounds, ref default, .. } => {
-                f.write_str(&self.name)?;
+impl clean::GenericParamDef {
+    crate fn print(&self) -> impl fmt::Display + '_ {
+        display_fn(move |f| {
+            match self.kind {
+                clean::GenericParamDefKind::Lifetime => write!(f, "{}", self.name),
+                clean::GenericParamDefKind::Type { ref bounds, ref default, .. } => {
+                    f.write_str(&self.name)?;
 
-                if !bounds.is_empty() {
-                    if f.alternate() {
-                        write!(f, ": {:#}", GenericBounds(bounds))?;
-                    } else {
-                        write!(f, ":&nbsp;{}", GenericBounds(bounds))?;
+                    if !bounds.is_empty() {
+                        if f.alternate() {
+                            write!(f, ": {:#}", print_generic_bounds(bounds))?;
+                        } else {
+                            write!(f, ":&nbsp;{}", print_generic_bounds(bounds))?;
+                        }
                     }
+
+                    if let Some(ref ty) = default {
+                        if f.alternate() {
+                            write!(f, " = {:#}", ty.print())?;
+                        } else {
+                            write!(f, "&nbsp;=&nbsp;{}", ty.print())?;
+                        }
+                    }
+
+                    Ok(())
                 }
+                clean::GenericParamDefKind::Const { ref ty, .. } => {
+                    f.write_str("const ")?;
+                    f.write_str(&self.name)?;
 
-                if let Some(ref ty) = default {
                     if f.alternate() {
-                        write!(f, " = {:#}", ty)?;
+                        write!(f, ": {:#}", ty.print())
                     } else {
-                        write!(f, "&nbsp;=&nbsp;{}", ty)?;
+                        write!(f, ":&nbsp;{}", ty.print())
                     }
                 }
-
-                Ok(())
             }
-            clean::GenericParamDefKind::Const { ref ty, .. } => {
-                f.write_str("const ")?;
-                f.write_str(&self.name)?;
-
-                if f.alternate() {
-                    write!(f, ": {:#}", ty)
-                } else {
-                    write!(f, ":&nbsp;{}", ty)
-                }
-            }
-        }
+        })
     }
 }
 
-impl fmt::Display for clean::Generics {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        let real_params = self.params
-            .iter()
-            .filter(|p| !p.is_synthetic_type_param())
-            .collect::<Vec<_>>();
-        if real_params.is_empty() {
-            return Ok(());
-        }
-        if f.alternate() {
-            write!(f, "<{:#}>", comma_sep(&real_params))
-        } else {
-            write!(f, "&lt;{}&gt;", comma_sep(&real_params))
-        }
+impl clean::Generics {
+    crate fn print(&self) -> impl fmt::Display + '_ {
+        display_fn(move |f| {
+            let real_params = self.params
+                .iter()
+                .filter(|p| !p.is_synthetic_type_param())
+                .collect::<Vec<_>>();
+            if real_params.is_empty() {
+                return Ok(());
+            }
+            if f.alternate() {
+                write!(f, "<{:#}>", comma_sep(real_params.iter().map(|g| g.print())))
+            } else {
+                write!(f, "&lt;{}&gt;", comma_sep(real_params.iter().map(|g| g.print())))
+            }
+        })
     }
 }
 
@@ -287,24 +257,26 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                 &clean::WherePredicate::BoundPredicate { ref ty, ref bounds } => {
                     let bounds = bounds;
                     if f.alternate() {
-                        clause.push_str(&format!("{:#}: {:#}", ty, GenericBounds(bounds)));
+                        clause.push_str(&format!("{:#}: {:#}",
+                                ty.print(), print_generic_bounds(bounds)));
                     } else {
-                        clause.push_str(&format!("{}: {}", ty, GenericBounds(bounds)));
+                        clause.push_str(&format!("{}: {}",
+                                ty.print(), print_generic_bounds(bounds)));
                     }
                 }
                 &clean::WherePredicate::RegionPredicate { ref lifetime, ref bounds } => {
                     clause.push_str(&format!("{}: {}",
-                                                lifetime,
+                                                lifetime.print(),
                                                 bounds.iter()
-                                                    .map(|b| b.to_string())
+                                                    .map(|b| b.print().to_string())
                                                     .collect::<Vec<_>>()
                                                     .join(" + ")));
                 }
                 &clean::WherePredicate::EqPredicate { ref lhs, ref rhs } => {
                     if f.alternate() {
-                        clause.push_str(&format!("{:#} == {:#}", lhs, rhs));
+                        clause.push_str(&format!("{:#} == {:#}", lhs.print(), rhs.print()));
                     } else {
-                        clause.push_str(&format!("{} == {}", lhs, rhs));
+                        clause.push_str(&format!("{} == {}", lhs.print(), rhs.print()));
                     }
                 }
             }
@@ -336,153 +308,164 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
     }
 }
 
-impl fmt::Display for clean::Lifetime {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.write_str(self.get_ref())?;
-        Ok(())
+impl clean::Lifetime {
+    crate fn print(&self) -> &str {
+        self.get_ref()
     }
 }
 
-impl fmt::Display for clean::Constant {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        fmt::Display::fmt(&self.expr, f)
+impl clean::Constant {
+    crate fn print(&self) -> &str {
+        &self.expr
     }
 }
 
-impl fmt::Display for clean::PolyTrait {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        if !self.generic_params.is_empty() {
+impl clean::PolyTrait {
+    fn print(&self) -> impl fmt::Display + '_ {
+        display_fn(move |f| {
+            if !self.generic_params.is_empty() {
+                if f.alternate() {
+                    write!(f, "for<{:#}> ",
+                        comma_sep(self.generic_params.iter().map(|g| g.print())))?;
+                } else {
+                    write!(f, "for&lt;{}&gt; ",
+                        comma_sep(self.generic_params.iter().map(|g| g.print())))?;
+                }
+            }
             if f.alternate() {
-                write!(f, "for<{:#}> ", comma_sep(&self.generic_params))?;
+                write!(f, "{:#}", self.trait_.print())
             } else {
-                write!(f, "for&lt;{}&gt; ", comma_sep(&self.generic_params))?;
+                write!(f, "{}", self.trait_.print())
             }
-        }
-        if f.alternate() {
-            write!(f, "{:#}", self.trait_)
-        } else {
-            write!(f, "{}", self.trait_)
-        }
+        })
     }
 }
 
-impl fmt::Display for clean::GenericBound {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        match *self {
-            clean::GenericBound::Outlives(ref lt) => {
-                write!(f, "{}", *lt)
-            }
-            clean::GenericBound::TraitBound(ref ty, modifier) => {
-                let modifier_str = match modifier {
-                    hir::TraitBoundModifier::None => "",
-                    hir::TraitBoundModifier::Maybe => "?",
-                };
-                if f.alternate() {
-                    write!(f, "{}{:#}", modifier_str, *ty)
-                } else {
-                    write!(f, "{}{}", modifier_str, *ty)
+impl clean::GenericBound {
+    crate fn print(&self) -> impl fmt::Display + '_ {
+        display_fn(move |f| {
+            match self {
+                clean::GenericBound::Outlives(lt) => {
+                    write!(f, "{}", lt.print())
+                }
+                clean::GenericBound::TraitBound(ty, modifier) => {
+                    let modifier_str = match modifier {
+                        hir::TraitBoundModifier::None => "",
+                        hir::TraitBoundModifier::Maybe => "?",
+                    };
+                    if f.alternate() {
+                        write!(f, "{}{:#}", modifier_str, ty.print())
+                    } else {
+                        write!(f, "{}{}", modifier_str, ty.print())
+                    }
                 }
             }
-        }
+        })
     }
 }
 
-impl fmt::Display for clean::GenericArgs {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        match *self {
-            clean::GenericArgs::AngleBracketed { ref args, ref bindings } => {
-                if !args.is_empty() || !bindings.is_empty() {
-                    if f.alternate() {
-                        f.write_str("<")?;
-                    } else {
-                        f.write_str("&lt;")?;
-                    }
-                    let mut comma = false;
-                    for arg in args {
-                        if comma {
-                            f.write_str(", ")?;
+impl clean::GenericArgs {
+    fn print(&self) -> impl fmt::Display + '_ {
+        display_fn(move |f| {
+            match *self {
+                clean::GenericArgs::AngleBracketed { ref args, ref bindings } => {
+                    if !args.is_empty() || !bindings.is_empty() {
+                        if f.alternate() {
+                            f.write_str("<")?;
+                        } else {
+                            f.write_str("&lt;")?;
+                        }
+                        let mut comma = false;
+                        for arg in args {
+                            if comma {
+                                f.write_str(", ")?;
+                            }
+                            comma = true;
+                            if f.alternate() {
+                                write!(f, "{:#}", arg.print())?;
+                            } else {
+                                write!(f, "{}", arg.print())?;
+                            }
+                        }
+                        for binding in bindings {
+                            if comma {
+                                f.write_str(", ")?;
+                            }
+                            comma = true;
+                            if f.alternate() {
+                                write!(f, "{:#}", binding.print())?;
+                            } else {
+                                write!(f, "{}", binding.print())?;
+                            }
                         }
-                        comma = true;
                         if f.alternate() {
-                            write!(f, "{:#}", *arg)?;
+                            f.write_str(">")?;
                         } else {
-                            write!(f, "{}", *arg)?;
+                            f.write_str("&gt;")?;
                         }
                     }
-                    for binding in bindings {
+                }
+                clean::GenericArgs::Parenthesized { ref inputs, ref output } => {
+                    f.write_str("(")?;
+                    let mut comma = false;
+                    for ty in inputs {
                         if comma {
                             f.write_str(", ")?;
                         }
                         comma = true;
                         if f.alternate() {
-                            write!(f, "{:#}", *binding)?;
+                            write!(f, "{:#}", ty.print())?;
                         } else {
-                            write!(f, "{}", *binding)?;
+                            write!(f, "{}", ty.print())?;
                         }
                     }
-                    if f.alternate() {
-                        f.write_str(">")?;
-                    } else {
-                        f.write_str("&gt;")?;
-                    }
-                }
-            }
-            clean::GenericArgs::Parenthesized { ref inputs, ref output } => {
-                f.write_str("(")?;
-                let mut comma = false;
-                for ty in inputs {
-                    if comma {
-                        f.write_str(", ")?;
-                    }
-                    comma = true;
-                    if f.alternate() {
-                        write!(f, "{:#}", *ty)?;
-                    } else {
-                        write!(f, "{}", *ty)?;
-                    }
-                }
-                f.write_str(")")?;
-                if let Some(ref ty) = *output {
-                    if f.alternate() {
-                        write!(f, " -> {:#}", ty)?;
-                    } else {
-                        write!(f, " -&gt; {}", ty)?;
+                    f.write_str(")")?;
+                    if let Some(ref ty) = *output {
+                        if f.alternate() {
+                            write!(f, " -> {:#}", ty.print())?;
+                        } else {
+                            write!(f, " -&gt; {}", ty.print())?;
+                        }
                     }
                 }
             }
-        }
-        Ok(())
+            Ok(())
+        })
     }
 }
 
-impl fmt::Display for clean::PathSegment {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.write_str(&self.name)?;
-        if f.alternate() {
-            write!(f, "{:#}", self.args)
-        } else {
-            write!(f, "{}", self.args)
-        }
+impl clean::PathSegment {
+    crate fn print(&self) -> impl fmt::Display + '_ {
+        display_fn(move |f| {
+            f.write_str(&self.name)?;
+            if f.alternate() {
+                write!(f, "{:#}", self.args.print())
+            } else {
+                write!(f, "{}", self.args.print())
+            }
+        })
     }
 }
 
-impl fmt::Display for clean::Path {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        if self.global {
-            f.write_str("::")?
-        }
-
-        for (i, seg) in self.segments.iter().enumerate() {
-            if i > 0 {
+impl clean::Path {
+    crate fn print(&self) -> impl fmt::Display + '_ {
+        display_fn(move |f| {
+            if self.global {
                 f.write_str("::")?
             }
-            if f.alternate() {
-                write!(f, "{:#}", seg)?;
-            } else {
-                write!(f, "{}", seg)?;
+
+            for (i, seg) in self.segments.iter().enumerate() {
+                if i > 0 {
+                    f.write_str("::")?
+                }
+                if f.alternate() {
+                    write!(f, "{:#}", seg.print())?;
+                } else {
+                    write!(f, "{}", seg.print())?;
+                }
             }
-        }
-        Ok(())
+            Ok(())
+        })
     }
 }
 
@@ -516,7 +499,7 @@ pub fn href(did: DefId) -> Option<(String, ItemType, Vec<String>)> {
             url.push_str("/index.html");
         }
         _ => {
-            url.push_str(shortty.css_class());
+            url.push_str(shortty.as_str());
             url.push_str(".");
             url.push_str(fqp.last().unwrap());
             url.push_str(".html");
@@ -537,7 +520,7 @@ fn resolved_path(w: &mut fmt::Formatter<'_>, did: DefId, path: &clean::Path,
         }
     }
     if w.alternate() {
-        write!(w, "{}{:#}", &last.name, last.args)?;
+        write!(w, "{}{:#}", &last.name, last.args.print())?;
     } else {
         let path = if use_absolute {
             if let Some((_, _, fqp)) = href(did) {
@@ -550,7 +533,7 @@ fn resolved_path(w: &mut fmt::Formatter<'_>, did: DefId, path: &clean::Path,
         } else {
             anchor(did, &last.name).to_string()
         };
-        write!(w, "{}{}", path, last.args)?;
+        write!(w, "{}{}", path, last.args.print())?;
     }
     Ok(())
 }
@@ -606,7 +589,7 @@ fn tybounds(param_names: &Option<Vec<clean::GenericBound>>) -> impl fmt::Display
             Some(ref params) => {
                 for param in params {
                     write!(f, " + ")?;
-                    fmt::Display::fmt(param, f)?;
+                    fmt::Display::fmt(&param.print(), f)?;
                 }
                 Ok(())
             }
@@ -644,14 +627,15 @@ 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),
-                       comma_sep(&decl.generic_params),
-                       decl.decl)
+                       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, "{}{}", comma_sep(&decl.generic_params), decl.decl)
+                write!(f, "{}{}", decl.print_generic_params(), decl.decl.print())
             }
         }
         clean::Tuple(ref typs) => {
@@ -660,28 +644,30 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter<'_>, use_absolute: bool) ->
                 &[ref one] => {
                     primitive_link(f, PrimitiveType::Tuple, "(")?;
                     // Carry `f.alternate()` into this display w/o branching manually.
-                    fmt::Display::fmt(one, f)?;
+                    fmt::Display::fmt(&one.print(), f)?;
                     primitive_link(f, PrimitiveType::Tuple, ",)")
                 }
                 many => {
                     primitive_link(f, PrimitiveType::Tuple, "(")?;
-                    fmt::Display::fmt(&comma_sep(many), f)?;
+                    for (i, item) in many.iter().enumerate() {
+                        if i != 0 { write!(f, ", ")?; }
+                        fmt::Display::fmt(&item.print(), f)?;
+                    }
                     primitive_link(f, PrimitiveType::Tuple, ")")
                 }
             }
         }
         clean::Slice(ref t) => {
             primitive_link(f, PrimitiveType::Slice, "[")?;
-            fmt::Display::fmt(t, f)?;
+            fmt::Display::fmt(&t.print(), f)?;
             primitive_link(f, PrimitiveType::Slice, "]")
         }
         clean::Array(ref t, ref n) => {
             primitive_link(f, PrimitiveType::Array, "[")?;
-            fmt::Display::fmt(t, f)?;
+            fmt::Display::fmt(&t.print(), f)?;
             primitive_link(f, PrimitiveType::Array, &format!("; {}]", n))
         }
         clean::Never => primitive_link(f, PrimitiveType::Never, "!"),
-        clean::CVarArgs => primitive_link(f, PrimitiveType::CVarArgs, "..."),
         clean::RawPointer(m, ref t) => {
             let m = match m {
                 clean::Immutable => "const",
@@ -691,24 +677,24 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter<'_>, use_absolute: bool) ->
                 clean::Generic(_) | clean::ResolvedPath {is_generic: true, ..} => {
                     if f.alternate() {
                         primitive_link(f, clean::PrimitiveType::RawPointer,
-                                       &format!("*{} {:#}", m, t))
+                                       &format!("*{} {:#}", m, t.print()))
                     } else {
                         primitive_link(f, clean::PrimitiveType::RawPointer,
-                                       &format!("*{} {}", m, t))
+                                       &format!("*{} {}", m, t.print()))
                     }
                 }
                 _ => {
                     primitive_link(f, clean::PrimitiveType::RawPointer, &format!("*{} ", m))?;
-                    fmt::Display::fmt(t, f)
+                    fmt::Display::fmt(&t.print(), f)
                 }
             }
         }
         clean::BorrowedRef{ lifetime: ref l, mutability, type_: ref ty} => {
-            let lt = match *l {
-                Some(ref l) => format!("{} ", *l),
-                _ => String::new(),
+            let lt = match l {
+                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 {
@@ -720,19 +706,19 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter<'_>, use_absolute: bool) ->
                         clean::Generic(_) => {
                             if f.alternate() {
                                 primitive_link(f, PrimitiveType::Slice,
-                                    &format!("{}{}{}[{:#}]", amp, lt, m, **bt))
+                                    &format!("{}{}{}[{:#}]", amp, lt, m, bt.print()))
                             } else {
                                 primitive_link(f, PrimitiveType::Slice,
-                                    &format!("{}{}{}[{}]", amp, lt, m, **bt))
+                                    &format!("{}{}{}[{}]", amp, lt, m, bt.print()))
                             }
                         }
                         _ => {
                             primitive_link(f, PrimitiveType::Slice,
                                            &format!("{}{}{}[", amp, lt, m))?;
                             if f.alternate() {
-                                write!(f, "{:#}", **bt)?;
+                                write!(f, "{:#}", bt.print())?;
                             } else {
-                                write!(f, "{}", **bt)?;
+                                write!(f, "{}", bt.print())?;
                             }
                             primitive_link(f, PrimitiveType::Slice, "]")
                         }
@@ -756,9 +742,9 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter<'_>, use_absolute: bool) ->
         }
         clean::ImplTrait(ref bounds) => {
             if f.alternate() {
-                write!(f, "impl {:#}", GenericBounds(bounds))
+                write!(f, "impl {:#}", print_generic_bounds(bounds))
             } else {
-                write!(f, "impl {}", GenericBounds(bounds))
+                write!(f, "impl {}", print_generic_bounds(bounds))
             }
         }
         clean::QPath { ref name, ref self_type, ref trait_ } => {
@@ -770,15 +756,15 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter<'_>, use_absolute: bool) ->
             };
             if f.alternate() {
                 if should_show_cast {
-                    write!(f, "<{:#} as {:#}>::", self_type, trait_)?
+                    write!(f, "<{:#} as {:#}>::", self_type.print(), trait_.print())?
                 } else {
-                    write!(f, "{:#}::", self_type)?
+                    write!(f, "{:#}::", self_type.print())?
                 }
             } else {
                 if should_show_cast {
-                    write!(f, "&lt;{} as {}&gt;::", self_type, trait_)?
+                    write!(f, "&lt;{} as {}&gt;::", self_type.print(), trait_.print())?
                 } else {
-                    write!(f, "{}::", self_type)?
+                    write!(f, "{}::", self_type.print())?
                 }
             };
             match *trait_ {
@@ -818,55 +804,64 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter<'_>, use_absolute: bool) ->
     }
 }
 
-impl fmt::Display for clean::Type {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        fmt_type(self, f, false)
+impl clean::Type {
+    crate fn print(&self) -> impl fmt::Display + '_ {
+        display_fn(move |f| {
+            fmt_type(self, f, false)
+        })
     }
 }
 
-fn fmt_impl(i: &clean::Impl,
-            f: &mut fmt::Formatter<'_>,
-            link_trait: bool,
-            use_absolute: bool) -> fmt::Result {
-    if f.alternate() {
-        write!(f, "impl{:#} ", i.generics)?;
-    } else {
-        write!(f, "impl{} ", i.generics)?;
+impl clean::Impl {
+    crate fn print(&self) -> impl fmt::Display + '_ {
+        self.print_inner(true, false)
     }
 
-    if let Some(ref ty) = i.trait_ {
-        if i.polarity == Some(clean::ImplPolarity::Negative) {
-            write!(f, "!")?;
-        }
+    fn print_inner(
+        &self,
+        link_trait: bool,
+        use_absolute: bool,
+    ) -> impl fmt::Display + '_ {
+        display_fn(move |f| {
+            if f.alternate() {
+                write!(f, "impl{:#} ", self.generics.print())?;
+            } else {
+                write!(f, "impl{} ", self.generics.print())?;
+            }
 
-        if link_trait {
-            fmt::Display::fmt(ty, f)?;
-        } else {
-            match *ty {
-                clean::ResolvedPath { param_names: None, ref path, is_generic: false, .. } => {
-                    let last = path.segments.last().unwrap();
-                    fmt::Display::fmt(&last.name, f)?;
-                    fmt::Display::fmt(&last.args, f)?;
+            if let Some(ref ty) = self.trait_ {
+                if self.polarity == Some(clean::ImplPolarity::Negative) {
+                    write!(f, "!")?;
                 }
-                _ => unreachable!(),
-            }
-        }
-        write!(f, " for ")?;
-    }
 
-    if let Some(ref ty) = i.blanket_impl {
-        fmt_type(ty, f, use_absolute)?;
-    } else {
-        fmt_type(&i.for_, f, use_absolute)?;
-    }
+                if link_trait {
+                    fmt::Display::fmt(&ty.print(), f)?;
+                } else {
+                    match ty {
+                        clean::ResolvedPath { param_names: None, path, is_generic: false, .. } => {
+                            let last = path.segments.last().unwrap();
+                            fmt::Display::fmt(&last.name, f)?;
+                            fmt::Display::fmt(&last.args.print(), f)?;
+                        }
+                        _ => unreachable!(),
+                    }
+                }
+                write!(f, " for ")?;
+            }
 
-    fmt::Display::fmt(&WhereClause { gens: &i.generics, indent: 0, end_newline: true }, f)?;
-    Ok(())
-}
+            if let Some(ref ty) = self.blanket_impl {
+                fmt_type(ty, f, use_absolute)?;
+            } else {
+                fmt_type(&self.for_, f, use_absolute)?;
+            }
 
-impl fmt::Display for clean::Impl {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        fmt_impl(self, f, true, false)
+            fmt::Display::fmt(&WhereClause {
+                gens: &self.generics,
+                indent: 0,
+                end_newline: true,
+            }, f)?;
+            Ok(())
+        })
     }
 }
 
@@ -874,275 +869,324 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 pub fn fmt_impl_for_trait_page(i: &clean::Impl,
                                f: &mut Buffer,
                                use_absolute: bool) {
-    f.with_formatter(|f| fmt_impl(i, f, false, use_absolute))
+    f.from_display(i.print_inner(false, use_absolute))
 }
 
-impl fmt::Display for clean::Arguments {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        for (i, input) in self.values.iter().enumerate() {
-            if !input.name.is_empty() {
-                write!(f, "{}: ", input.name)?;
-            }
-            if f.alternate() {
-                write!(f, "{:#}", input.type_)?;
-            } else {
-                write!(f, "{}", input.type_)?;
+impl clean::Arguments {
+    crate fn print(&self) -> impl fmt::Display + '_ {
+        display_fn(move |f| {
+            for (i, input) in self.values.iter().enumerate() {
+                if !input.name.is_empty() {
+                    write!(f, "{}: ", input.name)?;
+                }
+                if f.alternate() {
+                    write!(f, "{:#}", input.type_.print())?;
+                } else {
+                    write!(f, "{}", input.type_.print())?;
+                }
+                if i + 1 < self.values.len() { write!(f, ", ")?; }
             }
-            if i + 1 < self.values.len() { write!(f, ", ")?; }
-        }
-        Ok(())
+            Ok(())
+        })
     }
 }
 
-impl fmt::Display for clean::FunctionRetTy {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        match *self {
-            clean::Return(clean::Tuple(ref tys)) if tys.is_empty() => Ok(()),
-            clean::Return(ref ty) if f.alternate() => write!(f, " -> {:#}", ty),
-            clean::Return(ref ty) => write!(f, " -&gt; {}", ty),
-            clean::DefaultReturn => Ok(()),
-        }
+impl clean::FunctionRetTy {
+    crate fn print(&self) -> impl fmt::Display + '_ {
+        display_fn(move |f| {
+            match self {
+                clean::Return(clean::Tuple(tys)) if tys.is_empty() => Ok(()),
+                clean::Return(ty) if f.alternate() => write!(f, " -> {:#}", ty.print()),
+                clean::Return(ty) => write!(f, " -&gt; {}", ty.print()),
+                clean::DefaultReturn => Ok(()),
+            }
+        })
     }
 }
 
-impl fmt::Display for clean::FnDecl {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        if f.alternate() {
-            write!(f, "({args:#}){arrow:#}", args = self.inputs, arrow = self.output)
-        } else {
-            write!(f, "({args}){arrow}", args = self.inputs, arrow = self.output)
-        }
+impl clean::BareFunctionDecl {
+    fn print_generic_params(&self) -> impl fmt::Display + '_ {
+        comma_sep(self.generic_params.iter().map(|g| g.print()))
     }
 }
 
-impl<'a> fmt::Display for Function<'a> {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        let &Function { decl, header_len, indent, asyncness } = self;
-        let amp = if f.alternate() { "&" } else { "&amp;" };
-        let mut args = String::new();
-        let mut args_plain = String::new();
-        for (i, input) in decl.inputs.values.iter().enumerate() {
-            if i == 0 {
-                args.push_str("<br>");
+impl clean::FnDecl {
+    crate fn print(&self) -> impl fmt::Display + '_ {
+        display_fn(move |f| {
+        let ellipsis = if self.c_variadic { ", ..." } else { "" };
+            if f.alternate() {
+                write!(f,
+                    "({args:#}{ellipsis}){arrow:#}",
+                    args = self.inputs.print(), ellipsis = ellipsis, arrow = self.output.print())
+            } else {
+                write!(f,
+                    "({args}{ellipsis}){arrow}",
+                    args = self.inputs.print(), ellipsis = ellipsis, arrow = self.output.print())
             }
+        })
+    }
+}
+
 
-            if let Some(selfty) = input.to_self() {
-                match selfty {
-                    clean::SelfValue => {
-                        args.push_str("self");
-                        args_plain.push_str("self");
+impl Function<'_> {
+    crate fn print(&self) -> impl fmt::Display + '_ {
+        display_fn(move |f| {
+            let &Function { decl, header_len, indent, asyncness } = self;
+            let amp = if f.alternate() { "&" } else { "&amp;" };
+            let mut args = String::new();
+            let mut args_plain = String::new();
+            for (i, input) in decl.inputs.values.iter().enumerate() {
+                if i == 0 {
+                    args.push_str("<br>");
+                }
+
+                if let Some(selfty) = input.to_self() {
+                    match selfty {
+                        clean::SelfValue => {
+                            args.push_str("self");
+                            args_plain.push_str("self");
+                        }
+                        clean::SelfBorrowed(Some(ref lt), mtbl) => {
+                            args.push_str(
+                                &format!("{}{} {}self", amp, lt.print(), mtbl.print_with_space()));
+                            args_plain.push_str(
+                                &format!("&{} {}self", lt.print(), mtbl.print_with_space()));
+                        }
+                        clean::SelfBorrowed(None, 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() {
+                                args.push_str(&format!("self: {:#}", typ.print()));
+                            } else {
+                                args.push_str(&format!("self: {}", typ.print()));
+                            }
+                            args_plain.push_str(&format!("self: {:#}", typ.print()));
+                        }
                     }
-                    clean::SelfBorrowed(Some(ref lt), mtbl) => {
-                        args.push_str(&format!("{}{} {}self", amp, *lt, MutableSpace(mtbl)));
-                        args_plain.push_str(&format!("&{} {}self", *lt, MutableSpace(mtbl)));
+                } else {
+                    if i > 0 {
+                        args.push_str(" <br>");
+                        args_plain.push_str(" ");
                     }
-                    clean::SelfBorrowed(None, mtbl) => {
-                        args.push_str(&format!("{}{}self", amp, MutableSpace(mtbl)));
-                        args_plain.push_str(&format!("&{}self", MutableSpace(mtbl)));
+                    if !input.name.is_empty() {
+                        args.push_str(&format!("{}: ", input.name));
+                        args_plain.push_str(&format!("{}: ", input.name));
                     }
-                    clean::SelfExplicit(ref typ) => {
-                        if f.alternate() {
-                            args.push_str(&format!("self: {:#}", *typ));
-                        } else {
-                            args.push_str(&format!("self: {}", *typ));
-                        }
-                        args_plain.push_str(&format!("self: {:#}", *typ));
+
+                    if f.alternate() {
+                        args.push_str(&format!("{:#}", input.type_.print()));
+                    } else {
+                        args.push_str(&input.type_.print().to_string());
                     }
+                    args_plain.push_str(&format!("{:#}", input.type_.print()));
                 }
-            } else {
-                if i > 0 {
-                    args.push_str(" <br>");
-                    args_plain.push_str(" ");
-                }
-                if !input.name.is_empty() {
-                    args.push_str(&format!("{}: ", input.name));
-                    args_plain.push_str(&format!("{}: ", input.name));
-                }
-
-                if f.alternate() {
-                    args.push_str(&format!("{:#}", input.type_));
-                } else {
-                    args.push_str(&input.type_.to_string());
+                if i + 1 < decl.inputs.values.len() {
+                    args.push(',');
+                    args_plain.push(',');
                 }
-                args_plain.push_str(&format!("{:#}", input.type_));
-            }
-            if i + 1 < decl.inputs.values.len() {
-                args.push(',');
-                args_plain.push(',');
             }
-        }
 
-        let args_plain = format!("({})", args_plain);
+            let mut args_plain = format!("({})", args_plain);
 
-        let output = if let hir::IsAsync::Async = asyncness {
-            Cow::Owned(decl.sugared_async_return_type())
-        } else {
-            Cow::Borrowed(&decl.output)
-        };
+            if decl.c_variadic {
+                args.push_str(",<br> ...");
+                args_plain.push_str(", ...");
+            }
 
-        let arrow_plain = format!("{:#}", &output);
-        let arrow = if f.alternate() {
-            format!("{:#}", &output)
-        } else {
-            output.to_string()
-        };
+            let output = if let hir::IsAsync::Async = asyncness {
+                Cow::Owned(decl.sugared_async_return_type())
+            } else {
+                Cow::Borrowed(&decl.output)
+            };
 
-        let declaration_len = header_len + args_plain.len() + arrow_plain.len();
-        let output = if declaration_len > 80 {
-            let full_pad = format!("<br>{}", "&nbsp;".repeat(indent + 4));
-            let close_pad = format!("<br>{}", "&nbsp;".repeat(indent));
-            format!("({args}{close}){arrow}",
-                    args = args.replace("<br>", &full_pad),
-                    close = close_pad,
-                    arrow = arrow)
-        } else {
-            format!("({args}){arrow}", args = args.replace("<br>", ""), arrow = arrow)
-        };
+            let arrow_plain = format!("{:#}", &output.print());
+            let arrow = if f.alternate() {
+                format!("{:#}", &output.print())
+            } else {
+                output.print().to_string()
+            };
 
-        if f.alternate() {
-            write!(f, "{}", output.replace("<br>", "\n"))
-        } else {
-            write!(f, "{}", output)
-        }
+            let declaration_len = header_len + args_plain.len() + arrow_plain.len();
+            let output = if declaration_len > 80 {
+                let full_pad = format!("<br>{}", "&nbsp;".repeat(indent + 4));
+                let close_pad = format!("<br>{}", "&nbsp;".repeat(indent));
+                format!("({args}{close}){arrow}",
+                        args = args.replace("<br>", &full_pad),
+                        close = close_pad,
+                        arrow = arrow)
+            } else {
+                format!("({args}){arrow}", args = args.replace("<br>", ""), arrow = arrow)
+            };
+
+            if f.alternate() {
+                write!(f, "{}", output.replace("<br>", "\n"))
+            } else {
+                write!(f, "{}", output)
+            }
+        })
     }
 }
 
-impl<'a> fmt::Display for VisSpace<'a> {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        match *self.get() {
-            Some(clean::Public) => f.write_str("pub "),
-            Some(clean::Inherited) | None => Ok(()),
-            Some(clean::Visibility::Crate) => write!(f, "pub(crate) "),
-            Some(clean::Visibility::Restricted(did, ref path)) => {
-                f.write_str("pub(")?;
-                if path.segments.len() != 1
-                    || (path.segments[0].name != "self" && path.segments[0].name != "super")
-                {
-                    f.write_str("in ")?;
+impl clean::Visibility {
+    crate fn print_with_space(&self) -> impl fmt::Display + '_ {
+        display_fn(move |f| {
+            match *self {
+                clean::Public => f.write_str("pub "),
+                clean::Inherited => Ok(()),
+                clean::Visibility::Crate => write!(f, "pub(crate) "),
+                clean::Visibility::Restricted(did, ref path) => {
+                    f.write_str("pub(")?;
+                    if path.segments.len() != 1
+                        || (path.segments[0].name != "self" && path.segments[0].name != "super")
+                    {
+                        f.write_str("in ")?;
+                    }
+                    resolved_path(f, did, path, true, false)?;
+                    f.write_str(") ")
                 }
-                resolved_path(f, did, path, true, false)?;
-                f.write_str(") ")
             }
-        }
+        })
     }
 }
 
-impl fmt::Display for UnsafetySpace {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        match self.get() {
-            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.get() {
-            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 clean::Import {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        match *self {
-            clean::Import::Simple(ref name, ref src) => {
-                if *name == src.path.last_name() {
-                    write!(f, "use {};", *src)
-                } else {
-                    write!(f, "use {} as {};", *src, *name)
+impl clean::Import {
+    crate fn print(&self) -> impl fmt::Display + '_ {
+        display_fn(move |f| {
+            match *self {
+                clean::Import::Simple(ref name, ref src) => {
+                    if *name == src.path.last_name() {
+                        write!(f, "use {};", src.print())
+                    } else {
+                        write!(f, "use {} as {};", src.print(), *name)
+                    }
                 }
-            }
-            clean::Import::Glob(ref src) => {
-                if src.path.segments.is_empty() {
-                    write!(f, "use *;")
-                } else {
-                    write!(f, "use {}::*;", *src)
+                clean::Import::Glob(ref src) => {
+                    if src.path.segments.is_empty() {
+                        write!(f, "use *;")
+                    } else {
+                        write!(f, "use {}::*;", src.print())
+                    }
                 }
             }
-        }
+        })
     }
 }
 
-impl fmt::Display for clean::ImportSource {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        match self.did {
-            Some(did) => resolved_path(f, did, &self.path, true, false),
-            _ => {
-                for (i, seg) in self.path.segments.iter().enumerate() {
-                    if i > 0 {
-                        write!(f, "::")?
+impl clean::ImportSource {
+    crate fn print(&self) -> impl fmt::Display + '_ {
+        display_fn(move |f| {
+            match self.did {
+                Some(did) => resolved_path(f, did, &self.path, true, false),
+                _ => {
+                    for (i, seg) in self.path.segments.iter().enumerate() {
+                        if i > 0 {
+                            write!(f, "::")?
+                        }
+                        write!(f, "{}", seg.name)?;
                     }
-                    write!(f, "{}", seg.name)?;
+                    Ok(())
                 }
-                Ok(())
             }
-        }
+        })
     }
 }
 
-impl fmt::Display for clean::TypeBinding {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.write_str(&self.name)?;
-        match self.kind {
-            clean::TypeBindingKind::Equality { ref ty } => {
-                if f.alternate() {
-                    write!(f, " = {:#}", ty)?;
-                } else {
-                    write!(f, " = {}", ty)?;
-                }
-            }
-            clean::TypeBindingKind::Constraint { ref bounds } => {
-                if !bounds.is_empty() {
+impl clean::TypeBinding {
+    crate fn print(&self) -> impl fmt::Display + '_ {
+        display_fn(move |f| {
+            f.write_str(&self.name)?;
+            match self.kind {
+                clean::TypeBindingKind::Equality { ref ty } => {
                     if f.alternate() {
-                        write!(f, ": {:#}", GenericBounds(bounds))?;
+                        write!(f, " = {:#}", ty.print())?;
                     } else {
-                        write!(f, ":&nbsp;{}", GenericBounds(bounds))?;
+                        write!(f, " = {}", ty.print())?;
+                    }
+                }
+                clean::TypeBindingKind::Constraint { ref bounds } => {
+                    if !bounds.is_empty() {
+                        if f.alternate() {
+                            write!(f, ": {:#}", print_generic_bounds(bounds))?;
+                        } else {
+                            write!(f, ":&nbsp;{}", print_generic_bounds(bounds))?;
+                        }
                     }
                 }
             }
-        }
-        Ok(())
+            Ok(())
+        })
     }
 }
 
-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()),
         }
+    })
+}
+
+crate fn print_default_space<'a>(v: bool) -> &'a str {
+    if v {
+        "default "
+    } else {
+        ""
     }
 }
 
-impl fmt::Display for DefaultSpace {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        if self.0 {
-            write!(f, "default ")
-        } else {
-            Ok(())
-        }
+impl clean::GenericArg {
+    crate fn print(&self) -> impl fmt::Display + '_ {
+        display_fn(move |f| {
+            match self {
+                clean::GenericArg::Lifetime(lt) => fmt::Display::fmt(&lt.print(), f),
+                clean::GenericArg::Type(ty) => fmt::Display::fmt(&ty.print(), f),
+                clean::GenericArg::Const(ct) => fmt::Display::fmt(&ct.print(), f),
+            }
+        })
     }
 }