From 79a52c38c26eeba89c0df4c4021a32e6fd596698 Mon Sep 17 00:00:00 2001 From: Linken Dinh Date: Tue, 13 Dec 2022 13:24:01 +0100 Subject: [PATCH 1/7] add Cow conversion to Value --- src/value.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/value.rs b/src/value.rs index 114c39b5a..0de3e599a 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,35 @@ where } } +impl<'a> From> for Value { + fn from(x: Cow<'a, str>) -> Value { + // will stack overflow if not cloning the ref + #[allow(clippy::clone_double_ref)] + x.as_ref().clone().into() + } +} + +impl<'a> ValueType for Cow<'a, str> { + fn try_from(v: Value) -> Result { + match v { + Value::String(Some(x)) => Ok(x.to_string().into()), + _ => Err(ValueTypeErr), + } + } + + fn type_name() -> String { + "Cow".into() + } + + fn array_type() -> ArrayType { + ArrayType::String + } + + fn column_type() -> ColumnType { + ColumnType::Text + } +} + type_to_box_value!(Vec, Bytes, Binary(BlobSize::Blob(None))); type_to_box_value!(String, String, String(None)); @@ -1458,6 +1489,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(); From 749cbcbe7410125ee1daf4c5afffd2a00fccb327 Mon Sep 17 00:00:00 2001 From: Linken Dinh Date: Tue, 13 Dec 2022 13:58:47 +0100 Subject: [PATCH 2/7] clone_double_ref fix --- src/value.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/value.rs b/src/value.rs index 0de3e599a..e92fdddc2 100644 --- a/src/value.rs +++ b/src/value.rs @@ -439,9 +439,7 @@ where impl<'a> From> for Value { fn from(x: Cow<'a, str>) -> Value { - // will stack overflow if not cloning the ref - #[allow(clippy::clone_double_ref)] - x.as_ref().clone().into() + x.into_owned().into() } } From 3b6cb10c8435ab569e214b7a1ae890c39c6744c2 Mon Sep 17 00:00:00 2001 From: Linken Dinh Date: Tue, 13 Dec 2022 14:09:18 +0100 Subject: [PATCH 3/7] remove needless clone --- src/value.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/value.rs b/src/value.rs index e92fdddc2..4d656057f 100644 --- a/src/value.rs +++ b/src/value.rs @@ -446,7 +446,7 @@ impl<'a> From> for Value { impl<'a> ValueType for Cow<'a, str> { fn try_from(v: Value) -> Result { match v { - Value::String(Some(x)) => Ok(x.to_string().into()), + Value::String(Some(x)) => Ok(x.into()), _ => Err(ValueTypeErr), } } From 11c3d524e3d0e242a08a894f18ba1c0f53beab11 Mon Sep 17 00:00:00 2001 From: Linken Dinh Date: Tue, 13 Dec 2022 14:28:35 +0100 Subject: [PATCH 4/7] Update value.rs deal with box --- src/value.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/value.rs b/src/value.rs index 4d656057f..51344374e 100644 --- a/src/value.rs +++ b/src/value.rs @@ -446,7 +446,7 @@ impl<'a> From> for Value { impl<'a> ValueType for Cow<'a, str> { fn try_from(v: Value) -> Result { match v { - Value::String(Some(x)) => Ok(x.into()), + Value::String(Some(x)) => Ok(*x.into()), _ => Err(ValueTypeErr), } } From ba890a2f28572b3a7567b3e049e5a5ce3a1c1719 Mon Sep 17 00:00:00 2001 From: Ivan Krivosheev Date: Tue, 13 Dec 2022 16:33:33 +0300 Subject: [PATCH 5/7] Update src/value.rs --- src/value.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/value.rs b/src/value.rs index 51344374e..3f14bd893 100644 --- a/src/value.rs +++ b/src/value.rs @@ -446,7 +446,7 @@ impl<'a> From> for Value { impl<'a> ValueType for Cow<'a, str> { fn try_from(v: Value) -> Result { match v { - Value::String(Some(x)) => Ok(*x.into()), + Value::String(Some(x)) => Ok((*x).into()), _ => Err(ValueTypeErr), } } From 7c3a7460f0b8daacfc1056e248cd3cae839a0baa Mon Sep 17 00:00:00 2001 From: Linken Dinh Date: Wed, 14 Dec 2022 07:58:51 +0100 Subject: [PATCH 6/7] adjust ColumnType --- src/value.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/value.rs b/src/value.rs index 3f14bd893..99125a733 100644 --- a/src/value.rs +++ b/src/value.rs @@ -460,7 +460,7 @@ impl<'a> ValueType for Cow<'a, str> { } fn column_type() -> ColumnType { - ColumnType::Text + ColumnType::String(None) } } From 2e5e3350d68c909a1e8aa590779b4358f398cc20 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Thu, 15 Dec 2022 11:58:51 +0800 Subject: [PATCH 7/7] Lifetime elided --- src/value.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/value.rs b/src/value.rs index 99125a733..1fd628f98 100644 --- a/src/value.rs +++ b/src/value.rs @@ -437,13 +437,13 @@ where } } -impl<'a> From> for Value { - fn from(x: Cow<'a, str>) -> Value { +impl From> for Value { + fn from(x: Cow<'_, str>) -> Value { x.into_owned().into() } } -impl<'a> ValueType for Cow<'a, str> { +impl ValueType for Cow<'_, str> { fn try_from(v: Value) -> Result { match v { Value::String(Some(x)) => Ok((*x).into()),