Skip to content
This repository has been archived by the owner on Jun 8, 2024. It is now read-only.

Commit

Permalink
tidy up Value conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Mar 26, 2024
1 parent b766632 commit 0a45397
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 119 deletions.
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion core/src/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down
175 changes: 62 additions & 113 deletions core/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,9 @@ impl<'v> Value<'v> {
self.0.to_borrowed_str()
}

pub fn to_f64(&self) -> Option<f64> {
self.0.to_f64()
}

pub fn as_f64(&self) -> f64 {
self.0.as_f64()
}

pub fn to_usize(&self) -> Option<usize> {
self.0.to_u64()?.try_into().ok()
}

pub fn to_i64(&self) -> Option<i64> {
self.0.to_i64()
}
}

impl<'v> fmt::Debug for Value<'v> {
Expand Down Expand Up @@ -187,140 +175,101 @@ 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<Self> {
value.to_borrowed_str()
}
}

impl ToValue for usize {
fn to_value(&self) -> Value {
Value::from(*self)
}
}

impl<'v> From<usize> 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<Self> {
value.to_usize()
}
}
impl<const N: usize> 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<Self> {
value.0.try_into().ok()
}
}

impl<'v> From<f64> 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<Self> {
value.to_f64()
}
impl<'v> From<Option<$t>> for Value<'v> {
fn from(value: Option<$t>) -> Self {
Value(value_bag::ValueBag::from_option(value))
}
}
)*
};
}

impl<const N: usize> 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<Self> {
value.0.try_into().ok()
}
}

impl<'v> From<i64> 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<Self> {
value.to_i64()
}
impl<$l> From<Option<&$l $t>> for Value<$l> {
fn from(value: Option<&$l $t>) -> Self {
Value(value_bag::ValueBag::from_option(value))
}
}
)*
};
}

impl<const N: usize> 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 {
Value(value_bag::ValueBag::from_dyn_debug(self))
}
}

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 {
Value(value_bag::ValueBag::from_dyn_error(self))
}
}

#[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::*;
Expand Down
8 changes: 4 additions & 4 deletions src/macro_hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl CaptureStr for CaptureValue {}

impl<T: CaptureStr + ?Sized> Capture<T> for str {
fn capture(&self) -> Option<Value> {
Some(Value::from(self))
Some(self.to_value())
}
}

Expand Down Expand Up @@ -141,7 +141,7 @@ where

impl Capture<CaptureDisplay> for dyn fmt::Display {
fn capture(&self) -> Option<Value> {
Some(Value::from(self))
Some(self.to_value())
}
}

Expand All @@ -165,7 +165,7 @@ where

impl Capture<CaptureDebug> for dyn fmt::Debug {
fn capture(&self) -> Option<Value> {
Some(Value::from(self))
Some(self.to_value())
}
}

Expand Down Expand Up @@ -249,7 +249,7 @@ where
#[cfg(feature = "std")]
impl<'a> Capture<CaptureError> for (dyn Error + 'static) {
fn capture(&self) -> Option<Value> {
Some(Value::from(self))
Some(self.to_value())
}
}

Expand Down

0 comments on commit 0a45397

Please sign in to comment.