diff --git a/src/value.rs b/src/value.rs index 114c39b5a..1fd628f98 100644 --- a/src/value.rs +++ b/src/value.rs @@ -1,5 +1,7 @@ //! Container for all SQL value types. +use std::borrow::Cow; + #[cfg(feature = "with-json")] use serde_json::Value as Json; #[cfg(feature = "with-json")] @@ -435,6 +437,33 @@ where } } +impl From> for Value { + fn from(x: Cow<'_, str>) -> Value { + x.into_owned().into() + } +} + +impl ValueType for Cow<'_, str> { + fn try_from(v: Value) -> Result { + match v { + Value::String(Some(x)) => Ok((*x).into()), + _ => Err(ValueTypeErr), + } + } + + fn type_name() -> String { + "Cow".into() + } + + fn array_type() -> ArrayType { + ArrayType::String + } + + fn column_type() -> ColumnType { + ColumnType::String(None) + } +} + type_to_box_value!(Vec, Bytes, Binary(BlobSize::Blob(None))); type_to_box_value!(String, String, String(None)); @@ -1458,6 +1487,15 @@ mod tests { test_none!(i64, BigInt); } + #[test] + fn test_cow_value() { + let val: Cow = "hello".into(); + let val2 = val.clone(); + let v: Value = val.into(); + let out: Cow = v.unwrap(); + assert_eq!(out, val2); + } + #[test] fn test_box_value() { let val: String = "hello".to_owned();