Some ~[] -> Vec work

This commit is contained in:
Steven Fackler 2014-03-14 22:33:24 -07:00
parent 850982c0b0
commit 5b646c0ca1
5 changed files with 37 additions and 33 deletions

View File

@ -3,6 +3,7 @@
use collections::HashMap; use collections::HashMap;
use std::from_str::FromStr; use std::from_str::FromStr;
use std::io::IoError; use std::io::IoError;
use std::vec_ng::Vec;
use openssl::ssl::error::SslError; use openssl::ssl::error::SslError;
use phf::PhfMap; use phf::PhfMap;
@ -450,7 +451,7 @@ pub struct PostgresDbError {
impl PostgresDbError { impl PostgresDbError {
#[doc(hidden)] #[doc(hidden)]
pub fn new(fields: ~[(u8, ~str)]) -> PostgresDbError { pub fn new(fields: Vec<(u8, ~str)>) -> PostgresDbError {
let mut map: HashMap<u8, ~str> = fields.move_iter().collect(); let mut map: HashMap<u8, ~str> = fields.move_iter().collect();
PostgresDbError { PostgresDbError {
severity: map.pop(&('S' as u8)).unwrap(), severity: map.pop(&('S' as u8)).unwrap(),

View File

@ -88,6 +88,7 @@ use std::io::net::tcp::TcpStream;
use std::mem; use std::mem;
use std::str; use std::str;
use std::task; use std::task;
use std::vec_ng::Vec;
use error::{DnsError, use error::{DnsError,
InvalidUrl, InvalidUrl,
@ -588,18 +589,18 @@ impl InnerPostgresConnection {
_ => unreachable!() _ => unreachable!()
} }
let mut param_types: ~[PostgresType] = match if_ok_pg!(self.read_message()) { let mut param_types: Vec<PostgresType> = match if_ok_pg!(self.read_message()) {
ParameterDescription { types } => ParameterDescription { types } =>
types.iter().map(|ty| PostgresType::from_oid(*ty)).collect(), types.iter().map(|ty| PostgresType::from_oid(*ty)).collect(),
_ => unreachable!() _ => unreachable!()
}; };
let mut result_desc: ~[ResultDescription] = match if_ok_pg!(self.read_message()) { let mut result_desc: Vec<ResultDescription> = match if_ok_pg!(self.read_message()) {
RowDescription { descriptions } => RowDescription { descriptions } =>
descriptions.move_iter().map(|desc| { descriptions.move_iter().map(|desc| {
stmt::make_ResultDescription(desc) stmt::make_ResultDescription(desc)
}).collect(), }).collect(),
NoData => ~[], NoData => Vec::new(),
_ => unreachable!() _ => unreachable!()
}; };
@ -644,8 +645,8 @@ impl InnerPostgresConnection {
None => {} None => {}
} }
let name = try!(self.quick_query( let name = try!(self.quick_query(
format!("SELECT typname FROM pg_type WHERE oid={}", oid)))[0][0] format!("SELECT typname FROM pg_type WHERE oid={}", oid)))
.unwrap(); .move_iter().next().unwrap().move_iter().next().unwrap().unwrap();
self.unknown_types.insert(oid, name.clone()); self.unknown_types.insert(oid, name.clone());
Ok(name) Ok(name)
} }
@ -658,11 +659,11 @@ impl InnerPostgresConnection {
} }
fn quick_query(&mut self, query: &str) fn quick_query(&mut self, query: &str)
-> Result<~[~[Option<~str>]], PostgresError> { -> Result<Vec<Vec<Option<~str>>>, PostgresError> {
check_desync!(self); check_desync!(self);
if_ok_pg!(self.write_messages([Query { query: query }])); if_ok_pg!(self.write_messages([Query { query: query }]));
let mut result = ~[]; let mut result = Vec::new();
loop { loop {
match if_ok_pg!(self.read_message()) { match if_ok_pg!(self.read_message()) {
ReadyForQuery { .. } => break, ReadyForQuery { .. } => break,
@ -893,7 +894,7 @@ impl PostgresConnection {
} }
fn quick_query(&self, query: &str) fn quick_query(&self, query: &str)
-> Result<~[~[Option<~str>]], PostgresError> { -> Result<Vec<Vec<Option<~str>>>, PostgresError> {
self.conn.with_mut(|conn| conn.quick_query(query)) self.conn.with_mut(|conn| conn.quick_query(query))
} }

View File

@ -2,6 +2,7 @@ use std::str;
use std::io::{IoResult, MemWriter, MemReader}; use std::io::{IoResult, MemWriter, MemReader};
use std::mem; use std::mem;
use std::vec; use std::vec;
use std::vec_ng::Vec;
use types::Oid; use types::Oid;
@ -29,15 +30,15 @@ pub enum BackendMessage {
tag: ~str tag: ~str
}, },
DataRow { DataRow {
row: ~[Option<~[u8]>] row: Vec<Option<~[u8]>>
}, },
EmptyQueryResponse, EmptyQueryResponse,
ErrorResponse { ErrorResponse {
fields: ~[(u8, ~str)] fields: Vec<(u8, ~str)>
}, },
NoData, NoData,
NoticeResponse { NoticeResponse {
fields: ~[(u8, ~str)] fields: Vec<(u8, ~str)>
}, },
NotificationResponse { NotificationResponse {
pid: i32, pid: i32,
@ -45,7 +46,7 @@ pub enum BackendMessage {
payload: ~str payload: ~str
}, },
ParameterDescription { ParameterDescription {
types: ~[Oid] types: Vec<Oid>
}, },
ParameterStatus { ParameterStatus {
parameter: ~str, parameter: ~str,
@ -57,7 +58,7 @@ pub enum BackendMessage {
state: u8 state: u8
}, },
RowDescription { RowDescription {
descriptions: ~[RowDescriptionEntry] descriptions: Vec<RowDescriptionEntry>
} }
} }
@ -301,8 +302,8 @@ impl<R: Reader> ReadMessage for R {
} }
} }
fn read_fields(buf: &mut MemReader) -> IoResult<~[(u8, ~str)]> { fn read_fields(buf: &mut MemReader) -> IoResult<Vec<(u8, ~str)>> {
let mut fields = ~[]; let mut fields = Vec::new();
loop { loop {
let ty = try!(buf.read_u8()); let ty = try!(buf.read_u8());
if ty == 0 { if ty == 0 {
@ -317,7 +318,7 @@ fn read_fields(buf: &mut MemReader) -> IoResult<~[(u8, ~str)]> {
fn read_data_row(buf: &mut MemReader) -> IoResult<BackendMessage> { fn read_data_row(buf: &mut MemReader) -> IoResult<BackendMessage> {
let len = try!(buf.read_be_i16()) as uint; let len = try!(buf.read_be_i16()) as uint;
let mut values = vec::with_capacity(len); let mut values = Vec::with_capacity(len);
for _ in range(0, len) { for _ in range(0, len) {
let val = match try!(buf.read_be_i32()) { let val = match try!(buf.read_be_i32()) {
@ -345,7 +346,7 @@ fn read_auth_message(buf: &mut MemReader) -> IoResult<BackendMessage> {
fn read_parameter_description(buf: &mut MemReader) -> IoResult<BackendMessage> { fn read_parameter_description(buf: &mut MemReader) -> IoResult<BackendMessage> {
let len = try!(buf.read_be_i16()) as uint; let len = try!(buf.read_be_i16()) as uint;
let mut types = vec::with_capacity(len); let mut types = Vec::with_capacity(len);
for _ in range(0, len) { for _ in range(0, len) {
types.push(try!(buf.read_be_u32())); types.push(try!(buf.read_be_u32()));
@ -356,7 +357,7 @@ fn read_parameter_description(buf: &mut MemReader) -> IoResult<BackendMessage> {
fn read_row_description(buf: &mut MemReader) -> IoResult<BackendMessage> { fn read_row_description(buf: &mut MemReader) -> IoResult<BackendMessage> {
let len = try!(buf.read_be_i16()) as uint; let len = try!(buf.read_be_i16()) as uint;
let mut types = vec::with_capacity(len); let mut types = Vec::with_capacity(len);
for _ in range(0, len) { for _ in range(0, len) {
types.push(RowDescriptionEntry { types.push(RowDescriptionEntry {

View File

@ -2,6 +2,7 @@ use collections::{Deque, RingBuf};
use std::cell::Cell; use std::cell::Cell;
use std::from_str::FromStr; use std::from_str::FromStr;
use std::task; use std::task;
use std::vec_ng::Vec;
use PostgresConnection; use PostgresConnection;
use error::{PgDbError, use error::{PgDbError,
@ -116,16 +117,16 @@ pub trait PostgresStatement {
pub struct NormalPostgresStatement<'conn> { pub struct NormalPostgresStatement<'conn> {
priv conn: &'conn PostgresConnection, priv conn: &'conn PostgresConnection,
priv name: ~str, priv name: ~str,
priv param_types: ~[PostgresType], priv param_types: Vec<PostgresType>,
priv result_desc: ~[ResultDescription], priv result_desc: Vec<ResultDescription>,
priv next_portal_id: Cell<uint>, priv next_portal_id: Cell<uint>,
priv finished: Cell<bool>, priv finished: Cell<bool>,
} }
pub fn make_NormalPostgresStatement<'a>(conn: &'a PostgresConnection, pub fn make_NormalPostgresStatement<'a>(conn: &'a PostgresConnection,
name: ~str, name: ~str,
param_types: ~[PostgresType], param_types: Vec<PostgresType>,
result_desc: ~[ResultDescription]) result_desc: Vec<ResultDescription>)
-> NormalPostgresStatement<'a> { -> NormalPostgresStatement<'a> {
NormalPostgresStatement { NormalPostgresStatement {
conn: conn, conn: conn,
@ -174,8 +175,8 @@ impl<'conn> NormalPostgresStatement<'conn> {
fn execute(&self, portal_name: &str, row_limit: uint, params: &[&ToSql]) fn execute(&self, portal_name: &str, row_limit: uint, params: &[&ToSql])
-> Result<(), PostgresError> { -> Result<(), PostgresError> {
let mut formats = ~[]; let mut formats = Vec::new();
let mut values = ~[]; let mut values = Vec::new();
assert!(self.param_types.len() == params.len(), assert!(self.param_types.len() == params.len(),
"Expected {} parameters but found {}", "Expected {} parameters but found {}",
self.param_types.len(), params.len()); self.param_types.len(), params.len());
@ -185,7 +186,7 @@ impl<'conn> NormalPostgresStatement<'conn> {
values.push(value); values.push(value);
}; };
let result_formats: ~[i16] = self.result_desc.iter().map(|desc| { let result_formats: Vec<i16> = self.result_desc.iter().map(|desc| {
desc.ty.result_format() as i16 desc.ty.result_format() as i16
}).collect(); }).collect();
@ -193,9 +194,9 @@ impl<'conn> NormalPostgresStatement<'conn> {
Bind { Bind {
portal: portal_name, portal: portal_name,
statement: self.name.as_slice(), statement: self.name.as_slice(),
formats: formats, formats: formats.as_slice(),
values: values, values: values.as_slice(),
result_formats: result_formats result_formats: result_formats.as_slice()
}, },
Execute { Execute {
portal: portal_name, portal: portal_name,
@ -380,7 +381,7 @@ impl<'conn> TransactionalPostgresStatement<'conn> {
pub struct PostgresResult<'stmt> { pub struct PostgresResult<'stmt> {
priv stmt: &'stmt NormalPostgresStatement<'stmt>, priv stmt: &'stmt NormalPostgresStatement<'stmt>,
priv name: ~str, priv name: ~str,
priv data: RingBuf<~[Option<~[u8]>]>, priv data: RingBuf<Vec<Option<~[u8]>>>,
priv row_limit: uint, priv row_limit: uint,
priv more_rows: bool, priv more_rows: bool,
priv finished: bool, priv finished: bool,
@ -514,7 +515,7 @@ impl<'stmt> Iterator<PostgresRow<'stmt>> for PostgresResult<'stmt> {
/// ``` /// ```
pub struct PostgresRow<'stmt> { pub struct PostgresRow<'stmt> {
priv stmt: &'stmt NormalPostgresStatement<'stmt>, priv stmt: &'stmt NormalPostgresStatement<'stmt>,
priv data: ~[Option<~[u8]>] priv data: Vec<Option<~[u8]>>
} }
impl<'stmt> Container for PostgresRow<'stmt> { impl<'stmt> Container for PostgresRow<'stmt> {
@ -527,7 +528,7 @@ impl<'stmt> Container for PostgresRow<'stmt> {
impl<'stmt, I: RowIndex, T: FromSql> Index<I, T> for PostgresRow<'stmt> { impl<'stmt, I: RowIndex, T: FromSql> Index<I, T> for PostgresRow<'stmt> {
fn index(&self, idx: &I) -> T { fn index(&self, idx: &I) -> T {
let idx = idx.idx(self.stmt); let idx = idx.idx(self.stmt);
FromSql::from_sql(&self.stmt.result_desc[idx].ty, &self.data[idx]) FromSql::from_sql(&self.stmt.result_desc.get(idx).ty, self.data.get(idx))
} }
} }

@ -1 +1 @@
Subproject commit ba1a08d0c78a3aa672be46ab54eb00db7d6b81a9 Subproject commit 34da3b9a2704f129ff426eed66431340fee98112