From e2478bfbf4e53ab1a9e900d2414ac983dca813e5 Mon Sep 17 00:00:00 2001 From: numzero Date: Fri, 11 Apr 2025 19:48:18 +0300 Subject: [PATCH] into() --- src/types.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/types.rs b/src/types.rs index 01437de..7de3470 100644 --- a/src/types.rs +++ b/src/types.rs @@ -8,6 +8,34 @@ pub enum ValueInner { Table(Table), } +macro_rules! impl_from { + ($as: ident, $from: ty) => { + impl From<$from> for ValueInner { + fn from(value: $from) -> Self { + Self::$as(value.into()) + } + } + }; + (try $as: ident, $from: ty) => { + impl TryFrom<$from> for ValueInner { + type Error = ::std::num::TryFromIntError; + fn try_from(value: $from) -> Result { + Ok(Self::$as(value.try_into()?)) + } + } + }; +} + +impl_from!(Boolean, bool); +impl_from!(try Number, isize); +impl_from!(try Number, usize); +impl_from!(try Number, i64); +impl_from!(try Number, u64); +impl_from!(Number, u32); +impl_from!(Number, i32); +impl_from!(String, String); +impl_from!(String, &str); + pub type Value = Option; #[derive(Debug, Clone, Default)]