Struct log::kv::Value

source ·
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 as None. The former is null while the latter is undefined. 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 in serde’s data model.
  • sval: Any type in sval’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 the kv_sval feature.
  • serde::Serialize, requires the kv_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>

source

pub fn to_cow_str(&self) -> Option<Cow<'v, str>>

Try convert this value into a string.

source§

impl<'v> Value<'v>

source

pub fn from_any<T>(value: &'v T) -> Self
where T: ToValue,

Get a value from a type implementing ToValue.

source

pub fn from_debug<T>(value: &'v T) -> Self
where T: Debug,

Get a value from a type implementing std::fmt::Debug.

source

pub fn from_display<T>(value: &'v T) -> Self
where T: Display,

Get a value from a type implementing std::fmt::Display.

source

pub fn from_serde<T>(value: &'v T) -> Self
where T: Serialize,

Get a value from a type implementing serde::Serialize.

source

pub fn from_sval<T>(value: &'v T) -> Self
where T: Value,

Get a value from a type implementing sval::Value.

source

pub fn from_dyn_debug(value: &'v dyn Debug) -> Self

Get a value from a dynamic std::fmt::Debug.

source

pub fn from_dyn_display(value: &'v dyn Display) -> Self

Get a value from a dynamic std::fmt::Display.

source

pub fn from_dyn_error(err: &'v (dyn Error + 'static)) -> Self

Get a value from a dynamic error.

source

pub fn null() -> Self

Get a null value.

source

pub fn visit(&self, visitor: impl VisitValue<'v>) -> Result<(), Error>

Inspect this value using a simple visitor.

When the kv_serde or kv_sval features are enabled, you can also serialize a value using its Serialize or Value implementation.

source§

impl<'v> Value<'v>

source

pub fn to_u64(&self) -> Option<u64>

Try convert this value into a u64.

source

pub fn to_i64(&self) -> Option<i64>

Try convert this value into a i64.

source

pub fn to_u128(&self) -> Option<u128>

Try convert this value into a u128.

source

pub fn to_i128(&self) -> Option<i128>

Try convert this value into a i128.

source

pub fn to_f64(&self) -> Option<f64>

Try convert this value into a f64.

source

pub fn to_char(&self) -> Option<char>

Try convert this value into a char.

source

pub fn to_bool(&self) -> Option<bool>

Try convert this value into a bool.

source§

impl<'v> Value<'v>

source

pub fn to_borrowed_error(&self) -> Option<&(dyn Error + 'static)>

Try convert this value into an error.

source

pub fn to_borrowed_str(&self) -> Option<&str>

Try convert this value into a borrowed string.

Trait Implementations§

source§

impl<'v> Debug for Value<'v>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'v> Display for Value<'v>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'v> From<&'v NonZero<i128>> for Value<'v>

source§

fn from(value: &'v NonZeroI128) -> Self

Converts to this type from the input type.
source§

impl<'v> From<&'v NonZero<i16>> for Value<'v>

source§

fn from(value: &'v NonZeroI16) -> Self

Converts to this type from the input type.
source§

impl<'v> From<&'v NonZero<i32>> for Value<'v>

source§

fn from(value: &'v NonZeroI32) -> Self

Converts to this type from the input type.
source§

impl<'v> From<&'v NonZero<i64>> for Value<'v>

source§

fn from(value: &'v NonZeroI64) -> Self

Converts to this type from the input type.
source§

impl<'v> From<&'v NonZero<i8>> for Value<'v>

source§

fn from(value: &'v NonZeroI8) -> Self

Converts to this type from the input type.
source§

impl<'v> From<&'v NonZero<isize>> for Value<'v>

source§

fn from(value: &'v NonZeroIsize) -> Self

Converts to this type from the input type.
source§

impl<'v> From<&'v NonZero<u128>> for Value<'v>

source§

fn from(value: &'v NonZeroU128) -> Self

Converts to this type from the input type.
source§

impl<'v> From<&'v NonZero<u16>> for Value<'v>

source§

fn from(value: &'v NonZeroU16) -> Self

Converts to this type from the input type.
source§

impl<'v> From<&'v NonZero<u32>> for Value<'v>

source§

fn from(value: &'v NonZeroU32) -> Self

Converts to this type from the input type.
source§

impl<'v> From<&'v NonZero<u64>> for Value<'v>

source§

fn from(value: &'v NonZeroU64) -> Self

Converts to this type from the input type.
source§

impl<'v> From<&'v NonZero<u8>> for Value<'v>

source§

fn from(value: &'v NonZeroU8) -> Self

Converts to this type from the input type.
source§

impl<'v> From<&'v NonZero<usize>> for Value<'v>

source§

fn from(value: &'v NonZeroUsize) -> Self

Converts to this type from the input type.
source§

impl<'v> From<&'v String> for Value<'v>

source§

fn from(v: &'v String) -> Self

Converts to this type from the input type.
source§

impl<'v> From<&'v bool> for Value<'v>

source§

fn from(value: &'v bool) -> Self

Converts to this type from the input type.
source§

impl<'v> From<&'v char> for Value<'v>

source§

fn from(value: &'v char) -> Self

Converts to this type from the input type.
source§

impl<'v> From<&'v f32> for Value<'v>

source§

fn from(value: &'v f32) -> Self

Converts to this type from the input type.
source§

impl<'v> From<&'v f64> for Value<'v>

source§

fn from(value: &'v f64) -> Self

Converts to this type from the input type.
source§

impl<'v> From<&'v i128> for Value<'v>

source§

fn from(value: &'v i128) -> Self

Converts to this type from the input type.
source§

impl<'v> From<&'v i16> for Value<'v>

source§

fn from(value: &'v i16) -> Self

Converts to this type from the input type.
source§

impl<'v> From<&'v i32> for Value<'v>

source§

fn from(value: &'v i32) -> Self

Converts to this type from the input type.
source§

impl<'v> From<&'v i64> for Value<'v>

source§

fn from(value: &'v i64) -> Self

Converts to this type from the input type.
source§

impl<'v> From<&'v i8> for Value<'v>

source§

fn from(value: &'v i8) -> Self

Converts to this type from the input type.
source§

impl<'v> From<&'v isize> for Value<'v>

source§

fn from(value: &'v isize) -> Self

Converts to this type from the input type.
source§

impl<'v> From<&'v str> for Value<'v>

source§

fn from(value: &'v str) -> Self

Converts to this type from the input type.
source§

impl<'v> From<&'v u128> for Value<'v>

source§

fn from(value: &'v u128) -> Self

Converts to this type from the input type.
source§

impl<'v> From<&'v u16> for Value<'v>

source§

fn from(value: &'v u16) -> Self

Converts to this type from the input type.
source§

impl<'v> From<&'v u32> for Value<'v>

source§

fn from(value: &'v u32) -> Self

Converts to this type from the input type.
source§

impl<'v> From<&'v u64> for Value<'v>

source§

fn from(value: &'v u64) -> Self

Converts to this type from the input type.
source§

impl<'v> From<&'v u8> for Value<'v>

source§

fn from(value: &'v u8) -> Self

Converts to this type from the input type.
source§

impl<'v> From<&'v usize> for Value<'v>

source§

fn from(value: &'v usize) -> Self

Converts to this type from the input type.
source§

impl<'v> From<NonZero<i128>> for Value<'v>

source§

fn from(value: NonZeroI128) -> Self

Converts to this type from the input type.
source§

impl<'v> From<NonZero<i16>> for Value<'v>

source§

fn from(value: NonZeroI16) -> Self

Converts to this type from the input type.
source§

impl<'v> From<NonZero<i32>> for Value<'v>

source§

fn from(value: NonZeroI32) -> Self

Converts to this type from the input type.
source§

impl<'v> From<NonZero<i64>> for Value<'v>

source§

fn from(value: NonZeroI64) -> Self

Converts to this type from the input type.
source§

impl<'v> From<NonZero<i8>> for Value<'v>

source§

fn from(value: NonZeroI8) -> Self

Converts to this type from the input type.
source§

impl<'v> From<NonZero<isize>> for Value<'v>

source§

fn from(value: NonZeroIsize) -> Self

Converts to this type from the input type.
source§

impl<'v> From<NonZero<u128>> for Value<'v>

source§

fn from(value: NonZeroU128) -> Self

Converts to this type from the input type.
source§

impl<'v> From<NonZero<u16>> for Value<'v>

source§

fn from(value: NonZeroU16) -> Self

Converts to this type from the input type.
source§

impl<'v> From<NonZero<u32>> for Value<'v>

source§

fn from(value: NonZeroU32) -> Self

Converts to this type from the input type.
source§

impl<'v> From<NonZero<u64>> for Value<'v>

source§

fn from(value: NonZeroU64) -> Self

Converts to this type from the input type.
source§

impl<'v> From<NonZero<u8>> for Value<'v>

source§

fn from(value: NonZeroU8) -> Self

Converts to this type from the input type.
source§

impl<'v> From<NonZero<usize>> for Value<'v>

source§

fn from(value: NonZeroUsize) -> Self

Converts to this type from the input type.
source§

impl<'v> From<bool> for Value<'v>

source§

fn from(value: bool) -> Self

Converts to this type from the input type.
source§

impl<'v> From<char> for Value<'v>

source§

fn from(value: char) -> Self

Converts to this type from the input type.
source§

impl<'v> From<f32> for Value<'v>

source§

fn from(value: f32) -> Self

Converts to this type from the input type.
source§

impl<'v> From<f64> for Value<'v>

source§

fn from(value: f64) -> Self

Converts to this type from the input type.
source§

impl<'v> From<i128> for Value<'v>

source§

fn from(value: i128) -> Self

Converts to this type from the input type.
source§

impl<'v> From<i16> for Value<'v>

source§

fn from(value: i16) -> Self

Converts to this type from the input type.
source§

impl<'v> From<i32> for Value<'v>

source§

fn from(value: i32) -> Self

Converts to this type from the input type.
source§

impl<'v> From<i64> for Value<'v>

source§

fn from(value: i64) -> Self

Converts to this type from the input type.
source§

impl<'v> From<i8> for Value<'v>

source§

fn from(value: i8) -> Self

Converts to this type from the input type.
source§

impl<'v> From<isize> for Value<'v>

source§

fn from(value: isize) -> Self

Converts to this type from the input type.
source§

impl<'v> From<u128> for Value<'v>

source§

fn from(value: u128) -> Self

Converts to this type from the input type.
source§

impl<'v> From<u16> for Value<'v>

source§

fn from(value: u16) -> Self

Converts to this type from the input type.
source§

impl<'v> From<u32> for Value<'v>

source§

fn from(value: u32) -> Self

Converts to this type from the input type.
source§

impl<'v> From<u64> for Value<'v>

source§

fn from(value: u64) -> Self

Converts to this type from the input type.
source§

impl<'v> From<u8> for Value<'v>

source§

fn from(value: u8) -> Self

Converts to this type from the input type.
source§

impl<'v> From<usize> for Value<'v>

source§

fn from(value: usize) -> Self

Converts to this type from the input type.
source§

impl<'v> Serialize for Value<'v>

source§

fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<'v> ToValue for Value<'v>

source§

fn to_value(&self) -> Value<'_>

Perform the conversion.
source§

impl<'v> Value for Value<'v>

source§

fn stream<'sval, S: Stream<'sval> + ?Sized>( &'sval self, stream: &mut S ) -> Result

Stream this value through a Stream.
source§

fn tag(&self) -> Option<Tag>

Get the tag of this value, if there is one.
source§

fn to_bool(&self) -> Option<bool>

Try convert this value into a boolean.
source§

fn to_f32(&self) -> Option<f32>

Try convert this value into a 32bit binary floating point number.
source§

fn to_f64(&self) -> Option<f64>

Try convert this value into a 64bit binary floating point number.
source§

fn to_i8(&self) -> Option<i8>

Try convert this value into a signed 8bit integer.
source§

fn to_i16(&self) -> Option<i16>

Try convert this value into a signed 16bit integer.
source§

fn to_i32(&self) -> Option<i32>

Try convert this value into a signed 32bit integer.
source§

fn to_i64(&self) -> Option<i64>

Try convert this value into a signed 64bit integer.
source§

fn to_i128(&self) -> Option<i128>

Try convert this value into a signed 128bit integer.
source§

fn to_u8(&self) -> Option<u8>

Try convert this value into an unsigned 8bit integer.
source§

fn to_u16(&self) -> Option<u16>

Try convert this value into an unsigned 16bit integer.
source§

fn to_u32(&self) -> Option<u32>

Try convert this value into an unsigned 32bit integer.
source§

fn to_u64(&self) -> Option<u64>

Try convert this value into an unsigned 64bit integer.
source§

fn to_u128(&self) -> Option<u128>

Try convert this value into an unsigned 128bit integer.
source§

fn to_text(&self) -> Option<&str>

Try convert this value into a text string.
source§

fn to_binary(&self) -> Option<&[u8]>

Try convert this value into a bitstring.
source§

impl<'v> ValueRef<'v> for Value<'v>

source§

fn stream_ref<S: Stream<'v> + ?Sized>(&self, stream: &mut S) -> Result

Stream this value through a Stream.

Auto Trait Implementations§

§

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Serialize for T
where T: Serialize + ?Sized,

source§

fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<(), Error>

source§

fn do_erased_serialize( &self, serializer: &mut dyn Serializer ) -> Result<(), ErrorImpl>

source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> Value for T
where T: Value,