Skip to content

Commit 3fa1048

Browse files
committed
Avoid using callbacks in LogTarget and LogKVs
1 parent 81dd465 commit 3fa1048

1 file changed

Lines changed: 32 additions & 16 deletions

File tree

src/__private_api.rs

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ mod sealed {
2626
}
2727

2828
pub trait LogTarget {
29-
fn with(self, module_path: &'static str, f: impl FnOnce(&str));
29+
fn as_str(&self, module_path: &'static str) -> &str;
3030
}
3131

3232
pub trait LogKVs {
33-
fn with(self, f: impl FnOnce(&[(&str, &super::LogKvValue)]));
33+
fn as_slice(&self) -> &[(&str, &super::LogKvValue)];
3434
}
3535
}
3636

@@ -103,14 +103,15 @@ define_static_levels![
103103

104104
// `LogArgs`.
105105

106-
impl LogArgs for &str {
106+
impl LogArgs for &'static str {
107107
#[inline]
108108
fn with(self, f: impl FnOnce(Arguments)) {
109109
f(format_args!("{self}"))
110110
}
111111
}
112112

113113
impl LogArgs for Arguments<'_> {
114+
#[inline]
114115
fn with(self, f: impl FnOnce(Arguments)) {
115116
f(self)
116117
}
@@ -120,8 +121,8 @@ impl LogArgs for Arguments<'_> {
120121

121122
impl LogTarget for &str {
122123
#[inline]
123-
fn with(self, _module_path: &'static str, f: impl FnOnce(&str)) {
124-
f(self)
124+
fn as_str(&self, _module_path: &'static str) -> &str {
125+
self
125126
}
126127
}
127128

@@ -130,17 +131,17 @@ pub struct TargetIsModulePath;
130131

131132
impl LogTarget for TargetIsModulePath {
132133
#[inline]
133-
fn with(self, module_path: &'static str, f: impl FnOnce(&str)) {
134-
f(module_path)
134+
fn as_str(&self, module_path: &'static str) -> &str {
135+
module_path
135136
}
136137
}
137138

138139
// `LogKVs`.
139140

140141
impl LogKVs for &[(&str, &LogKvValue<'_>)] {
141142
#[inline]
142-
fn with(self, f: impl FnOnce(&[(&str, &LogKvValue)])) {
143-
f(self)
143+
fn as_slice(&self) -> &[(&str, &LogKvValue)] {
144+
self
144145
}
145146
}
146147

@@ -149,8 +150,8 @@ pub struct EmptyKVs;
149150

150151
impl LogKVs for EmptyKVs {
151152
#[inline]
152-
fn with(self, f: impl FnOnce(&[(&str, &LogKvValue)])) {
153-
f(&[])
153+
fn as_slice(&self) -> &[(&str, &LogKvValue)] {
154+
&[]
154155
}
155156
}
156157

@@ -197,7 +198,14 @@ fn log_1<K>(
197198
) where
198199
K: LogKVs,
199200
{
200-
kvs.with(|kvs| log_0(module_path_and_file, line, level, args, target, kvs));
201+
log_0(
202+
module_path_and_file,
203+
line,
204+
level,
205+
args,
206+
target,
207+
kvs.as_slice(),
208+
);
201209
}
202210

203211
fn log_2<T, K>(
@@ -211,9 +219,14 @@ fn log_2<T, K>(
211219
T: LogTarget,
212220
K: LogKVs,
213221
{
214-
target.with(module_path_and_file.0, |target| {
215-
log_1(module_path_and_file, line, level, args, target, kvs)
216-
});
222+
log_1(
223+
module_path_and_file,
224+
line,
225+
level,
226+
args,
227+
target.as_str(module_path_and_file.0),
228+
kvs,
229+
)
217230
}
218231

219232
pub fn log_3<A, T, K>(
@@ -258,6 +271,7 @@ pub fn enabled(level: Level, target: &str) -> bool {
258271
crate::logger().enabled(&Metadata::builder().level(level).target(target).build())
259272
}
260273

274+
/// Check whether the format argument can be treated as a string literal.
261275
pub const fn is_literal(s: &str) -> bool {
262276
let s = s.as_bytes();
263277
let n = s.len();
@@ -274,7 +288,9 @@ pub const fn is_literal(s: &str) -> bool {
274288
true
275289
}
276290

277-
pub fn unused() -> usize {
291+
/// This function is used for persuading the compiler to generate all `log` generic function instances, so they can be
292+
/// reused by downstream crates, it is not intended for calling by anyone.
293+
pub fn instantiate_log_functions() -> usize {
278294
fn for_all_kvs<L, A, T>() -> usize
279295
where
280296
L: LogLevel,

0 commit comments

Comments
 (0)