From 4b545ca4666b475fcdd705104ae27c1f223ffd37 Mon Sep 17 00:00:00 2001 From: Jonathan Reem Date: Fri, 31 Jul 2015 18:25:41 -0700 Subject: [PATCH 1/2] Implement ToSql for &'a T where T: ToSql This allows more flexible use of ToSql in generic contexts by allowing references to be used for encoding. --- src/types/mod.rs | 19 +++++++++++++++++++ tests/types/mod.rs | 8 ++++++++ 2 files changed, 27 insertions(+) diff --git a/src/types/mod.rs b/src/types/mod.rs index 6b398406..4aa8d8ce 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -815,6 +815,25 @@ pub trait ToSql: fmt::Debug { -> Result; } +impl<'a, T> ToSql for &'a T where T: ToSql { + fn to_sql_checked(&self, ty: &Type, out: &mut Write, ctx: &SessionInfo) + -> Result { + if !<&'a T as ToSql>::accepts(ty) { + return Err(Error::WrongType(ty.clone())); + } + self.to_sql(ty, out, ctx) + } + + + fn to_sql(&self, ty: &Type, out: &mut W, ctx: &SessionInfo) -> Result { + (*self).to_sql(ty, out, ctx) + } + + fn accepts(ty: &Type) -> bool { T::accepts(ty) } +} + + + impl ToSql for Option { to_sql_checked!(); diff --git a/tests/types/mod.rs b/tests/types/mod.rs index 22e3fbe3..1736e878 100644 --- a/tests/types/mod.rs +++ b/tests/types/mod.rs @@ -31,6 +31,14 @@ fn test_type(sql_type: &str, checks } } +#[test] +fn test_ref_tosql() { + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); + let stmt = conn.prepare("SELECT $1::Int").unwrap(); + let num: &ToSql = &&7; + stmt.query(&[num]).unwrap(); +} + #[test] fn test_bool_params() { test_type("BOOL", &[(Some(true), "'t'"), (Some(false), "'f'"), From d958316d2b0714276df55f38b43f1dbdb9d1b00b Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Mon, 3 Aug 2015 17:15:18 -0700 Subject: [PATCH 2/2] Release v0.9.5 --- Cargo.toml | 4 ++-- README.md | 2 +- src/lib.rs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 14f471a3..557a54f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,11 +1,11 @@ [package] name = "postgres" -version = "0.9.4" +version = "0.9.5" authors = ["Steven Fackler "] license = "MIT" description = "A native PostgreSQL driver" repository = "https://github.com/sfackler/rust-postgres" -documentation = "https://sfackler.github.io/rust-postgres/doc/v0.9.4/postgres" +documentation = "https://sfackler.github.io/rust-postgres/doc/v0.9.5/postgres" readme = "README.md" keywords = ["database", "sql"] build = "build.rs" diff --git a/README.md b/README.md index c9d0f956..488127de 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Rust-Postgres A native PostgreSQL driver for Rust. -[Documentation](https://sfackler.github.io/rust-postgres/doc/v0.9.4/postgres) +[Documentation](https://sfackler.github.io/rust-postgres/doc/v0.9.5/postgres) [![Build Status](https://travis-ci.org/sfackler/rust-postgres.png?branch=master)](https://travis-ci.org/sfackler/rust-postgres) [![Latest Version](https://img.shields.io/crates/v/postgres.svg)](https://crates.io/crates/postgres) diff --git a/src/lib.rs b/src/lib.rs index 437cad8f..e8c25f08 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -41,7 +41,7 @@ //! } //! } //! ``` -#![doc(html_root_url="https://sfackler.github.io/rust-postgres/doc/v0.9.4")] +#![doc(html_root_url="https://sfackler.github.io/rust-postgres/doc/v0.9.5")] #![warn(missing_docs)] extern crate bufstream;