diff --git a/core/Cargo.toml b/core/Cargo.toml index b0977e8..95d6ae3 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -11,7 +11,7 @@ serde = ["value-bag/serde", "dep:serde"] [dependencies.value-bag] version = "1" -features = ["seq"] +features = ["inline-i128", "seq"] [dependencies.sval] version = "2" diff --git a/core/src/str.rs b/core/src/str.rs index 3a12572..f4d63b8 100644 --- a/core/src/str.rs +++ b/core/src/str.rs @@ -182,7 +182,7 @@ impl<'a> From<&'a str> for Str<'a> { impl<'k> ToValue for Str<'k> { fn to_value(&self) -> Value { - Value::from(self.get()) + self.get().to_value() } } diff --git a/core/src/value.rs b/core/src/value.rs index 1bb3bed..d23cbec 100644 --- a/core/src/value.rs +++ b/core/src/value.rs @@ -100,21 +100,9 @@ impl<'v> Value<'v> { self.0.to_borrowed_str() } - pub fn to_f64(&self) -> Option { - self.0.to_f64() - } - pub fn as_f64(&self) -> f64 { self.0.as_f64() } - - pub fn to_usize(&self) -> Option { - self.0.to_u64()?.try_into().ok() - } - - pub fn to_i64(&self) -> Option { - self.0.to_i64() - } } impl<'v> fmt::Debug for Value<'v> { @@ -187,101 +175,81 @@ impl<'v> FromValue<'v> for Value<'v> { } } -impl ToValue for str { - fn to_value(&self) -> Value { - Value::from(self) - } -} - -impl<'v> From<&'v str> for Value<'v> { - fn from(value: &'v str) -> Self { - Value(value.into()) - } -} - -impl<'v> FromValue<'v> for &'v str { - fn from_value(value: Value<'v>) -> Option { - value.to_borrowed_str() - } -} - -impl ToValue for usize { - fn to_value(&self) -> Value { - Value::from(*self) - } -} - -impl<'v> From for Value<'v> { - fn from(value: usize) -> Self { - Value(value.into()) - } -} +macro_rules! impl_primitive { + ($($t:ty,)*) => { + $( + impl ToValue for $t { + fn to_value(&self) -> Value { + Value(self.into()) + } + } -impl<'v> FromValue<'v> for usize { - fn from_value(value: Value<'v>) -> Option { - value.to_usize() - } -} + impl ToValue for [$t; N] { + fn to_value(&self) -> Value { + Value(self.into()) + } + } -impl ToValue for f64 { - fn to_value(&self) -> Value { - Value::from(*self) - } -} + impl<'v> FromValue<'v> for $t { + fn from_value(value: Value<'v>) -> Option { + value.0.try_into().ok() + } + } -impl<'v> From for Value<'v> { - fn from(value: f64) -> Self { - Value(value.into()) - } -} + impl<'v> From<$t> for Value<'v> { + fn from(value: $t) -> Self { + Value(value.into()) + } + } -impl<'v> FromValue<'v> for f64 { - fn from_value(value: Value<'v>) -> Option { - value.to_f64() - } + impl<'v> From> for Value<'v> { + fn from(value: Option<$t>) -> Self { + Value(value_bag::ValueBag::from_option(value)) + } + } + )* + }; } -impl ToValue for [f64; N] { - fn to_value(&self) -> Value { - Value::from(self) - } -} +macro_rules! impl_ref { + ($(& $l:lifetime $t:ty,)*) => { + $( + impl ToValue for $t { + fn to_value(&self) -> Value { + Value(self.into()) + } + } -impl<'v, const N: usize> From<&'v [f64; N]> for Value<'v> { - fn from(value: &'v [f64; N]) -> Self { - Value(value.into()) - } -} + impl<$l, const N: usize> ToValue for [&$l $t; N] { + fn to_value(&self) -> Value { + Value(self.into()) + } + } -impl ToValue for i64 { - fn to_value(&self) -> Value { - Value::from(*self) - } -} + impl<$l> FromValue<$l> for &$l $t { + fn from_value(value: Value<$l>) -> Option { + value.0.try_into().ok() + } + } -impl<'v> From for Value<'v> { - fn from(value: i64) -> Self { - Value(value.into()) - } -} + impl<$l> From<&$l $t> for Value<$l> { + fn from(value: &$l $t) -> Self { + Value(value.into()) + } + } -impl<'v> FromValue<'v> for i64 { - fn from_value(value: Value<'v>) -> Option { - value.to_i64() - } + impl<$l> From> for Value<$l> { + fn from(value: Option<&$l $t>) -> Self { + Value(value_bag::ValueBag::from_option(value)) + } + } + )* + }; } -impl ToValue for [i64; N] { - fn to_value(&self) -> Value { - Value::from(self) - } -} +impl_primitive!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f64, bool,); -impl<'v, const N: usize> From<&'v [i64; N]> for Value<'v> { - fn from(value: &'v [i64; N]) -> Self { - Value(value.into()) - } -} +impl_ref!(&'v str,); impl ToValue for dyn fmt::Debug { fn to_value(&self) -> Value { @@ -289,24 +257,12 @@ impl ToValue for dyn fmt::Debug { } } -impl<'v> From<&'v dyn fmt::Debug> for Value<'v> { - fn from(value: &'v dyn fmt::Debug) -> Self { - Value(value_bag::ValueBag::from_dyn_debug(value)) - } -} - impl ToValue for dyn fmt::Display { fn to_value(&self) -> Value { Value(value_bag::ValueBag::from_dyn_display(self)) } } -impl<'v> From<&'v dyn fmt::Display> for Value<'v> { - fn from(value: &'v dyn fmt::Display) -> Self { - Value(value_bag::ValueBag::from_dyn_display(value)) - } -} - #[cfg(feature = "std")] impl ToValue for (dyn std::error::Error + 'static) { fn to_value(&self) -> Value { @@ -314,13 +270,6 @@ impl ToValue for (dyn std::error::Error + 'static) { } } -#[cfg(feature = "std")] -impl<'v> From<&'v (dyn std::error::Error + 'static)> for Value<'v> { - fn from(value: &'v (dyn std::error::Error + 'static)) -> Self { - Value(value_bag::ValueBag::from_dyn_error(value)) - } -} - #[cfg(feature = "alloc")] mod alloc_support { use super::*; diff --git a/src/macro_hooks.rs b/src/macro_hooks.rs index 8043504..3aa2ba5 100644 --- a/src/macro_hooks.rs +++ b/src/macro_hooks.rs @@ -90,7 +90,7 @@ impl CaptureStr for CaptureValue {} impl Capture for str { fn capture(&self) -> Option { - Some(Value::from(self)) + Some(self.to_value()) } } @@ -141,7 +141,7 @@ where impl Capture for dyn fmt::Display { fn capture(&self) -> Option { - Some(Value::from(self)) + Some(self.to_value()) } } @@ -165,7 +165,7 @@ where impl Capture for dyn fmt::Debug { fn capture(&self) -> Option { - Some(Value::from(self)) + Some(self.to_value()) } } @@ -249,7 +249,7 @@ where #[cfg(feature = "std")] impl<'a> Capture for (dyn Error + 'static) { fn capture(&self) -> Option { - Some(Value::from(self)) + Some(self.to_value()) } }