Coverage Report

Created: 2026-01-13 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/log-0.4.29/src/kv/value.rs
Line
Count
Source
1
//! Structured values.
2
//!
3
//! This module defines the [`Value`] type and supporting APIs for
4
//! capturing and serializing them.
5
6
use std::fmt;
7
8
pub use crate::kv::Error;
9
10
/// A type that can be converted into a [`Value`](struct.Value.html).
11
pub trait ToValue {
12
    /// Perform the conversion.
13
    fn to_value(&self) -> Value<'_>;
14
}
15
16
impl<T> ToValue for &T
17
where
18
    T: ToValue + ?Sized,
19
{
20
0
    fn to_value(&self) -> Value<'_> {
21
0
        (**self).to_value()
22
0
    }
23
}
24
25
impl<'v> ToValue for Value<'v> {
26
0
    fn to_value(&self) -> Value<'_> {
27
0
        Value {
28
0
            inner: self.inner.clone(),
29
0
        }
30
0
    }
31
}
32
33
/// A value in a key-value.
34
///
35
/// Values are an anonymous bag containing some structured datum.
36
///
37
/// # Capturing values
38
///
39
/// There are a few ways to capture a value:
40
///
41
/// - Using the `Value::from_*` methods.
42
/// - Using the `ToValue` trait.
43
/// - Using the standard `From` trait.
44
///
45
/// ## Using the `Value::from_*` methods
46
///
47
/// `Value` offers a few constructor methods that capture values of different kinds.
48
///
49
/// ```
50
/// use log::kv::Value;
51
///
52
/// let value = Value::from_debug(&42i32);
53
///
54
/// assert_eq!(None, value.to_i64());
55
/// ```
56
///
57
/// ## Using the `ToValue` trait
58
///
59
/// The `ToValue` trait can be used to capture values generically.
60
/// It's the bound used by `Source`.
61
///
62
/// ```
63
/// # use log::kv::ToValue;
64
/// let value = 42i32.to_value();
65
///
66
/// assert_eq!(Some(42), value.to_i64());
67
/// ```
68
///
69
/// ## Using the standard `From` trait
70
///
71
/// Standard types that implement `ToValue` also implement `From`.
72
///
73
/// ```
74
/// use log::kv::Value;
75
///
76
/// let value = Value::from(42i32);
77
///
78
/// assert_eq!(Some(42), value.to_i64());
79
/// ```
80
///
81
/// # Data model
82
///
83
/// Values can hold one of a number of types:
84
///
85
/// - **Null:** The absence of any other meaningful value. Note that
86
///   `Some(Value::null())` is not the same as `None`. The former is
87
///   `null` while the latter is `undefined`. This is important to be
88
///   able to tell the difference between a key-value that was logged,
89
///   but its value was empty (`Some(Value::null())`) and a key-value
90
///   that was never logged at all (`None`).
91
/// - **Strings:** `str`, `char`.
92
/// - **Booleans:** `bool`.
93
/// - **Integers:** `u8`-`u128`, `i8`-`i128`, `NonZero*`.
94
/// - **Floating point numbers:** `f32`-`f64`.
95
/// - **Errors:** `dyn (Error + 'static)`.
96
/// - **`serde`:** Any type in `serde`'s data model.
97
/// - **`sval`:** Any type in `sval`'s data model.
98
///
99
/// # Serialization
100
///
101
/// Values provide a number of ways to be serialized.
102
///
103
/// For basic types the [`Value::visit`] method can be used to extract the
104
/// underlying typed value. However, this is limited in the amount of types
105
/// supported (see the [`VisitValue`] trait methods).
106
///
107
/// For more complex types one of the following traits can be used:
108
///  * `sval::Value`, requires the `kv_sval` feature.
109
///  * `serde::Serialize`, requires the `kv_serde` feature.
110
///
111
/// You don't need a visitor to serialize values through `serde` or `sval`.
112
///
113
/// A value can always be serialized using any supported framework, regardless
114
/// of how it was captured. If, for example, a value was captured using its
115
/// `Display` implementation, it will serialize through `serde` as a string. If it was
116
/// captured as a struct using `serde`, it will also serialize as a struct
117
/// through `sval`, or can be formatted using a `Debug`-compatible representation.
118
#[derive(Clone)]
119
pub struct Value<'v> {
120
    inner: inner::Inner<'v>,
