From 2a04f06a3a373054c3c979be6ab8c61313c9950e Mon Sep 17 00:00:00 2001 From: zero-systems <60021149+zero-systems@users.noreply.github.com> Date: Wed, 1 Apr 2020 13:52:34 +1000 Subject: [PATCH] Relaxed Sized bound on Box/Rc/Arc/Cow for ToSql * Fix Box for unsized * refactoring * Add more tests * ?Sized for Cow, Rc, Arc too --- src/types/to_sql.rs | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/src/types/to_sql.rs b/src/types/to_sql.rs index b4a27b6..458d90d 100644 --- a/src/types/to_sql.rs +++ b/src/types/to_sql.rs @@ -87,37 +87,27 @@ pub trait ToSql { fn to_sql(&self) -> Result>; } -impl ToSql for Box { - fn to_sql(&self) -> Result> { - let derefed: &dyn ToSql = &**self; - derefed.to_sql() - } -} - -impl ToSql for Cow<'_, T> { +impl ToSql for Cow<'_, T> { fn to_sql(&self) -> Result> { self.as_ref().to_sql() } } -impl ToSql for Box { +impl ToSql for Box { fn to_sql(&self) -> Result> { - let derefed: &dyn ToSql = &**self; - derefed.to_sql() + self.as_ref().to_sql() } } -impl ToSql for std::rc::Rc { +impl ToSql for std::rc::Rc { fn to_sql(&self) -> Result> { - let derefed: &dyn ToSql = &**self; - derefed.to_sql() + self.as_ref().to_sql() } } -impl ToSql for std::sync::Arc { +impl ToSql for std::sync::Arc { fn to_sql(&self) -> Result> { - let derefed: &dyn ToSql = &**self; - derefed.to_sql() + self.as_ref().to_sql() } } @@ -239,13 +229,29 @@ mod test { } #[test] - fn test_box() { + fn test_box_dyn() { + let s: Box = Box::new("Hello world!"); + let r = ToSql::to_sql(&s); + + assert!(r.is_ok()); + } + + #[test] + fn test_box_deref() { let s: Box = "Hello world!".into(); let r = s.to_sql(); assert!(r.is_ok()); } + #[test] + fn test_box_direct() { + let s: Box = "Hello world!".into(); + let r = ToSql::to_sql(&s); + + assert!(r.is_ok()); + } + #[test] fn test_cells() { use std::{rc::Rc, sync::Arc};