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
ToValuetrait. - Using the standard
Fromtrait.
§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 isnullwhile 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_svalfeature.serde::Serialize, requires thekv_serdefeature.
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) -> Value<'v>where
T: ToValue,
pub fn from_any<T>(value: &'v T) -> Value<'v>where
T: ToValue,
Get a value from a type implementing ToValue.
Sourcepub fn from_debug<T>(value: &'v T) -> Value<'v>where
T: Debug,
pub fn from_debug<T>(value: &'v T) -> Value<'v>where
T: Debug,
Get a value from a type implementing std::fmt::Debug.
Sourcepub fn from_display<T>(value: &'v T) -> Value<'v>where
T: Display,
pub fn from_display<T>(value: &'v T) -> Value<'v>where
T: Display,
Get a value from a type implementing std::fmt::Display.
Sourcepub fn from_dyn_debug(value: &'v dyn Debug) -> Value<'v>
pub fn from_dyn_debug(value: &'v dyn Debug) -> Value<'v>
Get a value from a dynamic std::fmt::Debug.
Sourcepub fn from_dyn_display(value: &'v dyn Display) -> Value<'v>
pub fn from_dyn_display(value: &'v dyn Display) -> Value<'v>
Get a value from a dynamic std::fmt::Display.
Trait Implementations§
Auto Trait Implementations§
impl<'v> Freeze for Value<'v>
impl<'v> !RefUnwindSafe for Value<'v>
impl<'v> !Send for Value<'v>
impl<'v> !Sync for Value<'v>
impl<'v> Unpin for Value<'v>
impl<'v> !UnwindSafe for Value<'v>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T, D> Encode<Ambiguous1, D> for Twhere
D: ResourceDialect,
impl<T, D> Encode<Ambiguous1, D> for Twhere
D: ResourceDialect,
§impl<T, D> Encode<Ambiguous2, D> for Twhere
D: ResourceDialect,
impl<T, D> Encode<Ambiguous2, D> for Twhere
D: ResourceDialect,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more