Skip to content

Commit

Permalink
Merge pull request #242 from liangxianzhe/feat_into_datavalue
Browse files Browse the repository at this point in the history
Add from bytes and vector for DataValue.
  • Loading branch information
zh217 committed Mar 4, 2024
2 parents 7adfc6c + 160068c commit 8b1b60c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
2 changes: 2 additions & 0 deletions cozo-core/src/data/tests/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![
Expand Down
27 changes: 24 additions & 3 deletions cozo-core/src/data/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,12 +438,24 @@ impl Hash for Vector {
}
}

impl From<bool> for DataValue {
fn from(value: bool) -> Self {
DataValue::Bool(value)
}
}

impl From<i64> for DataValue {
fn from(v: i64) -> Self {
DataValue::Num(Num::Int(v))
}
}

impl From<i32> for DataValue {
fn from(v: i32) -> Self {
DataValue::Num(Num::Int(v as i64))
}
}

impl From<f64> for DataValue {
fn from(v: f64) -> Self {
DataValue::Num(Num::Float(v))
Expand All @@ -462,9 +474,18 @@ impl From<String> for DataValue {
}
}

impl From<bool> for DataValue {
fn from(value: bool) -> Self {
DataValue::Bool(value)
impl From<Vec<u8>> for DataValue {
fn from(v: Vec<u8>) -> Self {
DataValue::Bytes(v)
}
}

impl<T: Into<DataValue>> From<Vec<T>> for DataValue {
fn from(v: Vec<T>) -> Self
where
T: Into<DataValue>,
{
DataValue::List(v.into_iter().map(Into::into).collect())
}
}

Expand Down

0 comments on commit 8b1b60c

Please sign in to comment.