121
}
122
123
impl<'v> Value<'v> {
124
    /// Get a value from a type implementing `ToValue`.
125
0
    pub fn from_any<T>(value: &'v T) -> Self
126
0
    where
127
0
        T: ToValue,
128
    {
129
0
        value.to_value()
130
0
    }
131
132
    /// Get a value from a type implementing `std::fmt::Debug`.
133
0
    pub fn from_debug<T>(value: &'v T) -> Self
134
0
    where
135
0
        T: fmt::Debug,
136
    {
137
0
        Value {
138
0
            inner: inner::Inner::from_debug(value),
139
0
        }
140
0
    }
141
142
    /// Get a value from a type implementing `std::fmt::Display`.
143
0
    pub fn from_display<T>(value: &'v T) -> Self
144
0
    where
145
0
        T: fmt::Display,
146
    {
147
0
        Value {
148
0
            inner: inner::Inner::from_display(value),
149
0
        }
150
0
    }
151
152
    /// Get a value from a type implementing `serde::Serialize`.
153
    #[cfg(feature = "kv_serde")]
154
    pub fn from_serde<T>(value: &'v T) -> Self
155
    where
156
        T: serde_core::Serialize,
157
    {
158
        Value {
159
            inner: inner::Inner::from_serde1(value),
160
        }
161
    }
162
163
    /// Get a value from a type implementing `sval::Value`.
164
    #[cfg(feature = "kv_sval")]
165
    pub fn from_sval<T>(value: &'v T) -> Self
166
    where
167
        T: sval::Value,
168
    {
169
        Value {
170
            inner: inner::Inner::from_sval2(value),
171
        }
172
    }
173
174
    /// Get a value from a dynamic `std::fmt::Debug`.
175
0
    pub fn from_dyn_debug(value: &'v dyn fmt::Debug) -> Self {
176
0
        Value {
177
0
            inner: inner::Inner::from_dyn_debug(value),
178
0
        }
179
0
    }
180
181
    /// Get a value from a dynamic `std::fmt::Display`.
182
0
    pub fn from_dyn_display(value: &'v dyn fmt::Display) -> Self {
183
0
        Value {
184
0
            inner: inner::Inner::from_dyn_display(value),
185
0
        }
186
0
    }
187
188
    /// Get a value from a dynamic error.
189
    #[cfg(feature = "kv_std")]
190
    pub fn from_dyn_error(err: &'v (dyn std::error::Error + 'static)) -> Self {
191
        Value {
192
            inner: inner::Inner::from_dyn_error(err),
193
        }
194
    }
195
196
    /// Get a `null` value.
197
0
    pub fn null() -> Self {
198
0
        Value {
199
0
            inner: inner::Inner::empty(),
200
0
        }
201
0
    }
202
203
    /// Get a value from an internal primitive.
204
0
    fn from_inner<T>(value: T) -> Self
205
0
    where
206
0
        T: Into<inner::Inner<'v>>,
207
    {
208
0
        Value {
209
0
            inner: value.into(),
210
0
        }
211
0
    }
Unexecuted instantiation: <log::kv::value::Value>::from_inner::<&str>
Unexecuted instantiation: <log::kv::value::Value>::from_inner::<i8>
Unexecuted instantiation: <log::kv::value::Value>::from_inner::<bool>
Unexecuted instantiation: <log::kv::value::Value>::from_inner::<char>
Unexecuted instantiation: <log::kv::value::Value>::from_inner::<f64>
Unexecuted instantiation: <log::kv::value::Value>::from_inner::<f32>
Unexecuted instantiation: <log::kv::value::Value>::from_inner::<u8>
Unexecuted instantiation: <log::kv::value::Value>::from_inner::<isize>
Unexecuted instantiation: <log::kv::value::Value>::from_inner::<usize>
Unexecuted instantiation: <log::kv::value::Value>::from_inner::<i32>
Unexecuted instantiation: <log::kv::value::Value>::from_inner::<u32>
Unexecuted instantiation: <log::kv::value::Value>::from_inner::<i128>
Unexecuted instantiation: <log::kv::value::Value>::from_inner::<u128>
Unexecuted instantiation: <log::kv::value::Value>::from_inner::<i16>
Unexecuted instantiation: <log::kv::value::Value>::from_inner::<u16>
Unexecuted instantiation: <log::kv::value::Value>::from_inner::<()>
Unexecuted instantiation: <log::kv::value::Value>::from_inner::<i64>
Unexecuted instantiation: <log::kv::value::Value>::from_inner::<u64>
212
213
    /// Inspect this value using a simple visitor.
214
    ///
215
    /// When the `kv_serde` or `kv_sval` features are enabled, you can also
216
    /// serialize a value using its `Serialize` or `Value` implementation.
217
0
    pub fn visit(&self, visitor: impl VisitValue<'v>) -> Result<(), Error> {
218
0
        inner::visit(&self.inner, visitor)
219
0
    }
220
}
221
222
impl<'v> fmt::Debug for Value<'v> {
223
0
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
224
0
        fmt::Debug::fmt(&self.inner, f)
225
0
    }
226
}
227
228
impl<'v> fmt::Display for Value<'v> {
229
0
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
230
0
        fmt::Display::fmt(&self.inner, f)
231
0
    }
232
}
233
234
#[cfg(feature = "kv_serde")]
235
impl<'v> serde_core::Serialize for Value<'v> {
236
    fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
237
    where
238
        S: serde_core::Serializer,
239
    {
240
        self.inner.serialize(s)
241
    }
242
}
243
244
#[cfg(feature = "kv_sval")]
245
impl<'v> sval::Value for Value<'v> {
246
    fn stream<'sval, S: sval::Stream<'sval> + ?Sized>(&'sval self, stream: &mut S) -> sval::Result {
247
        sval::Value::stream(&self.inner, stream)
248
    }
249
}
250
251
#[cfg(feature = "kv_sval")]
252
impl<'v> sval_ref::ValueRef<'v> for Value<'v> {
253
    fn stream_ref<S: sval::Stream<'v> + ?Sized>(&self, stream: &mut S) -> sval::Result {
254
        sval_ref::ValueRef::stream_ref(&self.inner, stream)
255
    }
256
}
257
258
impl ToValue for str {
259
0
    fn to_value(&self) -> Value<'_> {
260
0
        Value::from(self)
261
0
    }
262
}
263
264
impl<'v> From<&'v str> for Value<'v> {
265
0
    fn from(value: &'v str) -> Self {
266
0
        Value::from_inner(value)
267
0
    }
268
}
269
270
impl ToValue for () {
271
0
    fn to_value(&self) -> Value<'_> {
272
0
        Value::from_inner(())
273
0
    }
274
}
275
276
impl<T> ToValue for Option<T>
277
where
278
    T: ToValue,
279
{
280
0
    fn to_value(&self) -> Value<'_> {
281
0
        match *self {
282
0
            Some(ref value) => value.to_value(),
283
0
            None => Value::from_inner(()),
284
        }
285
0
    }
286
}
287
288
macro_rules! impl_to_value_primitive {
289
    ($($into_ty:ty,)*) => {
290
        $(
291
            impl ToValue for $into_ty {
292
0
                fn to_value(&self) -> Value<'_> {
293
0
                    Value::from(*self)
294
0
                }
Unexecuted instantiation: <i32 as log::kv::value::ToValue>::to_value
Unexecuted instantiation: <i64 as log::kv::value::ToValue>::to_value
Unexecuted instantiation: <i128 as log::kv::value::ToValue>::to_value
Unexecuted instantiation: <f32 as log::kv::value::ToValue>::to_value
Unexecuted instantiation: <f64 as log::kv::value::ToValue>::to_value
Unexecuted instantiation: <char as log::kv::value::ToValue>::to_value
Unexecuted instantiation: <bool as log::kv::value::ToValue>::to_value
Unexecuted instantiation: <usize as log::kv::value::ToValue>::to_value
Unexecuted instantiation: <u8 as log::kv::value::ToValue>::to_value
Unexecuted instantiation: <u16 as log::kv::value::ToValue>::to_value
Unexecuted instantiation: <u32 as log::kv::value::ToValue>::to_value
Unexecuted instantiation: <u64 as log::kv::value::ToValue>::to_value
Unexecuted instantiation: <u128 as log::kv::value::ToValue>::to_value
Unexecuted instantiation: <isize as log::kv::value::ToValue>::to_value
Unexecuted instantiation: <i8 as log::kv::value::ToValue>::to_value
Unexecuted instantiation: <i16 as log::kv::value::ToValue>::to_value
295
            }
296
297
            impl<'v> From<$into_ty> for Value<'v> {
298
0
                fn from(value: $into_ty) -> Self {
299
0
                    Value::from_inner(value)
300
0
                }
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<i16>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<i32>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<i64>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<i128>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<f32>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<f64>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<char>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<bool>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<usize>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<u8>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<u16>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<u32>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<u64>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<u128>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<isize>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<i8>>::from
301
            }
302
303
            impl<'v> From<&'v $into_ty> for Value<'v> {
304
0
                fn from(value: &'v $into_ty) -> Self {
305
0
                    Value::from_inner(*value)
306
0
                }
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<&i16>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<&i32>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<&i64>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<&i128>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<&f32>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<&f64>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<&char>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<&bool>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<&usize>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<&u8>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<&u16>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<&u32>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<&u64>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<&u128>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<&isize>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<&i8>>::from
307
            }
308
        )*
309
    };
