Impls of ToSql for different generic types (Box, Cow, Rc, Arc). (#660)

* Impl ToSql for Box<str>

* Add generic impls for Cow, Box, Rc, Arc. Remove impl for Box<str>

* Remove impl of ToSql for Cow<'_, str>

* Add missing as_ref

Co-authored-by: Øsystems <>
This commit is contained in:
zero-systems 2020-03-29 02:53:03 +10:00 committed by GitHub
parent 25bd5fc914
commit 761df93a83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -94,6 +94,33 @@ impl ToSql for Box<dyn ToSql> {
} }
} }
impl<T: ToSql + Clone> ToSql for Cow<'_, T> {
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
self.as_ref().to_sql()
}
}
impl<T: ToSql> ToSql for Box<T> {
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
let derefed: &dyn ToSql = &**self;
derefed.to_sql()
}
}
impl<T: ToSql> ToSql for std::rc::Rc<T> {
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
let derefed: &dyn ToSql = &**self;
derefed.to_sql()
}
}
impl<T: ToSql> ToSql for std::sync::Arc<T> {
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
let derefed: &dyn ToSql = &**self;
derefed.to_sql()
}
}
// We should be able to use a generic impl like this: // We should be able to use a generic impl like this:
// //
// impl<T: Copy> ToSql for T where T: Into<Value> { // impl<T: Copy> ToSql for T where T: Into<Value> {
@ -182,12 +209,6 @@ impl<T: ToSql> ToSql for Option<T> {
} }
} }
impl ToSql for Cow<'_, str> {
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
Ok(ToSqlOutput::from(self.as_ref()))
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::ToSql; use super::ToSql;
@ -217,6 +238,29 @@ mod test {
assert!(r.is_ok()); assert!(r.is_ok());
} }
#[test]
fn test_box() {
let s: Box<str> = "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<str> = "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")] #[cfg(feature = "i128_blob")]
#[test] #[test]
fn test_i128() { fn test_i128() {