Key-value serializer adapters #426

Merged
KodrAus merged 2 commits from feat/kv-downcast into master 2020-12-20 23:32:39 -06:00
KodrAus commented 2020-12-20 23:22:49 -06:00 (Migrated from github.com)

This PR does two things:

  • Exposes the Value::downcast_ref method that was missed in #423
  • Adds adapters so that Sources can be treated like a map or sequence for formatting or serialization

This means Log implementors can now write something like:

use log::kv::source;

#[derive(Serialize)]
pub struct MyRecordAsMap<'a> {
    msg: &'a str,
    #[serde(flatten)]
    #[serde(with = "source::as_map")]
    kvs: &'a dyn Source,
}

to define a type that can be serialized using serde as a map with key values flattened.

This PR does two things: - Exposes the `Value::downcast_ref` method that was missed in #423 - Adds adapters so that `Source`s can be treated like a map or sequence for formatting or serialization This means `Log` implementors can now write something like: ```rust use log::kv::source; #[derive(Serialize)] pub struct MyRecordAsMap<'a> { msg: &'a str, #[serde(flatten)] #[serde(with = "source::as_map")] kvs: &'a dyn Source, } ``` to define a type that can be serialized using `serde` as a map with key values flattened.
passcod commented 2021-03-20 10:37:05 -05:00 (Migrated from github.com)

Would be nice to add that code sample to the kv docs as it's not super obvious how to use it otherwise

Would be nice to add that code sample to the kv docs as it's not super obvious how to use it otherwise
Sign in to join this conversation.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-rust-lang/log!426
No description provided.