From ac0aba2dda7f8f612b92b3dd9e4925fb9734f0c9 Mon Sep 17 00:00:00 2001 From: trivernis Date: Mon, 3 Oct 2022 14:46:47 +0200 Subject: [PATCH] Add ToString implementation for hashable types --- Cargo.toml | 2 +- src/value.rs | 39 +++++++++++++++++++++++++++++++++++++++ src/value_trait.rs | 17 +++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 1fabaa2..2a51562 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ members = [".", "derive"] [package] name = "rusty-value" -version = "0.1.0" +version = "0.2.0" edition = "2021" license = "Apache-2.0" repository = "https://github.com/Trivernis/rusty-value" diff --git a/src/value.rs b/src/value.rs index 97d88e2..43e33f6 100644 --- a/src/value.rs +++ b/src/value.rs @@ -83,3 +83,42 @@ pub enum HashablePrimitive { Char(char), Bool(bool), } + +impl ToString for HashablePrimitive { + fn to_string(&self) -> String { + match self { + HashablePrimitive::Integer(i) => i.to_string(), + HashablePrimitive::String(s) => s.to_owned(), + HashablePrimitive::Char(c) => c.to_string(), + HashablePrimitive::Bool(b) => b.to_string(), + } + } +} + +impl ToString for Integer { + fn to_string(&self) -> String { + match self { + Integer::USize(n) => n.to_string(), + Integer::ISize(n) => n.to_string(), + Integer::U8(n) => n.to_string(), + Integer::I8(n) => n.to_string(), + Integer::U16(n) => n.to_string(), + Integer::I16(n) => n.to_string(), + Integer::U32(n) => n.to_string(), + Integer::I32(n) => n.to_string(), + Integer::U64(n) => n.to_string(), + Integer::I64(n) => n.to_string(), + Integer::U128(n) => n.to_string(), + Integer::I128(n) => n.to_string(), + } + } +} + +impl ToString for Float { + fn to_string(&self) -> String { + match self { + Float::F32(f) => f.to_string(), + Float::F64(f) => f.to_string(), + } + } +} diff --git a/src/value_trait.rs b/src/value_trait.rs index 725041c..98694a5 100644 --- a/src/value_trait.rs +++ b/src/value_trait.rs @@ -11,84 +11,98 @@ pub trait HashableRustyValue { } impl HashableRustyValue for usize { + #[inline] fn into_hashable_rusty_value(self) -> HashableValue { HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::USize(self))) } } impl HashableRustyValue for isize { + #[inline] fn into_hashable_rusty_value(self) -> HashableValue { HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::ISize(self))) } } impl HashableRustyValue for u8 { + #[inline] fn into_hashable_rusty_value(self) -> HashableValue { HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::U8(self))) } } impl HashableRustyValue for i8 { + #[inline] fn into_hashable_rusty_value(self) -> HashableValue { HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::I8(self))) } } impl HashableRustyValue for u16 { + #[inline] fn into_hashable_rusty_value(self) -> HashableValue { HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::U16(self))) } } impl HashableRustyValue for i16 { + #[inline] fn into_hashable_rusty_value(self) -> HashableValue { HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::I16(self))) } } impl HashableRustyValue for u32 { + #[inline] fn into_hashable_rusty_value(self) -> HashableValue { HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::U32(self))) } } impl HashableRustyValue for i32 { + #[inline] fn into_hashable_rusty_value(self) -> HashableValue { HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::I32(self))) } } impl HashableRustyValue for u64 { + #[inline] fn into_hashable_rusty_value(self) -> HashableValue { HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::U64(self))) } } impl HashableRustyValue for i64 { + #[inline] fn into_hashable_rusty_value(self) -> HashableValue { HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::I64(self))) } } impl HashableRustyValue for u128 { + #[inline] fn into_hashable_rusty_value(self) -> HashableValue { HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::U128(self))) } } impl HashableRustyValue for i128 { + #[inline] fn into_hashable_rusty_value(self) -> HashableValue { HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::I128(self))) } } impl HashableRustyValue for String { + #[inline] fn into_hashable_rusty_value(self) -> HashableValue { HashableValue::Primitive(HashablePrimitive::String(self)) } } impl HashableRustyValue for bool { + #[inline] fn into_hashable_rusty_value(self) -> HashableValue { HashableValue::Primitive(HashablePrimitive::Bool(self)) } @@ -111,18 +125,21 @@ impl RustyValue for HashableValue { } impl RustyValue for H { + #[inline] fn into_rusty_value(self) -> Value { self.into_hashable_rusty_value().into_rusty_value() } } impl RustyValue for f32 { + #[inline] fn into_rusty_value(self) -> Value { Value::Primitive(Primitive::Float(Float::F32(self))) } } impl RustyValue for f64 { + #[inline] fn into_rusty_value(self) -> Value { Value::Primitive(Primitive::Float(Float::F64(self))) }