-
Notifications
You must be signed in to change notification settings - Fork 731
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core:
impl field::Value for String
(#2164)
## Motivation Currently, it is rather difficult to record `String`s as field values, even though `&str` is a primitive `Value` type. For example, this code does not currently compile: ```rust let my_string = String::from("hello world!"); tracing::debug!(my_string); ``` Instead, it is necessary to explicitly call `String::as_str` or a similar conversion method: ```rust let my_string = String::from("hello world!"); tracing::debug!(my_string = my_string.as_str()); ``` This is unfortunate, as it makes a fairly straightforward, commomplace task (recording a `String` as a field) unnecessarily verbose. ## Solution This branch adds an `impl Value for String` in `tracing-core` when the "alloc" feature flag is enabled. The impl simply calls `String::as_str` and then calls `record_str`. Because `Value` takes an `&self`, and there are preexisting `impl<T: Value> Value` for `&T` and `&mut T`, the string is not consumed, and `&String` or `&mut String`s can also be used as `Value`s. I've also added tests validating that this actually works.
- Loading branch information
Showing
3 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters