Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ kv_unstable_serde = ["kv_unstable_std", "value-bag/serde", "serde"]
cfg-if = "1.0"
serde = { version = "1.0", optional = true, default-features = false }
sval = { version = "=1.0.0-alpha.5", optional = true, default-features = false }
value-bag = { version = "=1.0.0-alpha.8", optional = true, default-features = false }
value-bag = { version = "=1.0.0-alpha.9", optional = true, default-features = false }

[dev-dependencies]
rustversion = "1.0"
serde = { version = "1.0", features = ["derive"] }
serde_test = "1.0"
sval = { version = "=1.0.0-alpha.5", features = ["derive"] }
value-bag = { version = "=1.0.0-alpha.8", features = ["test"] }
value-bag = { version = "=1.0.0-alpha.9", features = ["test"] }
62 changes: 55 additions & 7 deletions src/kv/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,12 +441,62 @@ impl ToValue for str {
}
}

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

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

impl ToValue for std::num::NonZeroU128 {
fn to_value(&self) -> Value {
Value::from(self)
}
}

impl ToValue for std::num::NonZeroI128 {
fn to_value(&self) -> Value {
Value::from(self)
}
}

impl<'v> From<&'v str> for Value<'v> {
fn from(value: &'v str) -> Self {
Value::from_value_bag(value)
}
}

impl<'v> From<&'v u128> for Value<'v> {
fn from(value: &'v u128) -> Self {
Value::from_value_bag(value)
}
}

impl<'v> From<&'v i128> for Value<'v> {
fn from(value: &'v i128) -> Self {
Value::from_value_bag(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(())
Expand Down Expand Up @@ -514,14 +564,12 @@ macro_rules! impl_value_to_primitive {
}
}

impl_to_value_primitive![
usize, u8, u16, u32, u64, u128, isize, i8, i16, i32, i64, i128, f32, f64, char, bool,
];
impl_to_value_primitive![usize, u8, u16, u32, u64, isize, i8, i16, i32, i64, f32, f64, char, bool,];
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: final ,.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that macro implementation is a little iffy and it actually requires that trailing , 😄


#[rustfmt::skip]
impl_to_value_nonzero_primitive![
NonZeroUsize, NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128,
NonZeroIsize, NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128,
NonZeroUsize, NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64,
NonZeroIsize, NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64,
];

impl_value_to_primitive![
Expand Down Expand Up @@ -617,12 +665,12 @@ pub trait Visit<'v> {

/// Visit a big unsigned integer.
fn visit_u128(&mut self, value: u128) -> Result<(), Error> {
self.visit_any(value.into())
self.visit_any((&value).into())
}

/// Visit a big signed integer.
fn visit_i128(&mut self, value: i128) -> Result<(), Error> {
self.visit_any(value.into())
self.visit_any((&value).into())
}

/// Visit a floating point.
Expand Down
22 changes: 22 additions & 0 deletions tests/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,28 @@ fn kv_string_keys() {
all_log_macros!(target: "my_target", "also dogs" = "Fílos", "key/that-can't/be/an/ident" = "hi"; "hello {world}", world = "world");
}

#[test]
#[cfg(feature = "kv_unstable")]
fn kv_common_value_types() {
all_log_macros!(
u8 = 42u8,
u16 = 42u16,
u32 = 42u32,
u64 = 42u64,
u128 = 42u128,
i8 = -42i8,
i16 = -42i16,
i32 = -42i32,
i64 = -42i64,
i128 = -42i128,
f32 = 4.2f32,
f64 = -4.2f64,
bool = true,
str = "string";
"hello world"
);
}

/// Some and None (from Option) are used in the macros.
#[derive(Debug)]
enum Type {
Expand Down