Skip to main content

Value

Enum Value 

Source
#[non_exhaustive]
pub enum Value<'a> {
Show 13 variants Null, Bool(bool), I64(i64), U64(u64), I128(i128), U128(u128), F64(f64), Str(&'a str), Char(char), Bytes(&'a [u8]), Display(&'a dyn Display), Debug(&'a dyn Debug), Error(&'a (dyn Error + 'static)),
}
Expand description

A field value attached to a log record.

Every variant borrows (or copies) its data, so constructing a Value never allocates. The lazy variants (Self::Display, Self::Debug, Self::Error) defer formatting until a sink actually serializes the record, so a record rejected by the filter never pays for them.

The enum is #[non_exhaustive]: new variants may be added in minor releases, so match statements outside this crate need a wildcard arm.

§Serialization

VariantJSONlogfmt / human
Nullnullempty / <null>
Booltrue / falsetrue / false
I64, U64, I128, U128numberdecimal
F64 (finite)number, always with . or exponent (1.0, 1e300)same text
F64 (NaN / infinite)nullNaN, inf, -inf
Str, Charescaped stringescaped, quoted when needed
Byteslowercase hex stringlowercase hex
Display, Debugescaped string of the rendered textrendered text, escaped
Errorthe error, then : source for each source, as a stringsame text

§Examples

use log_io::Value;

let port: Value<'_> = 8080_u16.into();
assert_eq!(port, Value::U64(8080));

let name = String::from("alice");
let v: Value<'_> = (&name).into();
assert_eq!(v, Value::Str("alice"));

// Lazy formatting: nothing is rendered until a sink serializes it.
let addr = std::net::Ipv4Addr::LOCALHOST;
let v = Value::Display(&addr);
assert_eq!(v.to_string(), "127.0.0.1");

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Null

Absence of a value.

§

Bool(bool)

A boolean.

§

I64(i64)

A signed 64-bit integer.

§

U64(u64)

An unsigned 64-bit integer.

§

I128(i128)

A signed 128-bit integer. JSON consumers that parse numbers as IEEE doubles lose precision above 2^53.

§

U128(u128)

An unsigned 128-bit integer. Same precision caveat as Self::I128.

§

F64(f64)

A 64-bit float. Finite values are written in Rust’s round-trippable {:?} form, which always carries a decimal point or an exponent so consumers never mistake 1.0 for an integer. NaN and infinities become JSON null because JSON has no representation for them.

§

Str(&'a str)

A borrowed string slice.

§

Char(char)

A single character, serialized as a one-character string.

§

Bytes(&'a [u8])

Raw bytes, serialized as lowercase hexadecimal.

§

Display(&'a dyn Display)

A value rendered through its fmt::Display implementation when (and only when) the record is serialized.

§

Debug(&'a dyn Debug)

A value rendered through its fmt::Debug implementation when (and only when) the record is serialized.

§

Error(&'a (dyn Error + 'static))

An error, rendered as its Display text followed by ": <source>" for every error in its source chain.

Implementations§

Source§

impl Value<'_>

Source

pub const fn is_null(&self) -> bool

Returns true if this value is Self::Null.

§Examples
use log_io::Value;
assert!(Value::Null.is_null());
assert!(!Value::Bool(false).is_null());
Source

pub const fn variant_name(&self) -> &'static str

The static name of the variant, for diagnostics.

§Examples
use log_io::Value;
assert_eq!(Value::U64(1).variant_name(), "u64");
assert_eq!(Value::Str("x").variant_name(), "str");

Trait Implementations§

Source§

impl<'a> Clone for Value<'a>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<'a> Copy for Value<'a>

Source§

impl Debug for Value<'_>

Source§

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

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

impl<'a> Default for Value<'a>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for Value<'_>

Source§

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

Renders the value’s plain text, without any format-specific quoting or escaping. Floats use the same text as the built-in formats.

Source§

impl<'a> From<&'a Cow<'a, str>> for Value<'a>

Available on crate feature std only.
Source§

fn from(value: &'a Cow<'a, str>) -> Self

Converts to this type from the input type.
Source§

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

Available on crate feature std only.
Source§

fn from(value: &'a String) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a Vec<u8>> for Value<'a>

Available on crate feature std only.
Source§

fn from(value: &'a Vec<u8>) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a [u8]> for Value<'a>

Source§

fn from(value: &'a [u8]) -> Self

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

impl<'a, T> From<Option<T>> for Value<'a>
where T: Into<Value<'a>>,

Source§

fn from(value: Option<T>) -> Self

Converts to this type from the input type.
Source§

impl From<bool> for Value<'_>

Source§

fn from(value: bool) -> Self

Converts to this type from the input type.
Source§

impl From<char> for Value<'_>

Source§

fn from(value: char) -> Self

Converts to this type from the input type.
Source§

impl From<f32> for Value<'_>

Source§

fn from(value: f32) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for Value<'_>

Source§

fn from(value: f64) -> Self

Converts to this type from the input type.
Source§

impl From<i8> for Value<'_>

Source§

fn from(value: i8) -> Self

Converts to this type from the input type.
Source§

impl From<i16> for Value<'_>

Source§

fn from(value: i16) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for Value<'_>

Source§

fn from(value: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for Value<'_>

Source§

fn from(value: i64) -> Self

Converts to this type from the input type.
Source§

impl From<i128> for Value<'_>

Source§

fn from(value: i128) -> Self

Converts to this type from the input type.
Source§

impl From<isize> for Value<'_>

Source§

fn from(value: isize) -> Self

Converts to this type from the input type.
Source§

impl From<u8> for Value<'_>

Source§

fn from(value: u8) -> Self

Converts to this type from the input type.
Source§

impl From<u16> for Value<'_>

Source§

fn from(value: u16) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for Value<'_>

Source§

fn from(value: u32) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for Value<'_>

Source§

fn from(value: u64) -> Self

Converts to this type from the input type.
Source§

impl From<u128> for Value<'_>

Source§

fn from(value: u128) -> Self

Converts to this type from the input type.
Source§

impl From<usize> for Value<'_>

Source§

fn from(value: usize) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Value<'_>

Source§

fn eq(&self, other: &Self) -> bool

Structural equality for data variants. The lazy variants (Value::Display, Value::Debug, Value::Error) compare equal only when they point at the same object; their rendered text is never computed for comparison. Floats follow IEEE semantics (NaN != NaN).

1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for Value<'a>

§

impl<'a> !Send for Value<'a>

§

impl<'a> !Sync for Value<'a>

§

impl<'a> !UnwindSafe for Value<'a>

§

impl<'a> Freeze for Value<'a>

§

impl<'a> Unpin for Value<'a>

§

impl<'a> UnsafeUnpin for Value<'a>

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> 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> 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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.