Skip to content

Commit f35eae9

Browse files
Add buffer abstraction
1 parent 43a5ff4 commit f35eae9

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

src/librustdoc/html/format.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,71 @@ use crate::clean::{self, PrimitiveType};
1818
use crate::html::item_type::ItemType;
1919
use crate::html::render::{self, cache, CURRENT_DEPTH};
2020

21+
pub trait Print {
22+
fn print(&self, buffer: &mut Buffer);
23+
}
24+
25+
#[derive(Debug, Clone)]
26+
pub struct Buffer {
27+
for_html: bool,
28+
buffer: String,
29+
}
30+
31+
impl Buffer {
32+
crate fn empty_from(v: &Buffer) -> Buffer {
33+
Buffer {
34+
for_html: v.for_html,
35+
buffer: String::new(),
36+
}
37+
}
38+
39+
crate fn html() -> Buffer {
40+
Buffer {
41+
for_html: true,
42+
buffer: String::new(),
43+
}
44+
}
45+
46+
crate fn is_empty(&self) -> bool {
47+
self.buffer.is_empty()
48+
}
49+
50+
crate fn into_inner(self) -> String {
51+
self.buffer
52+
}
53+
54+
crate fn insert_str(&mut self, idx: usize, s: &str) {
55+
self.buffer.insert_str(idx, s);
56+
}
57+
58+
crate fn push_str(&mut self, s: &str) {
59+
self.buffer.push_str(s);
60+
}
61+
62+
// Intended for consumption by write! and writeln! (std::fmt) but without
63+
// the fmt::Result return type imposed by fmt::Write (and avoiding the trait
64+
// import).
65+
crate fn write_str(&mut self, s: &str) {
66+
self.buffer.push_str(s);
67+
}
68+
69+
// Intended for consumption by write! and writeln! (std::fmt) but without
70+
// the fmt::Result return type imposed by fmt::Write (and avoiding the trait
71+
// import).
72+
crate fn write_fmt(&mut self, v: fmt::Arguments<'_>) {
73+
use fmt::Write;
74+
self.buffer.write_fmt(v).unwrap();
75+
}
76+
77+
crate fn display<T: fmt::Display>(&mut self, t: T) {
78+
if self.for_html {
79+
write!(self, "{}", t);
80+
} else {
81+
write!(self, "{:#}", t);
82+
}
83+
}
84+
}
85+
2186
/// Helper to render an optional visibility with a space after it (if the
2287
/// visibility is preset)
2388
#[derive(Copy, Clone)]

0 commit comments

Comments
 (0)