Use Vec instead of MemWriter

This commit is contained in:
Steven Fackler 2014-11-19 20:54:32 -08:00
parent ec6e242dd4
commit 1fa5941562
3 changed files with 21 additions and 31 deletions

View File

@ -72,7 +72,7 @@ use openssl::ssl::SslContext;
use serialize::hex::ToHex;
use std::cell::{Cell, RefCell};
use std::collections::{RingBuf, HashMap};
use std::io::{BufferedStream, IoResult, MemWriter};
use std::io::{BufferedStream, IoResult};
use std::io::net::ip::Port;
use std::mem;
use std::fmt;
@ -517,21 +517,21 @@ impl InnerConnection {
fn prepare_copy_in<'a>(&mut self, table: &str, rows: &[&str], conn: &'a Connection)
-> Result<CopyInStatement<'a>> {
let mut query = MemWriter::new();
let mut query = vec![];
let _ = write!(query, "SELECT ");
let _ = util::comma_join(&mut query, rows.iter().map(|&e| e));
let _ = write!(query, " FROM {}", table);
let query = String::from_utf8(query.unwrap()).unwrap();
let query = String::from_utf8(query).unwrap();
let (stmt_name, _, result_desc) = try!(self.raw_prepare(query[]));
let column_types = result_desc.iter().map(|desc| desc.ty.clone()).collect();
try!(self.close_statement(stmt_name[]));
let mut query = MemWriter::new();
let mut query = vec![];
let _ = write!(query, "COPY {} (", table);
let _ = util::comma_join(&mut query, rows.iter().map(|&e| e));
let _ = write!(query, ") FROM STDIN WITH (FORMAT binary)");
let query = String::from_utf8(query.unwrap()).unwrap();
let query = String::from_utf8(query).unwrap();
let (stmt_name, _, _) = try!(self.raw_prepare(query[]));
Ok(CopyInStatement {
@ -1549,7 +1549,7 @@ impl<'a> CopyInStatement<'a> {
}
}
let mut buf = MemWriter::new();
let mut buf = vec![];
let _ = buf.write(b"PGCOPY\n\xff\r\n\x00");
let _ = buf.write_be_i32(0);
let _ = buf.write_be_i32(0);
@ -1590,19 +1590,17 @@ impl<'a> CopyInStatement<'a> {
}
}
let mut data = buf.unwrap();
try_desync!(conn, conn.stream.write_message(
&CopyData {
data: data[]
data: buf[]
}));
data.clear();
buf = MemWriter::from_vec(data);
buf.clear();
}
let _ = buf.write_be_i16(-1);
try!(conn.write_messages(&[
CopyData {
data: buf.unwrap()[],
data: buf[],
},
CopyDone,
Sync]));

View File

@ -1,4 +1,4 @@
use std::io::{IoResult, IoError, OtherIoError, MemWriter, MemReader};
use std::io::{IoResult, IoError, OtherIoError, MemReader};
use std::mem;
use types::Oid;
@ -149,7 +149,7 @@ pub trait WriteMessage {
impl<W: Writer> WriteMessage for W {
fn write_message(&mut self, message: &FrontendMessage) -> IoResult<()> {
let mut buf = MemWriter::new();
let mut buf = vec![];
let mut ident = None;
match *message {
@ -250,7 +250,6 @@ impl<W: Writer> WriteMessage for W {
try!(self.write_u8(ident));
}
let buf = buf.unwrap();
// add size of length value
try!(self.write_be_i32((buf.len() + mem::size_of::<i32>()) as i32));
try!(self.write(buf[]));

View File

@ -1,7 +1,7 @@
//! Traits dealing with Postgres data types
use serialize::json;
use std::collections::HashMap;
use std::io::{ByRefReader, MemWriter, BufReader};
use std::io::{ByRefReader, BufReader};
use self::Type::*;
use Result;
@ -178,7 +178,6 @@ macro_rules! to_range_impl(
($t:ty) => (
impl ::types::RawToSql for ::types::range::Range<$t> {
fn raw_to_sql<W: Writer>(&self, buf: &mut W) -> Result<()> {
use std::io::MemWriter;
use types::{RANGE_EMPTY, RANGE_LOWER_UNBOUNDED, RANGE_LOWER_INCLUSIVE,
RANGE_UPPER_UNBOUNDED, RANGE_UPPER_INCLUSIVE};
use types::range::{BoundType, RangeBound};
@ -207,9 +206,8 @@ macro_rules! to_range_impl(
match self.lower() {
Some(bound) => {
let mut inner_buf = MemWriter::new();
let mut inner_buf = vec![];
try!(bound.value.raw_to_sql(&mut inner_buf));
let inner_buf = inner_buf.unwrap();
try!(buf.write_be_i32(inner_buf.len() as i32));
try!(buf.write(inner_buf[]));
}
@ -217,9 +215,8 @@ macro_rules! to_range_impl(
}
match self.upper() {
Some(bound) => {
let mut inner_buf = MemWriter::new();
let mut inner_buf = vec![];
try!(bound.value.raw_to_sql(&mut inner_buf));
let inner_buf = inner_buf.unwrap();
try!(buf.write_be_i32(inner_buf.len() as i32));
try!(buf.write(inner_buf[]));
}
@ -268,13 +265,11 @@ macro_rules! to_raw_to_impl(
$(#[$a])*
impl ::types::ToSql for $t {
fn to_sql(&self, ty: &Type) -> Result<Option<Vec<u8>>> {
use std::io::MemWriter;
check_types!($($oid)|+, ty)
let mut writer = MemWriter::new();
let mut writer = vec![];
try!(self.raw_to_sql(&mut writer));
Ok(Some(writer.unwrap()))
Ok(Some(writer))
}
}
@ -287,11 +282,10 @@ macro_rules! to_array_impl(
$(#[$a])*
impl ::types::ToSql for ::types::array::ArrayBase<Option<$t>> {
fn to_sql(&self, ty: &Type) -> Result<Option<Vec<u8>>> {
use std::io::MemWriter;
use types::array::Array;
check_types!($($oid)|+, ty)
let mut buf = MemWriter::new();
let mut buf = vec![];
try!(buf.write_be_i32(self.dimension_info().len() as i32));
try!(buf.write_be_i32(1));
@ -305,9 +299,8 @@ macro_rules! to_array_impl(
for v in self.values() {
match *v {
Some(ref val) => {
let mut inner_buf = MemWriter::new();
let mut inner_buf = vec![];
try!(val.raw_to_sql(&mut inner_buf));
let inner_buf = inner_buf.unwrap();
try!(buf.write_be_i32(inner_buf.len() as i32));
try!(buf.write(inner_buf[]));
}
@ -315,7 +308,7 @@ macro_rules! to_array_impl(
}
}
Ok(Some(buf.unwrap()))
Ok(Some(buf))
}
}
@ -740,7 +733,7 @@ impl ToSql for HashMap<String, Option<String>> {
_ => return Err(PgWrongType(ty.clone()))
}
let mut buf = MemWriter::new();
let mut buf = vec![];
try!(buf.write_be_i32(self.len() as i32));
@ -757,7 +750,7 @@ impl ToSql for HashMap<String, Option<String>> {
}
}
Ok(Some(buf.unwrap()))
Ok(Some(buf))
}
}