310
}
311
312
macro_rules! impl_to_value_nonzero_primitive {
313
    ($($into_ty:ident,)*) => {
314
        $(
315
            impl ToValue for std::num::$into_ty {
316
0
                fn to_value(&self) -> Value<'_> {
317
0
                    Value::from(self.get())
318
0
                }
Unexecuted instantiation: <core::num::nonzero::NonZero<u8> as log::kv::value::ToValue>::to_value
Unexecuted instantiation: <core::num::nonzero::NonZero<u16> as log::kv::value::ToValue>::to_value
Unexecuted instantiation: <core::num::nonzero::NonZero<u32> as log::kv::value::ToValue>::to_value
Unexecuted instantiation: <core::num::nonzero::NonZero<u64> as log::kv::value::ToValue>::to_value
Unexecuted instantiation: <core::num::nonzero::NonZero<u128> as log::kv::value::ToValue>::to_value
Unexecuted instantiation: <core::num::nonzero::NonZero<isize> as log::kv::value::ToValue>::to_value
Unexecuted instantiation: <core::num::nonzero::NonZero<i8> as log::kv::value::ToValue>::to_value
Unexecuted instantiation: <core::num::nonzero::NonZero<i16> as log::kv::value::ToValue>::to_value
Unexecuted instantiation: <core::num::nonzero::NonZero<i32> as log::kv::value::ToValue>::to_value
Unexecuted instantiation: <core::num::nonzero::NonZero<i64> as log::kv::value::ToValue>::to_value
Unexecuted instantiation: <core::num::nonzero::NonZero<i128> as log::kv::value::ToValue>::to_value
Unexecuted instantiation: <core::num::nonzero::NonZero<usize> as log::kv::value::ToValue>::to_value
319
            }
320
321
            impl<'v> From<std::num::$into_ty> for Value<'v> {
322
0
                fn from(value: std::num::$into_ty) -> Self {
323
0
                    Value::from(value.get())
324
0
                }
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<core::num::nonzero::NonZero<u8>>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<core::num::nonzero::NonZero<u16>>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<core::num::nonzero::NonZero<u32>>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<core::num::nonzero::NonZero<u64>>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<core::num::nonzero::NonZero<u128>>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<core::num::nonzero::NonZero<isize>>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<core::num::nonzero::NonZero<i8>>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<core::num::nonzero::NonZero<i16>>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<core::num::nonzero::NonZero<i32>>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<core::num::nonzero::NonZero<i64>>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<core::num::nonzero::NonZero<i128>>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<core::num::nonzero::NonZero<usize>>>::from
325
            }
326
327
            impl<'v> From<&'v std::num::$into_ty> for Value<'v> {
328
0
                fn from(value: &'v std::num::$into_ty) -> Self {
329
0
                    Value::from(value.get())
330
0
                }
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<&core::num::nonzero::NonZero<u8>>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<&core::num::nonzero::NonZero<u16>>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<&core::num::nonzero::NonZero<u32>>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<&core::num::nonzero::NonZero<u64>>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<&core::num::nonzero::NonZero<u128>>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<&core::num::nonzero::NonZero<isize>>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<&core::num::nonzero::NonZero<i8>>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<&core::num::nonzero::NonZero<i16>>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<&core::num::nonzero::NonZero<i32>>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<&core::num::nonzero::NonZero<i64>>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<&core::num::nonzero::NonZero<i128>>>::from
Unexecuted instantiation: <log::kv::value::Value as core::convert::From<&core::num::nonzero::NonZero<usize>>>::from
331
            }
332
        )*
333
    };
334
}
335
336
macro_rules! impl_value_to_primitive {
337
    ($(#[doc = $doc:tt] $into_name:ident -> $into_ty:ty,)*) => {
338
        impl<'v> Value<'v> {
339
            $(
340
                #[doc = $doc]
341
0
                pub fn $into_name(&self) -> Option<$into_ty> {
342
0
                    self.inner.$into_name()
343
0
                }
Unexecuted instantiation: <log::kv::value::Value>::to_f64
Unexecuted instantiation: <log::kv::value::Value>::to_i64
Unexecuted instantiation: <log::kv::value::Value>::to_u64
Unexecuted instantiation: <log::kv::value::Value>::to_bool
Unexecuted instantiation: <log::kv::value::Value>::to_char
Unexecuted instantiation: <log::kv::value::Value>::to_i128
Unexecuted instantiation: <log::kv::value::Value>::to_u128
344
            )*
345
        }
346
    }
347
}
348
349
impl_to_value_primitive![
350
    usize, u8, u16, u32, u64, u128, isize, i8, i16, i32, i64, i128, f32, f64, char, bool,
351
];
352
353
#[rustfmt::skip]
354
impl_to_value_nonzero_primitive![
355
    NonZeroUsize, NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128,
356
    NonZeroIsize, NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128,
357
];
358
359
impl_value_to_primitive![
360
    #[doc = "Try convert this value into a `u64`."]
361
    to_u64 -> u64,
362
    #[doc = "Try convert this value into a `i64`."]
363
    to_i64 -> i64,
364
    #[doc = "Try convert this value into a `u128`."]
365
    to_u128 -> u128,
366
    #[doc = "Try convert this value into a `i128`."]
367
    to_i128 -> i128,
368
    #[doc = "Try convert this value into a `f64`."]
369
    to_f64 -> f64,
370
    #[doc = "Try convert this value into a `char`."]
371
    to_char -> char,
372
    #[doc = "Try convert this value into a `bool`."]
373
    to_bool -> bool,
374
];
375
376
impl<'v> Value<'v> {
377
    /// Try to convert this value into an error.
378
    #[cfg(feature = "kv_std")]
379
    pub fn to_borrowed_error(&self) -> Option<&(dyn std::error::Error + 'static)> {
380
        self.inner.to_borrowed_error()
381
    }
382
383
    /// Try to convert this value into a borrowed string.
384
0
    pub fn to_borrowed_str(&self) -> Option<&'v str> {
385
0
        self.inner.to_borrowed_str()
386
0
    }
387
}
388
389
#[cfg(feature = "kv_std")]
390
mod std_support {
391
    use std::borrow::Cow;
392
    use std::rc::Rc;
393
    use std::sync::Arc;
394
395
    use super::*;
396
397
    impl<T> ToValue for Box<T>
398
    where
399
        T: ToValue + ?Sized,
400
    {
401
        fn to_value(&self) -> Value<'_> {
402
            (**self).to_value()
403
        }
404
    }
405
406
    impl<T> ToValue for Arc<T>
407
    where
408
        T: ToValue + ?Sized,
409
    {
410
        fn to_value(&self) -> Value<'_> {
411
            (**self).to_value()
412
        }
413
    }
414
415
    impl<T> ToValue for Rc<T>
416
    where
417
        T: ToValue + ?Sized,
418
    {
419
        fn to_value(&self) -> Value<'_> {
420
            (**self).to_value()
421
        }
422
    }
423
424
    impl ToValue for String {
425
        fn to_value(&self) -> Value<'_> {
426
            Value::from(&**self)
427
        }
428
    }
429
430
    impl<'v> ToValue for Cow<'v, str> {
431
        fn to_value(&self) -> Value<'_> {
432
            Value::from(&**self)
433
        }
434
    }
435
436
    impl<'v> Value<'v> {
437
        /// Try convert this value into a string.
438
        pub fn to_cow_str(&self) -> Option<Cow<'v, str>> {
439
            self.inner.to_str()
440
        }
441
    }
442
443
    impl<'v> From<&'v String> for Value<'v> {
444
        fn from(v: &'v String) -> Self {
445
            Value::from(&**v)
446
        }
447
    }
448
}
449
450
/// A visitor for a [`Value`].
451
///
452
/// Also see [`Value`'s documentation on serialization]. Value visitors are a simple alternative
453
/// to a more fully-featured serialization framework like `serde` or `sval`. A value visitor
454
/// can differentiate primitive types through methods like [`VisitValue::visit_bool`] and
455
/// [`VisitValue::visit_str`], but more complex types like maps and sequences
456
/// will fallthrough to [`VisitValue::visit_any`].
457
///
458
/// If you're trying to serialize a value to a format like JSON, you can use either `serde`
459
/// or `sval` directly with the value. You don't need a visitor.
460
///
461
/// [`Value`'s documentation on serialization]: Value#serialization
462
pub trait VisitValue<'v> {
463
    /// Visit a `Value`.
