Skip to content

Commit 785c674

Browse files
committed
Clean up FromSql API
1 parent 2b1b9c1 commit 785c674

2 files changed

Lines changed: 21 additions & 24 deletions

File tree

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ impl InnerConnection {
698698
_ => bad_response!(self)
699699
}
700700
let name: String = match try!(self.read_message()) {
701-
DataRow { row } => try!(FromSql::from_sql(&Type::Name, &row[0])),
701+
DataRow { row } => try!(FromSql::from_sql(&Type::Name, row[0].as_ref().map(|r| &**r))),
702702
ErrorResponse { fields } => {
703703
try!(self.wait_for_ready());
704704
return DbError::new(fields);
@@ -1521,7 +1521,7 @@ impl<'stmt> Row<'stmt> {
15211521
/// the return type is not compatible with the Postgres type.
15221522
pub fn get_opt<I, T>(&self, idx: I) -> Result<T> where I: RowIndex, T: FromSql {
15231523
let idx = try!(idx.idx(self.stmt).ok_or(Error::InvalidColumn));
1524-
FromSql::from_sql(&self.stmt.result_desc[idx].ty, &self.data[idx])
1524+
FromSql::from_sql(&self.stmt.result_desc[idx].ty, self.data[idx].as_ref().map(|e| &**e))
15251525
}
15261526

15271527
/// Retrieves the contents of a field of the row.

src/types/mod.rs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Traits dealing with Postgres data types
22
use serialize::json;
33
use std::collections::HashMap;
4-
use std::io::{ByRefReader, BufReader};
4+
use std::io::ByRefReader;
55
use std::io::util::LimitReader;
66
use std::io::net::ip::IpAddr;
77

@@ -33,7 +33,7 @@ macro_rules! from_option_impl {
3333
($t:ty $(, $a:meta)*) => {
3434
$(#[$a])*
3535
impl ::types::FromSql for $t {
36-
fn from_sql(ty: &Type, raw: &Option<Vec<u8>>) -> Result<$t> {
36+
fn from_sql(ty: &Type, raw: Option<&[u8]>) -> Result<$t> {
3737
use Error;
3838
use types::FromSql;
3939

@@ -53,10 +53,10 @@ macro_rules! from_map_impl {
5353
($($expected:pat)|+, $t:ty, $blk:expr $(, $a:meta)*) => (
5454
$(#[$a])*
5555
impl ::types::FromSql for Option<$t> {
56-
fn from_sql(ty: &Type, raw: &Option<Vec<u8>>) -> Result<Option<$t>> {
56+
fn from_sql(ty: &Type, raw: Option<&[u8]>) -> Result<Option<$t>> {
5757
check_types!($($expected)|+, ty);
58-
match *raw {
59-
Some(ref buf) => ($blk)(buf).map(|ok| Some(ok)),
58+
match raw {
59+
Some(buf) => ($blk)(buf).map(|ok| Some(ok)),
6060
None => Ok(None)
6161
}
6262
}
@@ -68,27 +68,25 @@ macro_rules! from_map_impl {
6868

6969
macro_rules! from_raw_from_impl {
7070
($($expected:pat)|+, $t:ty $(, $a:meta)*) => (
71-
from_map_impl!($($expected)|+, $t, |buf: &Vec<u8>| {
72-
use std::io::BufReader;
71+
from_map_impl!($($expected)|+, $t, |mut buf: &[u8]| {
7372
use types::RawFromSql;
7473

75-
let mut reader = BufReader::new(&**buf);
76-
RawFromSql::raw_from_sql(&mut reader)
74+
RawFromSql::raw_from_sql(&mut buf)
7775
} $(, $a)*);
7876
)
7977
}
8078

8179
macro_rules! from_array_impl {
8280
($($oid:pat)|+, $t:ty $(, $a:meta)*) => (
83-
from_map_impl!($($oid)|+, ::types::array::ArrayBase<Option<$t>>, |buf: &Vec<u8>| {
84-
use std::io::{BufReader, ByRefReader};
81+
from_map_impl!($($oid)|+, ::types::array::ArrayBase<Option<$t>>, |buf: &[u8]| {
82+
use std::io::ByRefReader;
8583
use std::io::util::LimitReader;
8684
use std::iter::MultiplicativeIterator;
8785
use types::{Oid, RawFromSql};
8886
use types::array::{ArrayBase, DimensionInfo};
8987
use Error;
9088

91-
let mut rdr = BufReader::new(&**buf);
89+
let mut rdr = buf;
9290

9391
let ndim = try!(rdr.read_be_i32()) as uint;
9492
let _has_null = try!(rdr.read_be_i32()) == 1;
@@ -431,7 +429,7 @@ pub trait FromSql {
431429
/// Creates a new value of this type from a buffer of Postgres data.
432430
///
433431
/// If the value was `NULL`, the buffer will be `None`.
434-
fn from_sql(ty: &Type, raw: &Option<Vec<u8>>) -> Result<Self>;
432+
fn from_sql(ty: &Type, raw: Option<&[u8]>) -> Result<Self>;
435433
}
436434

437435
#[doc(hidden)]
@@ -563,17 +561,16 @@ from_array_impl!(Type::Int4RangeArray, Range<i32>);
563561
from_array_impl!(Type::Int8RangeArray, Range<i64>);
564562

565563
impl FromSql for Option<String> {
566-
fn from_sql(ty: &Type, raw: &Option<Vec<u8>>) -> Result<Option<String>> {
564+
fn from_sql(ty: &Type, raw: Option<&[u8]>) -> Result<Option<String>> {
567565
match *ty {
568566
Type::Varchar | Type::Text | Type::CharN | Type::Name => {}
569567
Type::Unknown { ref name, .. } if "citext" == *name => {}
570568
_ => return Err(Error::WrongType(ty.clone()))
571569
}
572570

573-
match *raw {
574-
Some(ref buf) => {
575-
let mut rdr = &**buf;
576-
Ok(Some(try!(RawFromSql::raw_from_sql(&mut rdr))))
571+
match raw {
572+
Some(mut buf) => {
573+
Ok(Some(try!(RawFromSql::raw_from_sql(&mut buf))))
577574
}
578575
None => Ok(None)
579576
}
@@ -583,16 +580,16 @@ impl FromSql for Option<String> {
583580
from_option_impl!(String);
584581

585582
impl FromSql for Option<HashMap<String, Option<String>>> {
586-
fn from_sql(ty: &Type, raw: &Option<Vec<u8>>)
583+
fn from_sql(ty: &Type, raw: Option<&[u8]>)
587584
-> Result<Option<HashMap<String, Option<String>>>> {
588585
match *ty {
589586
Type::Unknown { ref name, .. } if "hstore" == *name => {}
590587
_ => return Err(Error::WrongType(ty.clone()))
591588
}
592589

593-
match *raw {
594-
Some(ref buf) => {
595-
let mut rdr = BufReader::new(&**buf);
590+
match raw {
591+
Some(buf) => {
592+
let mut rdr = buf;
596593
let mut map = HashMap::new();
597594

598595
let count = try!(rdr.read_be_i32());

0 commit comments

Comments
 (0)