mirror of
				https://github.com/isar/rusqlite.git
				synced 2025-10-31 05:48:56 +08:00 
			
		
		
		
	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:
		| @@ -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: | ||||
| // | ||||
| // 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)] | ||||
| mod test { | ||||
|     use super::ToSql; | ||||
| @@ -217,6 +238,29 @@ mod test { | ||||
|         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")] | ||||
|     #[test] | ||||
|     fn test_i128() { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user