Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use ToValue as the bound for captured values #1

Merged
merged 1 commit into from
Sep 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ fn main() {
info!("hello {}", "cats", {
cat_1: "chashu",
cat_2: "nori",
cat_count: 2,
});
}
31 changes: 2 additions & 29 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ macro_rules! log_impl {
__log_format_args!($($arg),*),
lvl,
&(__log_module_path!(), __log_module_path!(), __log_file!(), __log_line!()),
Some(&[$((__log_stringify!($key), $value)),*])
Some(&[$((__log_stringify!($key), &$value)),*])
);
}
};
Expand Down Expand Up @@ -229,35 +229,8 @@ pub fn __private_api_log(
args: fmt::Arguments<'_>,
level: Level,
&(target, module_path, file, line): &(&str, &'static str, &'static str, u32),
kvs: Option<&[(&str, &str)]>,
kvs: Option<&[(&str, &dyn log::kv::ToValue)]>,
) {
// Ideally there would be a `From` impl available for this.
struct KeyValues<'a> {
inner: &'a [(&'a str, &'a str)],
}

impl<'a> log::kv::Source for KeyValues<'a> {
fn visit<'kvs>(
&'kvs self,
visitor: &mut dyn log::kv::Visitor<'kvs>,
) -> Result<(), log::kv::Error> {
for pair in self.inner {
visitor.visit_pair(pair.0.into(), pair.1.into())?;
}
Ok(())
}

#[inline]
fn count(&self) -> usize {
self.inner.len()
}
}

let kvs = match kvs {
Some(kvs) => Some(KeyValues { inner: kvs }),
None => None,
};

logger().log(
&Record::builder()
.args(args)
Expand Down