Skip to content

Commit 4f9a212

Browse files
committed
Implement ToValue and From<$ty> for Value for NonZero
It can already by be done by calling `.get()`, but this makes it a bit easier.
1 parent d672066 commit 4f9a212

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/kv/value.rs

+34
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,24 @@ macro_rules! impl_to_value_primitive {
483483
};
484484
}
485485

486+
macro_rules! impl_to_value_nonzero_primitive {
487+
($($into_ty:ident,)*) => {
488+
$(
489+
impl ToValue for std::num::$into_ty {
490+
fn to_value(&self) -> Value {
491+
Value::from(self.get())
492+
}
493+
}
494+
495+
impl<'v> From<std::num::$into_ty> for Value<'v> {
496+
fn from(value: std::num::$into_ty) -> Self {
497+
Value::from(value.get())
498+
}
499+
}
500+
)*
501+
};
502+
}
503+
486504
macro_rules! impl_value_to_primitive {
487505
($(#[doc = $doc:tt] $into_name:ident -> $into_ty:ty,)*) => {
488506
impl<'v> Value<'v> {
@@ -500,6 +518,12 @@ impl_to_value_primitive![
500518
usize, u8, u16, u32, u64, u128, isize, i8, i16, i32, i64, i128, f32, f64, char, bool,
501519
];
502520

521+
#[rustfmt::skip]
522+
impl_to_value_nonzero_primitive![
523+
NonZeroUsize, NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128,
524+
NonZeroIsize, NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128,
525+
];
526+
503527
impl_value_to_primitive![
504528
#[doc = "Try convert this value into a `u64`."]
505529
to_u64 -> u64,
@@ -720,6 +744,11 @@ pub(crate) mod tests {
720744
Value::from(32u32),
721745
Value::from(64u64),
722746
Value::from(1usize),
747+
Value::from(std::num::NonZeroU8::new(8).unwrap()),
748+
Value::from(std::num::NonZeroU16::new(16).unwrap()),
749+
Value::from(std::num::NonZeroU32::new(32).unwrap()),
750+
Value::from(std::num::NonZeroU64::new(64).unwrap()),
751+
Value::from(std::num::NonZeroUsize::new(1).unwrap()),
723752
]
724753
.into_iter()
725754
}
@@ -731,6 +760,11 @@ pub(crate) mod tests {
731760
Value::from(-32i32),
732761
Value::from(-64i64),
733762
Value::from(-1isize),
763+
Value::from(std::num::NonZeroI8::new(-8).unwrap()),
764+
Value::from(std::num::NonZeroI16::new(-16).unwrap()),
765+
Value::from(std::num::NonZeroI32::new(-32).unwrap()),
766+
Value::from(std::num::NonZeroI64::new(-64).unwrap()),
767+
Value::from(std::num::NonZeroIsize::new(-1).unwrap()),
734768
]
735769
.into_iter()
736770
}

0 commit comments

Comments
 (0)