diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs
index 313734e3fdd6..941d2c4acb1a 100644
--- a/src/librustdoc/html/format.rs
+++ b/src/librustdoc/html/format.rs
@@ -18,6 +18,71 @@ use crate::clean::{self, PrimitiveType};
use crate::html::item_type::ItemType;
use crate::html::render::{self, cache, CURRENT_DEPTH};
+pub trait Print {
+ fn print(&self, buffer: &mut Buffer);
+}
+
+#[derive(Debug, Clone)]
+pub struct Buffer {
+ for_html: bool,
+ buffer: String,
+}
+
+impl Buffer {
+ crate fn empty_from(v: &Buffer) -> Buffer {
+ Buffer {
+ for_html: v.for_html,
+ buffer: String::new(),
+ }
+ }
+
+ crate fn html() -> Buffer {
+ Buffer {
+ for_html: true,
+ buffer: String::new(),
+ }
+ }
+
+ crate fn is_empty(&self) -> bool {
+ self.buffer.is_empty()
+ }
+
+ crate fn into_inner(self) -> String {
+ self.buffer
+ }
+
+ crate fn insert_str(&mut self, idx: usize, s: &str) {
+ self.buffer.insert_str(idx, s);
+ }
+
+ crate fn push_str(&mut self, s: &str) {
+ self.buffer.push_str(s);
+ }
+
+ // Intended for consumption by write! and writeln! (std::fmt) but without
+ // the fmt::Result return type imposed by fmt::Write (and avoiding the trait
+ // import).
+ crate fn write_str(&mut self, s: &str) {
+ self.buffer.push_str(s);
+ }
+
+ // Intended for consumption by write! and writeln! (std::fmt) but without
+ // the fmt::Result return type imposed by fmt::Write (and avoiding the trait
+ // import).
+ crate fn write_fmt(&mut self, v: fmt::Arguments<'_>) {
+ use fmt::Write;
+ self.buffer.write_fmt(v).unwrap();
+ }
+
+ crate fn display(&mut self, t: T) {
+ if self.for_html {
+ write!(self, "{}", t);
+ } else {
+ write!(self, "{:#}", t);
+ }
+ }
+}
+
/// Helper to render an optional visibility with a space after it (if the
/// visibility is preset)
#[derive(Copy, Clone)]