Skip to content

Commit 3acb571

Browse files
committed
refactor(fmt): Delegate formatting to DefaultFormat
1 parent e351bcb commit 3acb571

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

src/fmt/mod.rs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,20 @@ impl fmt::Debug for Formatter {
200200
}
201201
}
202202

203-
pub(crate) type FormatFn = Box<dyn Fn(&mut Formatter, &Record<'_>) -> io::Result<()> + Sync + Send>;
203+
pub(crate) trait RecordFormat {
204+
fn format(&self, formatter: &mut Formatter, record: &Record<'_>) -> io::Result<()>;
205+
}
206+
207+
impl<F> RecordFormat for F
208+
where
209+
F: Fn(&mut Formatter, &Record<'_>) -> io::Result<()>,
210+
{
211+
fn format(&self, formatter: &mut Formatter, record: &Record<'_>) -> io::Result<()> {
212+
(self)(formatter, record)
213+
}
214+
}
215+
216+
pub(crate) type FormatFn = Box<dyn RecordFormat + Sync + Send>;
204217

205218
#[derive(Default)]
206219
pub(crate) struct Builder {
@@ -229,15 +242,7 @@ impl Builder {
229242
if let Some(fmt) = built.custom_format {
230243
fmt
231244
} else {
232-
Box::new(move |buf, record| {
233-
let fmt = DefaultFormatWriter {
234-
format: &built.default_format,
235-
buf,
236-
written_header_value: false,
237-
};
238-
239-
fmt.write(record)
240-
})
245+
Box::new(built.default_format)
241246
}
242247
}
243248
}
@@ -304,6 +309,18 @@ impl Default for DefaultFormat {
304309
}
305310
}
306311

312+
impl RecordFormat for DefaultFormat {
313+
fn format(&self, formatter: &mut Formatter, record: &Record<'_>) -> io::Result<()> {
314+
let fmt = DefaultFormatWriter {
315+
format: self,
316+
buf: formatter,
317+
written_header_value: false,
318+
};
319+
320+
fmt.write(record)
321+
}
322+
}
323+
307324
/// The default format.
308325
///
309326
/// This format needs to work with any combination of crate features.

src/logger.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -664,8 +664,10 @@ impl Log for Logger {
664664
}
665665

666666
let print = |formatter: &mut Formatter, record: &Record<'_>| {
667-
let _ =
668-
(self.format)(formatter, record).and_then(|_| formatter.print(&self.writer));
667+
let _ = self
668+
.format
669+
.format(formatter, record)
670+
.and_then(|_| formatter.print(&self.writer));
669671

670672
// Always clear the buffer afterwards
671673
formatter.clear();

0 commit comments

Comments
 (0)