Add ToString implementation for hashable types

main
trivernis 2 years ago
parent a30cee178f
commit ac0aba2dda
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -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"

@ -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(),
}
}
}

@ -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<H: HashableRustyValue> 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)))
}

Loading…
Cancel
Save