From 160068cfc2fe61a404f4ad5a77c4187292a89d70 Mon Sep 17 00:00:00 2001 From: Xianzhe Liang Date: Sun, 3 Mar 2024 17:55:42 -0800 Subject: [PATCH] Add from bytes and vector for DataValue. So it is easier to into DataValue. --- cozo-core/src/data/tests/values.rs | 2 ++ cozo-core/src/data/value.rs | 27 ++++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/cozo-core/src/data/tests/values.rs b/cozo-core/src/data/tests/values.rs index 1f565214..572cf386 100644 --- a/cozo-core/src/data/tests/values.rs +++ b/cozo-core/src/data/tests/values.rs @@ -46,6 +46,8 @@ fn display_datavalues() { println!("{}", DataValue::from(-1121212121.331212121)); println!("{}", DataValue::from(f64::NAN)); println!("{}", DataValue::from(f64::NEG_INFINITY)); + println!("{}", DataValue::from(vec![10, 20])); + println!("{}", DataValue::from(vec!["hello", "你好"])); println!( "{}", DataValue::List(vec![ diff --git a/cozo-core/src/data/value.rs b/cozo-core/src/data/value.rs index ad91dd99..abcb2ce4 100644 --- a/cozo-core/src/data/value.rs +++ b/cozo-core/src/data/value.rs @@ -438,12 +438,24 @@ impl Hash for Vector { } } +impl From for DataValue { + fn from(value: bool) -> Self { + DataValue::Bool(value) + } +} + impl From for DataValue { fn from(v: i64) -> Self { DataValue::Num(Num::Int(v)) } } +impl From for DataValue { + fn from(v: i32) -> Self { + DataValue::Num(Num::Int(v as i64)) + } +} + impl From for DataValue { fn from(v: f64) -> Self { DataValue::Num(Num::Float(v)) @@ -462,9 +474,18 @@ impl From for DataValue { } } -impl From for DataValue { - fn from(value: bool) -> Self { - DataValue::Bool(value) +impl From> for DataValue { + fn from(v: Vec) -> Self { + DataValue::Bytes(v) + } +} + +impl> From> for DataValue { + fn from(v: Vec) -> Self + where + T: Into, + { + DataValue::List(v.into_iter().map(Into::into).collect()) } }