pub struct Value<'v> { /* private fields */ }
Expand description
A value in a key-value.
Values are an anonymous bag containing some structured datum.
§Capturing values
There are a few ways to capture a value:
- Using the
Value::from_*
methods. - Using the
ToValue
trait. - Using the standard
From
trait.
§Using the Value::from_*
methods
Value
offers a few constructor methods that capture values of different kinds.
use log::kv::Value;
let value = Value::from_debug(&42i32);
assert_eq!(None, value.to_i64());
§Using the ToValue
trait
The ToValue
trait can be used to capture values generically.
It’s the bound used by Source
.
let value = 42i32.to_value();
assert_eq!(Some(42), value.to_i64());
§Using the standard From
trait
Standard types that implement ToValue
also implement From
.
use log::kv::Value;
let value = Value::from(42i32);
assert_eq!(Some(42), value.to_i64());
§Data model
Values can hold one of a number of types:
- Null: The absence of any other meaningful value. Note that
Some(Value::null())
is not the same asNone
. The former isnull
while the latter isundefined
. This is important to be able to tell the difference between a key-value that was logged, but its value was empty (Some(Value::null())
) and a key-value that was never logged at all (None
). - Strings:
str
,char
. - Booleans:
bool
. - Integers:
u8
-u128
,i8
-i128
,NonZero*
. - Floating point numbers:
f32
-f64
. - Errors:
dyn (Error + 'static)
. serde
: Any type inserde
’s data model.sval
: Any type insval
’s data model.
§Serialization
Values provide a number of ways to be serialized.
For basic types the Value::visit
method can be used to extract the
underlying typed value. However this is limited in the amount of types
supported (see the VisitValue
trait methods).
For more complex types one of the following traits can be used:
sval::Value
, requires thekv_sval
feature.serde::Serialize
, requires thekv_serde
feature.
You don’t need a visitor to serialize values through serde
or sval
.
A value can always be serialized using any supported framework, regardless
of how it was captured. If, for example, a value was captured using its
Display
implementation, it will serialize through serde
as a string. If it was
captured as a struct using serde
, it will also serialize as a struct
through sval
, or can be formatted using a Debug
-compatible representation.
Implementations§
Source§impl<'v> Value<'v>
impl<'v> Value<'v>
Sourcepub fn from_any<T>(value: &'v T) -> Selfwhere
T: ToValue,
pub fn from_any<T>(value: &'v T) -> Selfwhere
T: ToValue,
Get a value from a type implementing ToValue
.
Sourcepub fn from_debug<T>(value: &'v T) -> Selfwhere
T: Debug,
pub fn from_debug<T>(value: &'v T) -> Selfwhere
T: Debug,
Get a value from a type implementing std::fmt::Debug
.
Sourcepub fn from_display<T>(value: &'v T) -> Selfwhere
T: Display,
pub fn from_display<T>(value: &'v T) -> Selfwhere
T: Display,
Get a value from a type implementing std::fmt::Display
.
Sourcepub fn from_dyn_debug(value: &'v dyn Debug) -> Self
pub fn from_dyn_debug(value: &'v dyn Debug) -> Self
Get a value from a dynamic std::fmt::Debug
.
Sourcepub fn from_dyn_display(value: &'v dyn Display) -> Self
pub fn from_dyn_display(value: &'v dyn Display) -> Self
Get a value from a dynamic std::fmt::Display
.