Relaxed Sized bound on Box/Rc/Arc/Cow for ToSql

* Fix Box<T> for unsized

* refactoring

* Add more tests

* ?Sized for Cow, Rc, Arc too
This commit is contained in:
zero-systems 2020-04-01 13:52:34 +10:00 committed by GitHub
parent 761df93a83
commit 2a04f06a3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -87,37 +87,27 @@ pub trait ToSql {
fn to_sql(&self) -> Result<ToSqlOutput<'_>>;
}
impl ToSql for Box<dyn ToSql> {
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
let derefed: &dyn ToSql = &**self;
derefed.to_sql()
}
}
impl<T: ToSql + Clone> ToSql for Cow<'_, T> {
impl<T: ToSql + Clone + ?Sized> ToSql for Cow<'_, T> {
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
self.as_ref().to_sql()
}
}
impl<T: ToSql> ToSql for Box<T> {
impl<T: ToSql + ?Sized> ToSql for Box<T> {
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
let derefed: &dyn ToSql = &**self;
derefed.to_sql()
self.as_ref().to_sql()
}
}
impl<T: ToSql> ToSql for std::rc::Rc<T> {
impl<T: ToSql + ?Sized> ToSql for std::rc::Rc<T> {
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
let derefed: &dyn ToSql = &**self;
derefed.to_sql()
self.as_ref().to_sql()
}
}
impl<T: ToSql> ToSql for std::sync::Arc<T> {
impl<T: ToSql + ?Sized> ToSql for std::sync::Arc<T> {
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
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<dyn ToSql> = Box::new("Hello world!");
let r = ToSql::to_sql(&s);
assert!(r.is_ok());
}
#[test]
fn test_box_deref() {
let s: Box<str> = "Hello world!".into();
let r = s.to_sql();
assert!(r.is_ok());
}
#[test]
fn test_box_direct() {
let s: Box<str> = "Hello world!".into();
let r = ToSql::to_sql(&s);
assert!(r.is_ok());
}
#[test]
fn test_cells() {
use std::{rc::Rc, sync::Arc};