Skip to content

Commit ca51dbb

Browse files
committed
Formatter inherit DynClone to avoid explicit clone_box method
1 parent bc05795 commit ca51dbb

File tree

7 files changed

+4
-26
lines changed

7 files changed

+4
-26
lines changed

spdlog/examples/04_format.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ fn impl_manually() {
5757
.style_range(style_range_begin..style_range_end)
5858
.build())
5959
}
60-
61-
fn clone_box(&self) -> Box<dyn Formatter> {
62-
Box::new(self.clone())
63-
}
6460
}
6561

6662
// Building a custom formatter.

spdlog/src/formatter/full_formatter.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,6 @@ impl Formatter for FullFormatter {
108108
fn format(&self, record: &Record, dest: &mut StringBuf) -> crate::Result<FmtExtraInfo> {
109109
self.format_impl(record, dest).map_err(Error::FormatRecord)
110110
}
111-
112-
fn clone_box(&self) -> Box<dyn Formatter> {
113-
Box::new(self.clone())
114-
}
115111
}
116112

117113
impl Default for FullFormatter {

spdlog/src/formatter/journald_formatter.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,6 @@ impl Formatter for JournaldFormatter {
5959
fn format(&self, record: &Record, dest: &mut StringBuf) -> crate::Result<FmtExtraInfo> {
6060
self.format_impl(record, dest).map_err(Error::FormatRecord)
6161
}
62-
63-
fn clone_box(&self) -> Box<dyn Formatter> {
64-
Box::new(self.clone())
65-
}
6662
}
6763

6864
impl Default for JournaldFormatter {

spdlog/src/formatter/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ mod pattern_formatter;
5959

6060
use std::ops::Range;
6161

62+
use dyn_clone::*;
6263
pub use full_formatter::*;
6364
#[cfg(any(
6465
all(target_os = "linux", feature = "native", feature = "libsystemd"),
@@ -77,14 +78,11 @@ use crate::{Record, Result, StringBuf};
7778
/// See the implementation of [`FullFormatter`] and [./examples] directory.
7879
///
7980
/// [./examples]: https://github.com/SpriteOvO/spdlog-rs/tree/main/spdlog/examples
80-
pub trait Formatter: Send + Sync {
81+
pub trait Formatter: Send + Sync + DynClone {
8182
/// Formats a log record.
8283
fn format(&self, record: &Record, dest: &mut StringBuf) -> Result<FmtExtraInfo>;
83-
84-
/// Clones self into a boxed trait object.
85-
#[must_use]
86-
fn clone_box(&self) -> Box<dyn Formatter>;
8784
}
85+
clone_trait_object!(Formatter);
8886

8987
/// Extra information for formatted text.
9088
#[derive(Clone, Eq, PartialEq, Hash, Debug, Default)]

spdlog/src/formatter/pattern_formatter/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,6 @@ where
374374
self.pattern.format(record, dest, &mut ctx)?;
375375
Ok(ctx.fmt_info_builder.build())
376376
}
377-
378-
fn clone_box(&self) -> Box<dyn Formatter> {
379-
Box::new(self.clone())
380-
}
381377
}
382378

383379
/// Provides context for patterns.

spdlog/src/sink/async_sink/async_pool_sink.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl Sink for AsyncPoolSink {
129129
/// internal sinks.
130130
fn set_formatter(&self, formatter: Box<dyn Formatter>) {
131131
for sink in &self.backend.sinks {
132-
sink.set_formatter(formatter.clone_box())
132+
sink.set_formatter(formatter.clone())
133133
}
134134
}
135135

spdlog/src/test_utils/common.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,6 @@ impl Formatter for NoModFormatter {
199199

200200
Ok(FmtExtraInfo::new())
201201
}
202-
203-
fn clone_box(&self) -> Box<dyn Formatter> {
204-
Box::new(self.clone())
205-
}
206202
}
207203

208204
impl Default for NoModFormatter {

0 commit comments

Comments
 (0)