464
    ///
465
    /// This is the only required method on `VisitValue` and acts as a fallback for any
466
    /// more specific methods that aren't overridden.
467
    /// The `Value` may be formatted using its `fmt::Debug` or `fmt::Display` implementation,
468
    /// or serialized using its `sval::Value` or `serde::Serialize` implementation.
469
    fn visit_any(&mut self, value: Value) -> Result<(), Error>;
470
471
    /// Visit an empty value.
472
0
    fn visit_null(&mut self) -> Result<(), Error> {
473
0
        self.visit_any(Value::null())
474
0
    }
475
476
    /// Visit an unsigned integer.
477
0
    fn visit_u64(&mut self, value: u64) -> Result<(), Error> {
478
0
        self.visit_any(value.into())
479
0
    }
480
481
    /// Visit a signed integer.
482
0
    fn visit_i64(&mut self, value: i64) -> Result<(), Error> {
483
0
        self.visit_any(value.into())
484
0
    }
485
486
    /// Visit a big unsigned integer.
487
0
    fn visit_u128(&mut self, value: u128) -> Result<(), Error> {
488
0
        self.visit_any((value).into())
489
0
    }
490
491
    /// Visit a big signed integer.
492
0
    fn visit_i128(&mut self, value: i128) -> Result<(), Error> {
493
0
        self.visit_any((value).into())
494
0
    }
495
496
    /// Visit a floating point.
497
0
    fn visit_f64(&mut self, value: f64) -> Result<(), Error> {
498
0
        self.visit_any(value.into())
499
0
    }
500
501
    /// Visit a boolean.
502
0
    fn visit_bool(&mut self, value: bool) -> Result<(), Error> {
503
0
        self.visit_any(value.into())
504
0
    }
505
506
    /// Visit a string.
507
0
    fn visit_str(&mut self, value: &str) -> Result<(), Error> {
508
0
        self.visit_any(value.into())
509
0
    }
510
511
    /// Visit a string.
512
0
    fn visit_borrowed_str(&mut self, value: &'v str) -> Result<(), Error> {
513
0
        self.visit_str(value)
514
0
    }
515
516
    /// Visit a Unicode character.
517
0
    fn visit_char(&mut self, value: char) -> Result<(), Error> {
518
0
        let mut b = [0; 4];
519
0
        self.visit_str(&*value.encode_utf8(&mut b))
520
0
    }
521
522
    /// Visit an error.
523
    #[cfg(feature = "kv_std")]
524
    fn visit_error(&mut self, err: &(dyn std::error::Error + 'static)) -> Result<(), Error> {
525
        self.visit_any(Value::from_dyn_error(err))
526
    }
527
528
    /// Visit an error.
529
    #[cfg(feature = "kv_std")]
530
    fn visit_borrowed_error(
531
        &mut self,
532
        err: &'v (dyn std::error::Error + 'static),
533
    ) -> Result<(), Error> {
534
        self.visit_any(Value::from_dyn_error(err))
535
    }
536
}
537
538
#[allow(clippy::needless_lifetimes)] // Not needless.
539
impl<'a, 'v, T: ?Sized> VisitValue<'v> for &'a mut T
540
where
541
    T: VisitValue<'v>,
542
{
543
0
    fn visit_any(&mut self, value: Value) -> Result<(), Error> {
544
0
        (**self).visit_any(value)
545
0
    }
546
547
0
    fn visit_null(&mut self) -> Result<(), Error> {
548
0
        (**self).visit_null()
549
0
    }
550
551
0
    fn visit_u64(&mut self, value: u64) -> Result<(), Error> {
552
0
        (**self).visit_u64(value)
553
0
    }
554
555
0
    fn visit_i64(&mut self, value: i64) -> Result<(), Error> {
556
0
        (**self).visit_i64(value)
557
0
    }
558
559
0
    fn visit_u128(&mut self, value: u128) -> Result<(), Error> {
560
0
        (**self).visit_u128(value)
561
0
    }
562
563
0
    fn visit_i128(&mut self, value: i128) -> Result<(), Error> {
564
0
        (**self).visit_i128(value)
565
0
    }
566
567
0
    fn visit_f64(&mut self, value: f64) -> Result<(), Error> {
568
0
        (**self).visit_f64(value)
569
0
    }
570
571
0
    fn visit_bool(&mut self, value: bool) -> Result<(), Error> {
572
0
        (**self).visit_bool(value)
573
0
    }
574
575
0
    fn visit_str(&mut self, value: &str) -> Result<(), Error> {
576
0
        (**self).visit_str(value)
577
0
    }
578
579
0
    fn visit_borrowed_str(&mut self, value: &'v str) -> Result<(), Error> {
580
0
        (**self).visit_borrowed_str(value)
581
0
    }
582
583
0
    fn visit_char(&mut self, value: char) -> Result<(), Error> {
584
0
        (**self).visit_char(value)
585
0
    }
586
587
    #[cfg(feature = "kv_std")]
588
    fn visit_error(&mut self, err: &(dyn std::error::Error + 'static)) -> Result<(), Error> {
589
        (**self).visit_error(err)
590
    }
591
592
    #[cfg(feature = "kv_std")]
593
    fn visit_borrowed_error(
594
        &mut self,
595
        err: &'v (dyn std::error::Error + 'static),
596
    ) -> Result<(), Error> {
597
        (**self).visit_borrowed_error(err)
598
    }
599
}
600
601
#[cfg(feature = "value-bag")]
602
pub(in crate::kv) mod inner {
603
    /**
604
    An implementation of `Value` based on a library called `value_bag`.
605
606
    `value_bag` was written specifically for use in `log`'s value, but was split out when it outgrew
607
    the codebase here. It's a general-purpose type-erasure library that handles mapping between
608
    more fully-featured serialization frameworks.
609
    */
610
    use super::*;
611
612
    pub use value_bag::ValueBag as Inner;
613
614
    pub use value_bag::Error;
615
616
    #[cfg(test)]
617
    pub use value_bag::test::TestToken as Token;
618
619
    pub fn visit<'v>(
620
        inner: &Inner<'v>,
621
        visitor: impl VisitValue<'v>,
622
    ) -> Result<(), crate::kv::Error> {
623
        struct InnerVisitValue<V>(V);
624
625
        impl<'v, V> value_bag::visit::Visit<'v> for InnerVisitValue<V>
626
        where
627
            V: VisitValue<'v>,
628
        {
629
            fn visit_any(&mut self, value: value_bag::ValueBag) -> Result<(), Error> {
630
                self.0
631
                    .visit_any(Value { inner: value })
632
                    .map_err(crate::kv::Error::into_value)
633
            }
634
635
            fn visit_empty(&mut self) -> Result<(), Error> {
636
                self.0.visit_null().map_err(crate::kv::Error::into_value)
637
            }
638
639
            fn visit_u64(&mut self, value: u64) -> Result<(), Error> {
640
                self.0
641
                    .visit_u64(value)
642
                    .map_err(crate::kv::Error::into_value)
643
            }
644
645
            fn visit_i64(&mut self, value: i64) -> Result<(), Error> {
646
                self.0
647
                    .visit_i64(value)
648
                    .map_err(crate::kv::Error::into_value)
649
            }
650
651
            fn visit_u128(&mut self, value: u128) -> Result<(), Error> {
652
                self.0
653
                    .visit_u128(value)
654
                    .map_err(crate::kv::Error::into_value)
655
            }
656
657
            fn visit_i128(&mut self, value: i128) -> Result<(), Error> {
658
                self.0
659
                    .visit_i128(value)
660
                    .map_err(crate::kv::Error::into_value)
661
            }
662
663
            fn visit_f64(&mut self, value: f64) -> Result<(), Error> {
664
                self.0
665
                    .visit_f64(value)
666
                    .map_err(crate::kv::Error::into_value)
667
            }
668
669
            fn visit_bool(&mut self, value: bool) -> Result<(), Error> {
670
                self.0
671
                    .visit_bool(value)
672
                    .map_err(crate::kv::Error::into_value)
673
            }
674
675
            fn visit_str(&mut self, value: &str) -> Result<(), Error> {
676
                self.0
677
                    .visit_str(value)
678
                    .map_err(crate::kv::Error::into_value)
679
            }
680
681
            fn visit_borrowed_str(&mut self, value: &'v str) -> Result<(), Error> {
682
                self.0
683
                    .visit_borrowed_str(value)
684
                    .map_err(crate::kv::Error::into_value)
685
            }
686
687
            fn visit_char(&mut self, value: char) -> Result<(), Error> {
688
                self.0
689
                    .visit_char(value)
690
                    .map_err(crate::kv::Error::into_value)
691
            }
692
693
            #[cfg(feature = "kv_std")]
694
            fn visit_error(
695
                &mut self,
696
                err: &(dyn std::error::Error + 'static),
697
            ) -> Result<(), Error> {
698
                self.0
699
                    .visit_error(err)
700
                    .map_err(crate::kv::Error::into_value)
701
            }
702
703
            #[cfg(feature = "kv_std")]
704
            fn visit_borrowed_error(
705
                &mut self,
706
                err: &'v (dyn std::error::Error + 'static),
707
            ) -> Result<(), Error> {
708
                self.0
709
                    .visit_borrowed_error(err)
710
                    .map_err(crate::kv::Error::into_value)
711
            }
712
        }
713
714
        inner
715
            .visit(&mut InnerVisitValue(visitor))
716
            .map_err(crate::kv::Error::from_value)
717
    }
