Value

Struct 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 from_any<T>(value: &'v T) -> Value<'v>
where T: ToValue,

Get a value from a type implementing ToValue.

Source

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

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

Source

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

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

Source

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

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

Source

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

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

Source

pub fn null() -> Value<'v>

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_str(&self) -> Option<&'v str>

Try to convert this value into a borrowed string.

Trait Implementations§

Source§

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

Source§

fn clone(&self) -> Value<'v>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

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

Source§

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

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

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

Source§

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

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

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

Source§

fn from(value: &'v NonZero<i128>) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: &'v NonZero<i16>) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: &'v NonZero<i32>) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: &'v NonZero<i64>) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: &'v NonZero<i8>) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: &'v NonZero<isize>) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: &'v NonZero<u128>) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: &'v NonZero<u16>) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: &'v NonZero<u32>) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: &'v NonZero<u64>) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: &'v NonZero<u8>) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: &'v NonZero<usize>) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

fn from(value: NonZero<i128>) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: NonZero<i16>) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: NonZero<i32>) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: NonZero<i64>) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: NonZero<i8>) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: NonZero<isize>) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: NonZero<u128>) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: NonZero<u16>) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: NonZero<u32>) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: NonZero<u64>) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: NonZero<u8>) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: NonZero<usize>) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: bool) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: char) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: f32) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: f64) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: i128) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: i16) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: i32) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: i64) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: i8) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: isize) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: u128) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: u16) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: u32) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: u64) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: u8) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

fn from(value: usize) -> Value<'v>

Converts to this type from the input type.
Source§

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

Source§

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

Perform the conversion.

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AsAny for T
where T: Any,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Source§

fn type_name(&self) -> &'static str

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

§

impl<T, D> Encode<Ambiguous1, D> for T
where D: ResourceDialect,

§

unsafe fn encode( self, _encoder: &mut Encoder<'_, D>, _offset: usize, _depth: Depth, ) -> Result<(), Error>

Encodes the object into the encoder’s buffers. Any handles stored in the object are swapped for Handle::INVALID. Read more
§

impl<T, D> Encode<Ambiguous2, D> for T
where D: ResourceDialect,

§

unsafe fn encode( self, _encoder: &mut Encoder<'_, D>, _offset: usize, _depth: Depth, ) -> Result<(), Error>

Encodes the object into the encoder’s buffers. Any handles stored in the object are swapped for Handle::INVALID. 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, U> Into32<U> for T
where U: MultiArchFrom<T>,

Source§

fn into_32(self) -> U

Source§

impl<T, U> Into64<U> for T
where U: MultiArchFrom<T>,

Source§

fn into_64(self) -> U

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

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

Source§

fn from_64(value: T) -> U

Source§

fn from_32(value: T) -> U

§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

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>,

Source§

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>,

Source§

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<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

Source§

impl<T> VertexId for T
where T: Display,

Source§

fn get_id(&self) -> Cow<'_, str>

Fetches the ID of a vertex, which must have a string representation.
Source§

impl<B, A> LockBefore<B> for A
where B: LockAfter<A>,

Source§

impl<B, A> LockEqualOrBefore<B> for A
where A: LockBefore<B>,