/// A type that can be converted into a [`Value`](struct.Value.html). pubtrait ToValue { /// Perform the conversion. fn to_value(&self) -> Value;
}
impl<'a, T> ToValue for &'a T where
T: ToValue + ?Sized,
{ fn to_value(&self) -> Value {
(**self).to_value()
}
}
impl<'v> ToValue for Value<'v> { fn to_value(&self) -> Value {
Value {
inner: self.inner.clone(),
}
}
}
/// Get a value from a type implementing `std::fmt::Debug`. #[macro_export]
macro_rules! as_debug {
($capture:expr) => {
$crate::kv::Value::from_debug(&$capture)
};
}
/// Get a value from a type implementing `std::fmt::Display`. #[macro_export]
macro_rules! as_display {
($capture:expr) => {
$crate::kv::Value::from_display(&$capture)
};
}
/// Get a value from an error. #[cfg(feature = "kv_unstable_std")] #[macro_export]
macro_rules! as_error {
($capture:expr) => {
$crate::kv::Value::from_dyn_error(&$capture)
};
}
#[cfg(feature = "kv_unstable_serde")] /// Get a value from a type implementing `serde::Serialize`. #[macro_export]
macro_rules! as_serde {
($capture:expr) => {
$crate::kv::Value::from_serde(&$capture)
};
}
/// Get a value from a type implementing `sval::Value`. #[cfg(feature = "kv_unstable_sval")] #[macro_export]
macro_rules! as_sval {
($capture:expr) => {
$crate::kv::Value::from_sval(&$capture)
};
}
/// A value in a structured key-value pair. /// /// # Capturing values /// /// There are a few ways to capture a value: /// /// - Using the `Value::capture_*` methods. /// - Using the `Value::from_*` methods. /// - Using the `ToValue` trait. /// - Using the standard `From` trait. /// /// ## Using the `Value::capture_*` methods /// /// `Value` offers a few constructor methods that capture values of different kinds. /// These methods require a `T: 'static` to support downcasting. /// /// ``` /// use log::kv::Value; /// /// let value = Value::capture_debug(&42i32); /// /// assert_eq!(Some(42), value.to_i64()); /// ``` /// /// ## Using the `Value::from_*` methods /// /// `Value` offers a few constructor methods that capture values of different kinds. /// These methods don't require `T: 'static`, but can't support downcasting. /// /// ``` /// 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`. /// /// ``` /// # use log::kv::ToValue; /// let value = 42i32.to_value(); /// /// assert_eq!(Some(42), value.to_i64()); /// ``` /// /// ``` /// # use std::fmt::Debug; /// use log::kv::ToValue; /// /// let value = (&42i32 as &dyn Debug).to_value(); /// /// assert_eq!(None, 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()); /// ``` pubstruct Value<'v> {
inner: ValueBag<'v>,
}
impl<'v> Value<'v> { /// Get a value from a type implementing `ToValue`. pubfn from_any<T>(value: &'v T) -> Self where
T: ToValue,
{
value.to_value()
}
/// Get a value from a type implementing `std::fmt::Debug`. pubfn capture_debug<T>(value: &'v T) -> Self where
T: fmt::Debug + 'static,
{
Value {
inner: ValueBag::capture_debug(value),
}
}
/// Get a value from a type implementing `std::fmt::Display`. pubfn capture_display<T>(value: &'v T) -> Self where
T: fmt::Display + 'static,
{
Value {
inner: ValueBag::capture_display(value),
}
}
/// Get a value from an error. #[cfg(feature = "kv_unstable_std")] pubfn capture_error<T>(err: &'v T) -> Self where
T: std::error::Error + 'static,
{
Value {
inner: ValueBag::capture_error(err),
}
}
#[cfg(feature = "kv_unstable_serde")] /// Get a value from a type implementing `serde::Serialize`. pubfn capture_serde<T>(value: &'v T) -> Self where
T: self::serde::Serialize + 'static,
{
Value {
inner: ValueBag::capture_serde1(value),
}
}
/// Get a value from a type implementing `sval::Value`. #[cfg(feature = "kv_unstable_sval")] pubfn capture_sval<T>(value: &'v T) -> Self where
T: self::sval::Value + 'static,
{
Value {
inner: ValueBag::capture_sval2(value),
}
}
/// Get a value from a type implementing `std::fmt::Debug`. pubfn from_debug<T>(value: &'v T) -> Self where
T: fmt::Debug,
{
Value {
inner: ValueBag::from_debug(value),
}
}
/// Get a value from a type implementing `std::fmt::Display`. pubfn from_display<T>(value: &'v T) -> Self where
T: fmt::Display,
{
Value {
inner: ValueBag::from_display(value),
}
}
/// Get a value from a type implementing `serde::Serialize`. #[cfg(feature = "kv_unstable_serde")] pubfn from_serde<T>(value: &'v T) -> Self where
T: self::serde::Serialize,
{
Value {
inner: ValueBag::from_serde1(value),
}
}
/// Get a value from a type implementing `sval::Value`. #[cfg(feature = "kv_unstable_sval")] pubfn from_sval<T>(value: &'v T) -> Self where
T: self::sval::Value,
{
Value {
inner: ValueBag::from_sval2(value),
}
}
/// Get a value from a dynamic `std::fmt::Debug`. pubfn from_dyn_debug(value: &'v dyn fmt::Debug) -> Self {
Value {
inner: ValueBag::from_dyn_debug(value),
}
}
/// Get a value from a dynamic `std::fmt::Display`. pubfn from_dyn_display(value: &'v dyn fmt::Display) -> Self {
Value {
inner: ValueBag::from_dyn_display(value),
}
}
/// Get a value from a dynamic error. #[cfg(feature = "kv_unstable_std")] pubfn from_dyn_error(err: &'v (dyn std::error::Error + 'static)) -> Self {
Value {
inner: ValueBag::from_dyn_error(err),
}
}
/// Get a value from an internal primitive. fn from_value_bag<T>(value: T) -> Self where
T: Into<ValueBag<'v>>,
{
Value {
inner: value.into(),
}
}
/// Check whether this value can be downcast to `T`. pubfn is<T: 'static>(&self) -> bool { self.inner.is::<T>()
}
/// Try downcast this value to `T`. pubfn downcast_ref<T: 'static>(&self) -> Option<&T> { self.inner.downcast_ref::<T>()
}
/// Inspect this value using a simple visitor. pubfn visit(&self, visitor: impl Visit<'v>) -> Result<(), Error> { struct Visitor<V>(V);
impl<'v, V> value_bag::visit::Visit<'v> for Visitor<V> where
V: Visit<'v>,
{ fn visit_any(&mutself, value: ValueBag) -> Result<(), value_bag::Error> { self.0
.visit_any(Value { inner: value })
.map_err(Error::into_value)
}
impl<'v> From<&'v std::num::NonZeroU128> for Value<'v> { fn from(v: &'v std::num::NonZeroU128) -> Value<'v> { // SAFETY: `NonZeroU128` and `u128` have the same ABI
Value::from_value_bag(unsafe { std::mem::transmute::<&std::num::NonZeroU128, &u128>(v) })
}
}
impl<'v> From<&'v std::num::NonZeroI128> for Value<'v> { fn from(v: &'v std::num::NonZeroI128) -> Value<'v> { // SAFETY: `NonZeroI128` and `i128` have the same ABI
Value::from_value_bag(unsafe { std::mem::transmute::<&std::num::NonZeroI128, &i128>(v) })
}
}
impl ToValue for () { fn to_value(&self) -> Value {
Value::from_value_bag(())
}
}
impl<T> ToValue for Option<T> where
T: ToValue,
{ fn to_value(&self) -> Value { match *self {
Some(ref value) => value.to_value(),
None => Value::from_value_bag(()),
}
}
}
macro_rules! impl_to_value_primitive {
($($into_ty:ty,)*) => {
$( impl ToValue for $into_ty { fn to_value(&self) -> Value {
Value::from(*self)
}
}
impl_value_to_primitive![ #[doc = "Try convert this value into a `u64`."]
to_u64 -> u64, #[doc = "Try convert this value into a `i64`."]
to_i64 -> i64, #[doc = "Try convert this value into a `u128`."]
to_u128 -> u128, #[doc = "Try convert this value into a `i128`."]
to_i128 -> i128, #[doc = "Try convert this value into a `f64`."]
to_f64 -> f64, #[doc = "Try convert this value into a `char`."]
to_char -> char, #[doc = "Try convert this value into a `bool`."]
to_bool -> bool,
];
impl<'v> Value<'v> { /// Try convert this value into an error. #[cfg(feature = "kv_unstable_std")] pubfn to_borrowed_error(&self) -> Option<&(>dyn std::error::Error + 'static)> { self.inner.to_borrowed_error()
}
/// Try convert this value into a borrowed string. pubfn to_borrowed_str(&self) -> Option<&str> { self.inner.to_borrowed_str()
}
}
#[cfg(feature = "kv_unstable_std")] mod std_support { usesuper::*;
use std::borrow::Cow;
impl<T> ToValue forBox<T> where
T: ToValue + ?Sized,
{ fn to_value(&self) -> Value {
(**self).to_value()
}
}
impl ToValue for String { fn to_value(&self) -> Value {
Value::from(&**self)
}
}
impl<'v> ToValue for Cow<'v, str> { fn to_value(&self) -> Value {
Value::from(&**self)
}
}
impl<'v> Value<'v> { /// Try convert this value into a string. pubfn to_str(&self) -> Option<Cow<str>> { self.inner.to_str()
}
}
/// A visitor for a `Value`. pubtrait Visit<'v> { /// Visit a `Value`. /// /// This is the only required method on `Visit` and acts as a fallback for any /// more specific methods that aren't overridden. /// The `Value` may be formatted using its `fmt::Debug` or `fmt::Display` implementation, /// or serialized using its `sval::Value` or `serde::Serialize` implementation. fn visit_any(&mutself, value: Value) -> Result<(), Error>;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.