718
}
719
720
#[cfg(not(feature = "value-bag"))]
721
pub(in crate::kv) mod inner {
722
    /**
723
    This is a dependency-free implementation of `Value` when there's no serialization frameworks involved.
724
    In these simple cases a more fully featured solution like `value_bag` isn't needed, so we avoid pulling it in.
725
726
    There are a few things here that need to remain consistent with the `value_bag`-based implementation:
727
728
    1. Conversions should always produce the same results. If a conversion here returns `Some`, then
729
       the same `value_bag`-based conversion must also. Of particular note here are floats to ints; they're
730
       based on the standard library's `TryInto` conversions, which need to be converted to `i32` or `u32`,
731
       and then to `f64`.
732
    2. VisitValues should always be called in the same way. If a particular type of value calls `visit_i64`,
733
       then the same `value_bag`-based visitor must also.
734
    */
735
    use super::*;
736
737
    #[derive(Clone)]
738
    pub enum Inner<'v> {
739
        None,
740
        Bool(bool),
741
        Str(&'v str),
742
        Char(char),
743
        I64(i64),
744
        U64(u64),
745
        F64(f64),
746
        I128(i128),
747
        U128(u128),
748
        Debug(&'v dyn fmt::Debug),
749
        Display(&'v dyn fmt::Display),
750
    }
751
752
    impl<'v> From<()> for Inner<'v> {
753
0
        fn from(_: ()) -> Self {
754
0
            Inner::None
755
0
        }
756
    }
757
758
    impl<'v> From<bool> for Inner<'v> {
759
0
        fn from(v: bool) -> Self {
760
0
            Inner::Bool(v)
761
0
        }
762
    }
763
764
    impl<'v> From<char> for Inner<'v> {
765
0
        fn from(v: char) -> Self {
766
0
            Inner::Char(v)
767
0
        }
768
    }
769
770
    impl<'v> From<f32> for Inner<'v> {
771
0
        fn from(v: f32) -> Self {
772
0
            Inner::F64(v as f64)
773
0
        }
774
    }
775
776
    impl<'v> From<f64> for Inner<'v> {
777
0
        fn from(v: f64) -> Self {
778
0
            Inner::F64(v)
779
0
        }
780
    }
781
782
    impl<'v> From<i8> for Inner<'v> {
783
0
        fn from(v: i8) -> Self {
784
0
            Inner::I64(v as i64)
785
0
        }
786
    }
787
788
    impl<'v> From<i16> for Inner<'v> {
789
0
        fn from(v: i16) -> Self {
790
0
            Inner::I64(v as i64)
791
0
        }
792
    }
793
794
    impl<'v> From<i32> for Inner<'v> {
795
0
        fn from(v: i32) -> Self {
796
0
            Inner::I64(v as i64)
797
0
        }
798
    }
799
800
    impl<'v> From<i64> for Inner<'v> {
801
0
        fn from(v: i64) -> Self {
802
0
            Inner::I64(v as i64)
803
0
        }
804
    }
805
806
    impl<'v> From<isize> for Inner<'v> {
807
0
        fn from(v: isize) -> Self {
808
0
            Inner::I64(v as i64)
809
0
        }
810
    }
811
812
    impl<'v> From<u8> for Inner<'v> {
813
0
        fn from(v: u8) -> Self {
814
0
            Inner::U64(v as u64)
815
0
        }
816
    }
817
818
    impl<'v> From<u16> for Inner<'v> {
819
0
        fn from(v: u16) -> Self {
820
0
            Inner::U64(v as u64)
821
0
        }
822
    }
823
824
    impl<'v> From<u32> for Inner<'v> {
825
0
        fn from(v: u32) -> Self {
826
0
            Inner::U64(v as u64)
827
0
        }
828
    }
829
830
    impl<'v> From<u64> for Inner<'v> {
831
0
        fn from(v: u64) -> Self {
832
0
            Inner::U64(v as u64)
833
0
        }
834
    }
835
836
    impl<'v> From<usize> for Inner<'v> {
837
0
        fn from(v: usize) -> Self {
838
0
            Inner::U64(v as u64)
839
0
        }
840
    }
841
842
    impl<'v> From<i128> for Inner<'v> {
843
0
        fn from(v: i128) -> Self {
844
0
            Inner::I128(v)
845
0
        }
846
    }
847
848
    impl<'v> From<u128> for Inner<'v> {
849
0
        fn from(v: u128) -> Self {
850
0
            Inner::U128(v)
851
0
        }
852
    }
853
854
    impl<'v> From<&'v str> for Inner<'v> {
855
0
        fn from(v: &'v str) -> Self {
856
0
            Inner::Str(v)
857
0
        }
858
    }
859
860
    impl<'v> fmt::Debug for Inner<'v> {
861
0
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
862
0
            match self {
863
0
                Inner::None => fmt::Debug::fmt(&None::<()>, f),
864
0
                Inner::Bool(v) => fmt::Debug::fmt(v, f),
865
0
                Inner::Str(v) => fmt::Debug::fmt(v, f),
866
0
                Inner::Char(v) => fmt::Debug::fmt(v, f),
867
0
                Inner::I64(v) => fmt::Debug::fmt(v, f),
868
0
                Inner::U64(v) => fmt::Debug::fmt(v, f),
869
0
                Inner::F64(v) => fmt::Debug::fmt(v, f),
870
0
                Inner::I128(v) => fmt::Debug::fmt(v, f),
871
0
                Inner::U128(v) => fmt::Debug::fmt(v, f),
872
0
                Inner::Debug(v) => fmt::Debug::fmt(v, f),
873
0
                Inner::Display(v) => fmt::Display::fmt(v, f),
874
            }
875
0
        }
876
    }
877
878
    impl<'v> fmt::Display for Inner<'v> {
879
0
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
880
0
            match self {
881
0
                Inner::None => fmt::Debug::fmt(&None::<()>, f),
882
0
                Inner::Bool(v) => fmt::Display::fmt(v, f),
883
0
                Inner::Str(v) => fmt::Display::fmt(v, f),
884
0
                Inner::Char(v) => fmt::Display::fmt(v, f),
885
0
                Inner::I64(v) => fmt::Display::fmt(v, f),
886
0
                Inner::U64(v) => fmt::Display::fmt(v, f),
887
0
                Inner::F64(v) => fmt::Display::fmt(v, f),
888
0
                Inner::I128(v) => fmt::Display::fmt(v, f),
889
0
                Inner::U128(v) => fmt::Display::fmt(v, f),
890
0
                Inner::Debug(v) => fmt::Debug::fmt(v, f),
891
0
                Inner::Display(v) => fmt::Display::fmt(v, f),
892
            }
893
0
        }
894
    }
