Skip to content

Commit 18ef163

Browse files
committed
Receive formatter parameter as generic if dyn-compatible is not required
1 parent 7f26227 commit 18ef163

File tree

9 files changed

+43
-12
lines changed

9 files changed

+43
-12
lines changed

spdlog/src/sink/async_sink/async_pool_sink.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl AsyncPoolSink {
7272
// AsyncPoolSink does not have its own formatter, and we do not impl
7373
// `GetSinkProp` for it, so there should be no way to access the
7474
// formatter inside the `prop`.
75-
prop.set_formatter(Box::new(UnreachableFormatter::new()));
75+
prop.set_formatter(UnreachableFormatter::new());
7676

7777
AsyncPoolSinkBuilder {
7878
prop,
@@ -210,7 +210,10 @@ impl AsyncPoolSinkBuilder {
210210
///
211211
/// This parameter is **optional**.
212212
#[must_use]
213-
pub fn formatter(self, formatter: Box<dyn Formatter>) -> Self {
213+
pub fn formatter<F>(self, formatter: F) -> Self
214+
where
215+
F: Formatter + 'static,
216+
{
214217
self.prop.set_formatter(formatter);
215218
self
216219
}

spdlog/src/sink/dedup_sink.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,10 @@ impl<ArgS> DedupSinkBuilder<ArgS> {
254254
///
255255
/// This parameter is **optional**.
256256
#[must_use]
257-
pub fn formatter(self, formatter: Box<dyn Formatter>) -> Self {
257+
pub fn formatter<F>(self, formatter: F) -> Self
258+
where
259+
F: Formatter + 'static,
260+
{
258261
self.prop.set_formatter(formatter);
259262
self
260263
}

spdlog/src/sink/file_sink.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,10 @@ impl<ArgPath> FileSinkBuilder<ArgPath> {
187187
///
188188
/// This parameter is **optional**.
189189
#[must_use]
190-
pub fn formatter(self, formatter: Box<dyn Formatter>) -> Self {
190+
pub fn formatter<F>(self, formatter: F) -> Self
191+
where
192+
F: Formatter + 'static,
193+
{
191194
self.prop.set_formatter(formatter);
192195
self
193196
}

spdlog/src/sink/journald_sink.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl JournaldSink {
109109
#[must_use]
110110
pub fn builder() -> JournaldSinkBuilder {
111111
let prop = SinkProp::default();
112-
prop.set_formatter(Box::new(JournaldFormatter::new()));
112+
prop.set_formatter(JournaldFormatter::new());
113113

114114
JournaldSinkBuilder { prop }
115115
}
@@ -175,7 +175,10 @@ impl JournaldSinkBuilder {
175175
///
176176
/// This parameter is **optional**.
177177
#[must_use]
178-
pub fn formatter(self, formatter: Box<dyn Formatter>) -> Self {
178+
pub fn formatter<F>(self, formatter: F) -> Self
179+
where
180+
F: Formatter + 'static,
181+
{
179182
self.prop.set_formatter(formatter);
180183
self
181184
}

spdlog/src/sink/mod.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,14 @@ impl SinkProp {
103103
}
104104

105105
/// Sets the formatter.
106-
pub fn set_formatter(&self, formatter: Box<dyn Formatter>) {
106+
pub fn set_formatter<F>(&self, formatter: F)
107+
where
108+
F: Formatter + 'static,
109+
{
110+
self.set_formatter_boxed(Box::new(formatter));
111+
}
112+
113+
pub fn set_formatter_boxed(&self, formatter: Box<dyn Formatter>) {
107114
*self.formatter.write() = formatter;
108115
}
109116

@@ -173,7 +180,7 @@ impl<S: GetSinkProp> SinkAccess for S {
173180
}
174181

175182
fn set_formatter(&self, formatter: Box<dyn Formatter>) {
176-
self.prop().set_formatter(formatter);
183+
self.prop().set_formatter_boxed(formatter);
177184
}
178185

179186
fn set_error_handler(&self, handler: Option<ErrorHandler>) {

spdlog/src/sink/rotating_file_sink.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,10 @@ impl<ArgBP, ArgRP> RotatingFileSinkBuilder<ArgBP, ArgRP> {
815815
///
816816
/// This parameter is **optional**.
817817
#[must_use]
818-
pub fn formatter(self, formatter: Box<dyn Formatter>) -> Self {
818+
pub fn formatter<F>(self, formatter: F) -> Self
819+
where
820+
F: Formatter + 'static,
821+
{
819822
self.prop.set_formatter(formatter);
820823
self
821824
}

spdlog/src/sink/std_stream_sink.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,10 @@ impl<ArgSS> StdStreamSinkBuilder<ArgSS> {
273273
///
274274
/// This parameter is **optional**.
275275
#[must_use]
276-
pub fn formatter(self, formatter: Box<dyn Formatter>) -> Self {
276+
pub fn formatter<F>(self, formatter: F) -> Self
277+
where
278+
F: Formatter + 'static,
279+
{
277280
self.prop.set_formatter(formatter);
278281
self
279282
}

spdlog/src/sink/win_debug_sink.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ impl WinDebugSinkBuilder {
9595
///
9696
/// This parameter is **optional**.
9797
#[must_use]
98-
pub fn formatter(self, formatter: Box<dyn Formatter>) -> Self {
98+
pub fn formatter<F>(self, formatter: F) -> Self
99+
where
100+
F: Formatter + 'static,
101+
{
99102
self.prop.set_formatter(formatter);
100103
self
101104
}

spdlog/src/sink/write_sink.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,10 @@ where
180180
///
181181
/// This parameter is **optional**.
182182
#[must_use]
183-
pub fn formatter(self, formatter: Box<dyn Formatter>) -> Self {
183+
pub fn formatter<F>(self, formatter: F) -> Self
184+
where
185+
F: Formatter + 'static,
186+
{
184187
self.prop.set_formatter(formatter);
185188
self
186189
}

0 commit comments

Comments
 (0)