From 4b877e0a337bb0c3f65a0ca8699b36f7abbeac8f Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Tue, 7 Apr 2015 23:57:22 -0700 Subject: [PATCH] Cleanup --- src/lib.rs | 5 +++-- src/message.rs | 14 +++----------- src/types/mod.rs | 18 +++++++++++++++--- src/types/slice.rs | 3 ++- src/ugh_privacy.rs | 2 +- 5 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index cb430601..4bd4ef58 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -115,7 +115,7 @@ pub enum ConnectTarget { Unix(PathBuf) } -/// Authentication information +/// Authentication information. #[derive(Clone, Debug)] pub struct UserInfo { /// The username @@ -502,7 +502,8 @@ impl InnerConnection { } match self.raw_prepare(TYPEINFO_QUERY, - "SELECT typname, typelem, NULL::OID FROM pg_catalog.pg_type \ + "SELECT typname, typelem, NULL::OID \ + FROM pg_catalog.pg_type \ WHERE oid = $1") { Ok(..) => Ok(()), Err(Error::IoError(e)) => Err(ConnectError::IoError(e)), diff --git a/src/message.rs b/src/message.rs index e46b90b3..ff9e7841 100644 --- a/src/message.rs +++ b/src/message.rs @@ -263,8 +263,7 @@ impl ReadCStr for R { let mut buf = vec![]; try!(self.read_until(0, &mut buf)); buf.pop(); - String::from_utf8(buf).map_err(|_| io::Error::new(io::ErrorKind::Other, - "received a non-utf8 string from server")) + String::from_utf8(buf).map_err(|err| io::Error::new(io::ErrorKind::Other, err)) } } @@ -320,11 +319,7 @@ impl ReadMessage for R { b't' => try!(read_parameter_description(&mut rdr)), b'T' => try!(read_row_description(&mut rdr)), b'Z' => ReadyForQuery { _state: try!(rdr.read_u8()) }, - _ => { - return Err(io::Error::new(io::ErrorKind::Other, - "unexpected message tag")) - - } + _ => return Err(io::Error::new(io::ErrorKind::Other, "unexpected message tag")), }; if rdr.limit() != 0 { return Err(io::Error::new(io::ErrorKind::Other, "didn't read entire message")); @@ -380,10 +375,7 @@ fn read_auth_message(buf: &mut R) -> io::Result { 6 => AuthenticationSCMCredential, 7 => AuthenticationGSS, 9 => AuthenticationSSPI, - _ => { - return Err(io::Error::new(io::ErrorKind::Other, - "unexpected authentication tag")); - } + _ => return Err(io::Error::new(io::ErrorKind::Other, "unexpected authentication tag")), }) } diff --git a/src/types/mod.rs b/src/types/mod.rs index e11444e0..cb7b52f2 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -8,6 +8,7 @@ use byteorder::{ReadBytesExt, WriteBytesExt, BigEndian}; use Result; use error::Error; +use util; pub use ugh_privacy::Other; @@ -106,7 +107,16 @@ macro_rules! make_postgres_type { } /// Returns the OID of the `Type`. + /// + /// # Deprecated + /// + /// Use `oid` instead. pub fn to_oid(&self) -> Oid { + self.oid() + } + + /// Returns the OID of the `Type`. + pub fn oid(&self) -> Oid { match *self { $(Type::$variant => as_expr!($oid),)+ Type::Other(ref u) => u.oid(), @@ -464,7 +474,7 @@ pub trait FromSql: Sized { } } - /// Creates a new value of this type from a `Read` of the binary format + /// Creates a new value of this type from a `Read`er of the binary format /// of the specified Postgres `Type`. /// /// The caller of this method is responsible for ensuring that this type @@ -564,7 +574,8 @@ impl FromSql for HashMap> { for _ in 0..count { let key_len = try!(raw.read_i32::()); let mut key = vec![]; - try!(raw.take(key_len as u64).read_to_end(&mut key)); + key.extend((0..key_len).map(|_| 0)); + try!(util::read_all(raw, &mut key)); let key = match String::from_utf8(key) { Ok(key) => key, Err(_) => return Err(Error::BadResponse), @@ -575,7 +586,8 @@ impl FromSql for HashMap> { None } else { let mut val = vec![]; - try!(raw.take(val_len as u64).read_to_end(&mut val)); + val.extend((0..val_len).map(|_| 0)); + try!(util::read_all(raw, &mut val)); match String::from_utf8(val) { Ok(val) => Some(val), Err(_) => return Err(Error::BadResponse), diff --git a/src/types/slice.rs b/src/types/slice.rs index fe72b043..dc70aeeb 100644 --- a/src/types/slice.rs +++ b/src/types/slice.rs @@ -46,8 +46,8 @@ impl<'a, T: 'a + ToSql> ToSql for Slice<'a, T> { try!(w.write_i32::(self.0.len() as i32)); try!(w.write_i32::(0)); // index offset + let mut inner_buf = vec![]; for e in self.0 { - let mut inner_buf = vec![]; match try!(e.to_sql(&member_type, &mut inner_buf)) { IsNull::No => { try!(w.write_i32::(inner_buf.len() as i32)); @@ -55,6 +55,7 @@ impl<'a, T: 'a + ToSql> ToSql for Slice<'a, T> { } IsNull::Yes => try!(w.write_i32::(-1)), } + inner_buf.clear(); } Ok(IsNull::No) diff --git a/src/ugh_privacy.rs b/src/ugh_privacy.rs index a1bfbef0..97628cc5 100644 --- a/src/ugh_privacy.rs +++ b/src/ugh_privacy.rs @@ -57,7 +57,7 @@ pub struct DbError { constraint: Option, file: String, line: u32, - routine: String + routine: String, } pub fn dberror_new_raw(fields: Vec<(u8, String)>) -> result::Result {