895
896
    impl<'v> Inner<'v> {
897
0
        pub fn from_debug<T: fmt::Debug>(value: &'v T) -> Self {
898
0
            Inner::Debug(value)
899
0
        }
900
901
0
        pub fn from_display<T: fmt::Display>(value: &'v T) -> Self {
902
0
            Inner::Display(value)
903
0
        }
904
905
0
        pub fn from_dyn_debug(value: &'v dyn fmt::Debug) -> Self {
906
0
            Inner::Debug(value)
907
0
        }
908
909
0
        pub fn from_dyn_display(value: &'v dyn fmt::Display) -> Self {
910
0
            Inner::Display(value)
911
0
        }
912
913
0
        pub fn empty() -> Self {
914
0
            Inner::None
915
0
        }
916
917
0
        pub fn to_bool(&self) -> Option<bool> {
918
0
            match self {
919
0
                Inner::Bool(v) => Some(*v),
920
0
                _ => None,
921
            }
922
0
        }
923
924
0
        pub fn to_char(&self) -> Option<char> {
925
0
            match self {
926
0
                Inner::Char(v) => Some(*v),
927
0
                _ => None,
928
            }
929
0
        }
930
931
0
        pub fn to_f64(&self) -> Option<f64> {
932
0
            match self {
933
0
                Inner::F64(v) => Some(*v),
934
0
                Inner::I64(v) => {
935
0
                    let v: i32 = (*v).try_into().ok()?;
936
0
                    v.try_into().ok()
937
                }
938
0
                Inner::U64(v) => {
939
0
                    let v: u32 = (*v).try_into().ok()?;
940
0
                    v.try_into().ok()
941
                }
942
0
                Inner::I128(v) => {
943
0
                    let v: i32 = (*v).try_into().ok()?;
944
0
                    v.try_into().ok()
945
                }
946
0
                Inner::U128(v) => {
947
0
                    let v: u32 = (*v).try_into().ok()?;
948
0
                    v.try_into().ok()
949
                }
950
0
                _ => None,
951
            }
952
0
        }
953
954
0
        pub fn to_i64(&self) -> Option<i64> {
955
0
            match self {
956
0
                Inner::I64(v) => Some(*v),
957
0
                Inner::U64(v) => (*v).try_into().ok(),
958
0
                Inner::I128(v) => (*v).try_into().ok(),
959
0
                Inner::U128(v) => (*v).try_into().ok(),
960
0
                _ => None,
961
            }
962
0
        }
963
964
0
        pub fn to_u64(&self) -> Option<u64> {
965
0
            match self {
966
0
                Inner::U64(v) => Some(*v),
967
0
                Inner::I64(v) => (*v).try_into().ok(),
968
0
                Inner::I128(v) => (*v).try_into().ok(),
969
0
                Inner::U128(v) => (*v).try_into().ok(),
970
0
                _ => None,
971
            }
972
0
        }
973
974
0
        pub fn to_u128(&self) -> Option<u128> {
975
0
            match self {
976
0
                Inner::U128(v) => Some(*v),
977
0
                Inner::I64(v) => (*v).try_into().ok(),
978
0
                Inner::U64(v) => (*v).try_into().ok(),
979
0
                Inner::I128(v) => (*v).try_into().ok(),
980
0
                _ => None,
981
            }
982
0
        }
983
984
0
        pub fn to_i128(&self) -> Option<i128> {
985
0
            match self {
986
0
                Inner::I128(v) => Some(*v),
987
0
                Inner::I64(v) => (*v).try_into().ok(),
988
0
                Inner::U64(v) => (*v).try_into().ok(),
989
0
                Inner::U128(v) => (*v).try_into().ok(),
990
0
                _ => None,
991
            }
992
0
        }
993
994
0
        pub fn to_borrowed_str(&self) -> Option<&'v str> {
995
0
            match self {
996
0
                Inner::Str(v) => Some(v),
997
0
                _ => None,
998
            }
999
0
        }
1000
1001
        #[cfg(test)]
1002
        pub fn to_test_token(&self) -> Token {
1003
            match self {
1004
                Inner::None => Token::None,
1005
                Inner::Bool(v) => Token::Bool(*v),
1006
                Inner::Str(v) => Token::Str(*v),
1007
                Inner::Char(v) => Token::Char(*v),
1008
                Inner::I64(v) => Token::I64(*v),
1009
                Inner::U64(v) => Token::U64(*v),
1010
                Inner::F64(v) => Token::F64(*v),
1011
                Inner::I128(_) => unimplemented!(),
1012
                Inner::U128(_) => unimplemented!(),
1013
                Inner::Debug(_) => unimplemented!(),
1014
                Inner::Display(_) => unimplemented!(),
1015
            }
1016
        }
1017
    }
1018
1019
    #[cfg(test)]
1020
    #[derive(Debug, PartialEq)]
1021
    pub enum Token<'v> {
1022
        None,
1023
        Bool(bool),
1024
        Char(char),
1025
        Str(&'v str),
1026
        F64(f64),
1027
        I64(i64),
1028
        U64(u64),
1029
    }
1030
1031
0
    pub fn visit<'v>(
1032
0
        inner: &Inner<'v>,
1033
0
        mut visitor: impl VisitValue<'v>,
