Skip to content

Commit 8121cf0

Browse files
committed
Restructure debug builders to minimize codegen
Switching from generic bounds to trait objects and having un-inlined inner methods should cut down on the size of Debug impls, since we care about the speed of a Debug implementation way less than binary bloat.
1 parent e3656bd commit 8121cf0

File tree

1 file changed

+58
-18
lines changed

1 file changed

+58
-18
lines changed

src/libcore/fmt/builders.rs

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,13 @@ pub fn debug_struct_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>, name: &str)
6363
impl<'a, 'b: 'a> DebugStruct<'a, 'b> {
6464
/// Adds a new field to the generated struct output.
6565
#[unstable(feature = "core", reason = "method was just created")]
66-
pub fn field<S>(mut self, name: &str, value: &S) -> DebugStruct<'a, 'b>
67-
where S: fmt::Debug {
66+
#[inline]
67+
pub fn field(mut self, name: &str, value: &fmt::Debug) -> DebugStruct<'a, 'b> {
68+
self.field_inner(name, value);
69+
self
70+
}
71+
72+
fn field_inner(&mut self, name: &str, value: &fmt::Debug) {
6873
self.result = self.result.and_then(|_| {
6974
let prefix = if self.has_fields {
7075
","
@@ -81,13 +86,18 @@ impl<'a, 'b: 'a> DebugStruct<'a, 'b> {
8186
});
8287

8388
self.has_fields = true;
84-
self
8589
}
8690

8791
/// Consumes the `DebugStruct`, finishing output and returning any error
8892
/// encountered.
8993
#[unstable(feature = "core", reason = "method was just created")]
94+
#[inline]
9095
pub fn finish(mut self) -> fmt::Result {
96+
self.finish_inner();
97+
self.result
98+
}
99+
100+
fn finish_inner(&mut self) {
91101
if self.has_fields {
92102
self.result = self.result.and_then(|_| {
93103
if self.is_pretty() {
@@ -97,7 +107,6 @@ impl<'a, 'b: 'a> DebugStruct<'a, 'b> {
97107
}
98108
});
99109
}
100-
self.result
101110
}
102111

103112
fn is_pretty(&self) -> bool {
@@ -127,7 +136,13 @@ pub fn debug_tuple_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>, name: &str) -> D
127136
impl<'a, 'b: 'a> DebugTuple<'a, 'b> {
128137
/// Adds a new field to the generated tuple struct output.
129138
#[unstable(feature = "core", reason = "method was just created")]
130-
pub fn field<S>(mut self, value: &S) -> DebugTuple<'a, 'b> where S: fmt::Debug {
139+
#[inline]
140+
pub fn field(mut self, value: &fmt::Debug) -> DebugTuple<'a, 'b> {
141+
self.field_inner(value);
142+
self
143+
}
144+
145+
fn field_inner(&mut self, value: &fmt::Debug) {
131146
self.result = self.result.and_then(|_| {
132147
let (prefix, space) = if self.has_fields {
133148
(",", " ")
@@ -144,13 +159,18 @@ impl<'a, 'b: 'a> DebugTuple<'a, 'b> {
144159
});
145160

146161
self.has_fields = true;
147-
self
148162
}
149163

150164
/// Consumes the `DebugTuple`, finishing output and returning any error
151165
/// encountered.
152166
#[unstable(feature = "core", reason = "method was just created")]
167+
#[inline]
153168
pub fn finish(mut self) -> fmt::Result {
169+
self.finish_inner();
170+
self.result
171+
}
172+
173+
fn finish_inner(&mut self) {
154174
if self.has_fields {
155175
self.result = self.result.and_then(|_| {
156176
if self.is_pretty() {
@@ -160,7 +180,6 @@ impl<'a, 'b: 'a> DebugTuple<'a, 'b> {
160180
}
161181
});
162182
}
163-
self.result
164183
}
165184

166185
fn is_pretty(&self) -> bool {
@@ -190,7 +209,13 @@ pub fn debug_set_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>, name: &str) -> Deb
190209
impl<'a, 'b: 'a> DebugSet<'a, 'b> {
191210
/// Adds a new entry to the set output.
192211
#[unstable(feature = "core", reason = "method was just created")]
193-
pub fn entry<S>(mut self, entry: &S) -> DebugSet<'a, 'b> where S: fmt::Debug {
212+
#[inline]
213+
pub fn entry(mut self, entry: &fmt::Debug) -> DebugSet<'a, 'b> {
214+
self.entry_inner(entry);
215+
self
216+
}
217+
218+
fn entry_inner(&mut self, entry: &fmt::Debug) {
194219
self.result = self.result.and_then(|_| {
195220
let prefix = if self.has_fields {
196221
","
@@ -207,21 +232,26 @@ impl<'a, 'b: 'a> DebugSet<'a, 'b> {
207232
});
208233

209234
self.has_fields = true;
210-
self
211235
}
212236

213237
/// Consumes the `DebugSet`, finishing output and returning any error
214238
/// encountered.
215239
#[unstable(feature = "core", reason = "method was just created")]
216-
pub fn finish(self) -> fmt::Result {
217-
self.result.and_then(|_| {
240+
#[inline]
241+
pub fn finish(mut self) -> fmt::Result {
242+
self.finish_inner();
243+
self.result
244+
}
245+
246+
fn finish_inner(&mut self) {
247+
self.result = self.result.and_then(|_| {
218248
let end = match (self.has_fields, self.is_pretty()) {
219249
(false, _) => "}",
220250
(true, false) => " }",
221251
(true, true) => "\n}",
222252
};
223253
self.fmt.write_str(end)
224-
})
254+
});
225255
}
226256

227257
fn is_pretty(&self) -> bool {
@@ -251,8 +281,13 @@ pub fn debug_map_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>, name: &str) -> Deb
251281
impl<'a, 'b: 'a> DebugMap<'a, 'b> {
252282
/// Adds a new entry to the map output.
253283
#[unstable(feature = "core", reason = "method was just created")]
254-
pub fn entry<K, V>(mut self, key: &K, value: &V) -> DebugMap<'a, 'b>
255-
where K: fmt::Debug, V: fmt::Debug {
284+
#[inline]
285+
pub fn entry(mut self, key: &fmt::Debug, value: &fmt::Debug) -> DebugMap<'a, 'b> {
286+
self.entry_inner(key, value);
287+
self
288+
}
289+
290+
fn entry_inner(&mut self, key: &fmt::Debug, value: &fmt::Debug) {
256291
self.result = self.result.and_then(|_| {
257292
let prefix = if self.has_fields {
258293
","
@@ -269,21 +304,26 @@ impl<'a, 'b: 'a> DebugMap<'a, 'b> {
269304
});
270305

271306
self.has_fields = true;
272-
self
273307
}
274308

275309
/// Consumes the `DebugMap`, finishing output and returning any error
276310
/// encountered.
277311
#[unstable(feature = "core", reason = "method was just created")]
278-
pub fn finish(self) -> fmt::Result {
279-
self.result.and_then(|_| {
312+
#[inline]
313+
pub fn finish(mut self) -> fmt::Result {
314+
self.finish_inner();
315+
self.result
316+
}
317+
318+
fn finish_inner(&mut self) {
319+
self.result = self.result.and_then(|_| {
280320
let end = match (self.has_fields, self.is_pretty()) {
281321
(false, _) => "}",
282322
(true, false) => " }",
283323
(true, true) => "\n}",
284324
};
285325
self.fmt.write_str(end)
286-
})
326+
});
287327
}
288328

289329
fn is_pretty(&self) -> bool {

0 commit comments

Comments
 (0)