#[non_exhaustive]pub enum Value<'a> {
}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
| Variant | JSON | logfmt / human |
|---|---|---|
Null | null | empty / <null> |
Bool | true / false | true / false |
I64, U64, I128, U128 | number | decimal |
F64 (finite) | number, always with . or exponent (1.0, 1e300) | same text |
F64 (NaN / infinite) | null | NaN, inf, -inf |
Str, Char | escaped string | escaped, quoted when needed |
Bytes | lowercase hex string | lowercase hex |
Display, Debug | escaped string of the rendered text | rendered text, escaped |
Error | the error, then : source for each source, as a string | same 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
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<'_>
impl Value<'_>
Sourcepub const fn is_null(&self) -> bool
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());Sourcepub const fn variant_name(&self) -> &'static str
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§
impl<'a> Copy for Value<'a>
Source§impl PartialEq for Value<'_>
impl PartialEq for Value<'_>
Source§fn eq(&self, other: &Self) -> bool
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).