1034
0
    ) -> Result<(), crate::kv::Error> {
1035
0
        match inner {
1036
0
            Inner::None => visitor.visit_null(),
1037
0
            Inner::Bool(v) => visitor.visit_bool(*v),
1038
0
            Inner::Str(v) => visitor.visit_borrowed_str(*v),
1039
0
            Inner::Char(v) => visitor.visit_char(*v),
1040
0
            Inner::I64(v) => visitor.visit_i64(*v),
1041
0
            Inner::U64(v) => visitor.visit_u64(*v),
1042
0
            Inner::F64(v) => visitor.visit_f64(*v),
1043
0
            Inner::I128(v) => visitor.visit_i128(*v),
1044
0
            Inner::U128(v) => visitor.visit_u128(*v),
1045
0
            Inner::Debug(v) => visitor.visit_any(Value::from_dyn_debug(*v)),
1046
0
            Inner::Display(v) => visitor.visit_any(Value::from_dyn_display(*v)),
1047
        }
1048
0
    }
1049
}
1050
1051
impl<'v> Value<'v> {
1052
    /// Get a value from a type implementing `std::fmt::Debug`.
1053
    #[cfg(feature = "kv_unstable")]
1054
    #[deprecated(note = "use `from_debug` instead")]
1055
    pub fn capture_debug<T>(value: &'v T) -> Self
1056
    where
1057
        T: fmt::Debug + 'static,
1058
    {
1059
        Value::from_debug(value)
1060
    }
1061
1062
    /// Get a value from a type implementing `std::fmt::Display`.
1063
    #[cfg(feature = "kv_unstable")]
1064
    #[deprecated(note = "use `from_display` instead")]
1065
    pub fn capture_display<T>(value: &'v T) -> Self
1066
    where
1067
        T: fmt::Display + 'static,
1068
    {
1069
        Value::from_display(value)
1070
    }
1071
1072
    /// Get a value from an error.
1073
    #[cfg(feature = "kv_unstable_std")]
1074
    #[deprecated(note = "use `from_dyn_error` instead")]
1075
    pub fn capture_error<T>(err: &'v T) -> Self
1076
    where
1077
        T: std::error::Error + 'static,
1078
    {
1079
        Value::from_dyn_error(err)
1080
    }
1081
1082
    /// Get a value from a type implementing `serde::Serialize`.
1083
    #[cfg(feature = "kv_unstable_serde")]
1084
    #[deprecated(note = "use `from_serde` instead")]
1085
    pub fn capture_serde<T>(value: &'v T) -> Self
1086
    where
1087
        T: serde_core::Serialize + 'static,
1088
    {
1089
        Value::from_serde(value)
1090
    }
1091
1092
    /// Get a value from a type implementing `sval::Value`.
1093
    #[cfg(feature = "kv_unstable_sval")]
1094
    #[deprecated(note = "use `from_sval` instead")]
1095
    pub fn capture_sval<T>(value: &'v T) -> Self
1096
    where
1097
        T: sval::Value + 'static,
1098
    {
1099
        Value::from_sval(value)
1100
    }
1101
1102
    /// Check whether this value can be downcast to `T`.
1103
    #[cfg(feature = "kv_unstable")]
1104
    #[deprecated(
1105
        note = "downcasting has been removed; log an issue at https://github.com/rust-lang/log/issues if this is something you rely on"
1106
    )]
1107
    pub fn is<T: 'static>(&self) -> bool {
1108
        false
1109
    }
1110
1111
    /// Try downcast this value to `T`.
1112
    #[cfg(feature = "kv_unstable")]
1113
    #[deprecated(
1114
        note = "downcasting has been removed; log an issue at https://github.com/rust-lang/log/issues if this is something you rely on"
1115
    )]
1116
    pub fn downcast_ref<T: 'static>(&self) -> Option<&T> {
1117
        None
1118
    }
1119
}
1120
1121
// NOTE: Deprecated; but aliases can't carry this attribute
1122
#[cfg(feature = "kv_unstable")]
1123
pub use VisitValue as Visit;
1124
1125
/// Get a value from a type implementing `std::fmt::Debug`.
1126
#[cfg(feature = "kv_unstable")]
1127
#[deprecated(note = "use the `key:? = value` macro syntax instead")]
1128
#[macro_export]
1129
macro_rules! as_debug {
1130
    ($capture:expr) => {
1131
        $crate::kv::Value::from_debug(&$capture)
1132
    };
1133
}
1134
1135
/// Get a value from a type implementing `std::fmt::Display`.
1136
#[cfg(feature = "kv_unstable")]
1137
#[deprecated(note = "use the `key:% = value` macro syntax instead")]
1138
#[macro_export]
1139
macro_rules! as_display {
1140
    ($capture:expr) => {
1141
        $crate::kv::Value::from_display(&$capture)
1142
    };
1143
}
1144
1145
/// Get a value from an error.
1146
#[cfg(feature = "kv_unstable_std")]
1147
#[deprecated(note = "use the `key:err = value` macro syntax instead")]
1148
#[macro_export]
1149
macro_rules! as_error {
1150
    ($capture:expr) => {
1151
        $crate::kv::Value::from_dyn_error(&$capture)
1152
    };
1153
}
1154
1155
#[cfg(feature = "kv_unstable_serde")]
1156
#[deprecated(note = "use the `key:serde = value` macro syntax instead")]
1157
/// Get a value from a type implementing `serde::Serialize`.
1158
#[macro_export]
1159
macro_rules! as_serde {
1160
    ($capture:expr) => {
1161
        $crate::kv::Value::from_serde(&$capture)
1162
    };
1163
}
1164
1165
/// Get a value from a type implementing `sval::Value`.
1166
#[cfg(feature = "kv_unstable_sval")]
1167
#[deprecated(note = "use the `key:sval = value` macro syntax instead")]
1168
#[macro_export]
1169
macro_rules! as_sval {
1170
    ($capture:expr) => {
1171
        $crate::kv::Value::from_sval(&$capture)
1172
    };
1173
}
1174
1175
#[cfg(test)]
1176
pub(crate) mod tests {
1177
    use super::*;
1178
1179
    impl<'v> Value<'v> {
1180
        pub(crate) fn to_token(&self) -> inner::Token {
1181
            self.inner.to_test_token()
1182
        }
1183
    }
1184
1185
    fn unsigned() -> impl Iterator<Item = Value<'static>> {
1186
        vec![
1187
            Value::from(8u8),
1188
            Value::from(16u16),
1189
            Value::from(32u32),
1190
            Value::from(64u64),
1191
            Value::from(1usize),
1192
            Value::from(std::num::NonZeroU8::new(8).unwrap()),
1193
            Value::from(std::num::NonZeroU16::new(16).unwrap()),
1194
            Value::from(std::num::NonZeroU32::new(32).unwrap()),
1195
            Value::from(std::num::NonZeroU64::new(64).unwrap()),
1196
            Value::from(std::num::NonZeroUsize::new(1).unwrap()),
1197
        ]
1198
        .into_iter()
1199
    }
1200
1201
    fn signed() -> impl Iterator<Item = Value<'static>> {
