Skip to content

Commit

Permalink
Merge pull request #491 from Thomasdezeeuw/kv-value-nonzero
Browse files Browse the repository at this point in the history
Implement ToValue and From<$ty> for Value for NonZero
  • Loading branch information
KodrAus authored Mar 22, 2022
2 parents d672066 + 4f9a212 commit 59fa5dc
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/kv/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,24 @@ macro_rules! impl_to_value_primitive {
};
}

macro_rules! impl_to_value_nonzero_primitive {
($($into_ty:ident,)*) => {
$(
impl ToValue for std::num::$into_ty {
fn to_value(&self) -> Value {
Value::from(self.get())
}
}

impl<'v> From<std::num::$into_ty> for Value<'v> {
fn from(value: std::num::$into_ty) -> Self {
Value::from(value.get())
}
}
)*
};
}

macro_rules! impl_value_to_primitive {
($(#[doc = $doc:tt] $into_name:ident -> $into_ty:ty,)*) => {
impl<'v> Value<'v> {
Expand All @@ -500,6 +518,12 @@ impl_to_value_primitive![
usize, u8, u16, u32, u64, u128, isize, i8, i16, i32, i64, i128, f32, f64, char, bool,
];

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

impl_value_to_primitive![
#[doc = "Try convert this value into a `u64`."]
to_u64 -> u64,
Expand Down Expand Up @@ -720,6 +744,11 @@ pub(crate) mod tests {
Value::from(32u32),
Value::from(64u64),
Value::from(1usize),
Value::from(std::num::NonZeroU8::new(8).unwrap()),
Value::from(std::num::NonZeroU16::new(16).unwrap()),
Value::from(std::num::NonZeroU32::new(32).unwrap()),
Value::from(std::num::NonZeroU64::new(64).unwrap()),
Value::from(std::num::NonZeroUsize::new(1).unwrap()),
]
.into_iter()
}
Expand All @@ -731,6 +760,11 @@ pub(crate) mod tests {
Value::from(-32i32),
Value::from(-64i64),
Value::from(-1isize),
Value::from(std::num::NonZeroI8::new(-8).unwrap()),
Value::from(std::num::NonZeroI16::new(-16).unwrap()),
Value::from(std::num::NonZeroI32::new(-32).unwrap()),
Value::from(std::num::NonZeroI64::new(-64).unwrap()),
Value::from(std::num::NonZeroIsize::new(-1).unwrap()),
]
.into_iter()
}
Expand Down

0 comments on commit 59fa5dc

Please sign in to comment.