From 761df93a83622ce3e05ad9330257ec5459fd8d74 Mon Sep 17 00:00:00 2001 From: zero-systems <60021149+zero-systems@users.noreply.github.com> Date: Sun, 29 Mar 2020 02:53:03 +1000 Subject: [PATCH] Impls of ToSql for different generic types (Box, Cow, Rc, Arc). (#660) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Impl ToSql for Box * Add generic impls for Cow, Box, Rc, Arc. Remove impl for Box * Remove impl of ToSql for Cow<'_, str> * Add missing as_ref Co-authored-by: Øsystems <> --- src/types/to_sql.rs | 56 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 6 deletions(-) diff --git a/src/types/to_sql.rs b/src/types/to_sql.rs index 3a7481a..b4a27b6 100644 --- a/src/types/to_sql.rs +++ b/src/types/to_sql.rs @@ -94,6 +94,33 @@ impl ToSql for Box { } } +impl ToSql for Cow<'_, T> { + fn to_sql(&self) -> Result> { + self.as_ref().to_sql() + } +} + +impl ToSql for Box { + fn to_sql(&self) -> Result> { + let derefed: &dyn ToSql = &**self; + derefed.to_sql() + } +} + +impl ToSql for std::rc::Rc { + fn to_sql(&self) -> Result> { + let derefed: &dyn ToSql = &**self; + derefed.to_sql() + } +} + +impl ToSql for std::sync::Arc { + fn to_sql(&self) -> Result> { + let derefed: &dyn ToSql = &**self; + derefed.to_sql() + } +} + // We should be able to use a generic impl like this: // // impl ToSql for T where T: Into { @@ -182,12 +209,6 @@ impl ToSql for Option { } } -impl ToSql for Cow<'_, str> { - fn to_sql(&self) -> Result> { - Ok(ToSqlOutput::from(self.as_ref())) - } -} - #[cfg(test)] mod test { use super::ToSql; @@ -217,6 +238,29 @@ mod test { assert!(r.is_ok()); } + #[test] + fn test_box() { + let s: Box = "Hello world!".into(); + let r = s.to_sql(); + + assert!(r.is_ok()); + } + + #[test] + fn test_cells() { + use std::{rc::Rc, sync::Arc}; + + let source_str: Box = "Hello world!".into(); + + let s: Rc<_> = Rc::new(source_str.clone()); + let r = s.to_sql(); + assert!(r.is_ok()); + + let s: Arc<_> = Arc::new(source_str); + let r = s.to_sql(); + assert!(r.is_ok()); + } + #[cfg(feature = "i128_blob")] #[test] fn test_i128() {