1202
        vec![
1203
            Value::from(-8i8),
1204
            Value::from(-16i16),
1205
            Value::from(-32i32),
1206
            Value::from(-64i64),
1207
            Value::from(-1isize),
1208
            Value::from(std::num::NonZeroI8::new(-8).unwrap()),
1209
            Value::from(std::num::NonZeroI16::new(-16).unwrap()),
1210
            Value::from(std::num::NonZeroI32::new(-32).unwrap()),
1211
            Value::from(std::num::NonZeroI64::new(-64).unwrap()),
1212
            Value::from(std::num::NonZeroIsize::new(-1).unwrap()),
1213
        ]
1214
        .into_iter()
1215
    }
1216
1217
    fn float() -> impl Iterator<Item = Value<'static>> {
1218
        vec![Value::from(32.32f32), Value::from(64.64f64)].into_iter()
1219
    }
1220
1221
    fn bool() -> impl Iterator<Item = Value<'static>> {
1222
        vec![Value::from(true), Value::from(false)].into_iter()
1223
    }
1224
1225
    fn str() -> impl Iterator<Item = Value<'static>> {
1226
        vec![Value::from("a string"), Value::from("a loong string")].into_iter()
1227
    }
1228
1229
    fn char() -> impl Iterator<Item = Value<'static>> {
1230
        vec![Value::from('a'), Value::from('⛰')].into_iter()
1231
    }
1232
1233
    #[test]
1234
    fn test_to_value_display() {
1235
        assert_eq!(42u64.to_value().to_string(), "42");
1236
        assert_eq!(42i64.to_value().to_string(), "42");
1237
        assert_eq!(42.01f64.to_value().to_string(), "42.01");
1238
        assert_eq!(true.to_value().to_string(), "true");
1239
        assert_eq!('a'.to_value().to_string(), "a");
1240
        assert_eq!("a loong string".to_value().to_string(), "a loong string");
1241
        assert_eq!(Some(true).to_value().to_string(), "true");
1242
        assert_eq!(().to_value().to_string(), "None");
1243
        assert_eq!(None::<bool>.to_value().to_string(), "None");
1244
    }
1245
1246
    #[test]
1247
    fn test_to_value_structured() {
1248
        assert_eq!(42u64.to_value().to_token(), inner::Token::U64(42));
1249
        assert_eq!(42i64.to_value().to_token(), inner::Token::I64(42));
1250
        assert_eq!(42.01f64.to_value().to_token(), inner::Token::F64(42.01));
1251
        assert_eq!(true.to_value().to_token(), inner::Token::Bool(true));
1252
        assert_eq!('a'.to_value().to_token(), inner::Token::Char('a'));
1253
        assert_eq!(
1254
            "a loong string".to_value().to_token(),
1255
            inner::Token::Str("a loong string".into())
1256
        );
1257
        assert_eq!(Some(true).to_value().to_token(), inner::Token::Bool(true));
1258
        assert_eq!(().to_value().to_token(), inner::Token::None);
1259
        assert_eq!(None::<bool>.to_value().to_token(), inner::Token::None);
1260
    }
1261
1262
    #[test]
1263
    fn test_to_number() {
1264
        for v in unsigned() {
1265
            assert!(v.to_u64().is_some());
1266
            assert!(v.to_i64().is_some());
1267
        }
1268
1269
        for v in signed() {
1270
            assert!(v.to_i64().is_some());
1271
        }
1272
1273
        for v in unsigned().chain(signed()).chain(float()) {
1274
            assert!(v.to_f64().is_some());
1275
        }
1276
1277
        for v in bool().chain(str()).chain(char()) {
1278
            assert!(v.to_u64().is_none());
1279
            assert!(v.to_i64().is_none());
1280
            assert!(v.to_f64().is_none());
1281
        }
1282
    }
1283
1284
    #[test]
1285
    fn test_to_float() {
1286
        // Only integers from i32::MIN..=u32::MAX can be converted into floats
1287
        assert!(Value::from(i32::MIN).to_f64().is_some());
1288
        assert!(Value::from(u32::MAX).to_f64().is_some());
1289
1290
        assert!(Value::from((i32::MIN as i64) - 1).to_f64().is_none());
1291
        assert!(Value::from((u32::MAX as u64) + 1).to_f64().is_none());
1292
    }
1293
1294
    #[test]
1295
    fn test_to_cow_str() {
1296
        for v in str() {
1297
            assert!(v.to_borrowed_str().is_some());
1298
1299
            #[cfg(feature = "kv_std")]
1300
            assert!(v.to_cow_str().is_some());
1301
        }
1302
1303
        let short_lived = String::from("short lived");
1304
        let v = Value::from(&*short_lived);
1305
1306
        assert!(v.to_borrowed_str().is_some());
1307
1308
        #[cfg(feature = "kv_std")]
1309
        assert!(v.to_cow_str().is_some());
1310
1311
        for v in unsigned().chain(signed()).chain(float()).chain(bool()) {
1312
            assert!(v.to_borrowed_str().is_none());
1313
1314
            #[cfg(feature = "kv_std")]
1315
            assert!(v.to_cow_str().is_none());
1316
        }
1317
    }
1318
1319
    #[test]
1320
    fn test_to_bool() {
1321
        for v in bool() {
1322
            assert!(v.to_bool().is_some());
1323
        }
1324
1325
        for v in unsigned()
1326
            .chain(signed())
1327
            .chain(float())
1328
            .chain(str())
1329
            .chain(char())
1330
        {
1331
            assert!(v.to_bool().is_none());
1332
        }
1333
    }
1334
1335
    #[test]
1336
    fn test_to_char() {
1337
        for v in char() {
1338
            assert!(v.to_char().is_some());
1339
        }
1340
1341
        for v in unsigned()
1342
            .chain(signed())
1343
            .chain(float())
1344
            .chain(str())
1345
            .chain(bool())
1346
        {
1347
            assert!(v.to_char().is_none());
1348
        }
1349
    }
1350
1351
    #[test]
1352
    fn test_visit_integer() {
1353
        struct Extract(Option<u64>);
1354
1355
        impl<'v> VisitValue<'v> for Extract {
1356
            fn visit_any(&mut self, value: Value) -> Result<(), Error> {
1357
                unimplemented!("unexpected value: {value:?}")
1358
            }
1359
1360
            fn visit_u64(&mut self, value: u64) -> Result<(), Error> {
1361
                self.0 = Some(value);
1362
1363
                Ok(())
1364
            }
1365
        }
1366
1367
        let mut extract = Extract(None);
1368
        Value::from(42u64).visit(&mut extract).unwrap();
1369
1370
        assert_eq!(Some(42), extract.0);
1371
    }
1372
1373
    #[test]
1374
    fn test_visit_borrowed_str() {
1375
        struct Extract<'v>(Option<&'v str>);
1376
1377
        impl<'v> VisitValue<'v> for Extract<'v> {
1378
            fn visit_any(&mut self, value: Value) -> Result<(), Error> {
1379
                unimplemented!("unexpected value: {value:?}")
1380
            }
1381
1382
            fn visit_borrowed_str(&mut self, value: &'v str) -> Result<(), Error> {
1383
                self.0 = Some(value);
1384
1385
                Ok(())
1386
            }
1387
        }
1388
1389
        let mut extract = Extract(None);
1390
1391
        let short_lived = String::from("A short-lived string");
1392
        Value::from(&*short_lived).visit(&mut extract).unwrap();
1393
1394
        assert_eq!(Some("A short-lived string"), extract.0);
1395
    }
1396
}