Fixes for Rust updates

No more hacky macro module!
This commit is contained in:
Steven Fackler 2013-11-26 18:27:34 -08:00
parent 48dfb4dd8d
commit 99d570f17b
7 changed files with 66 additions and 85 deletions

View File

@ -3,28 +3,23 @@
use std::hashmap::HashMap;
use super::ssl::error::SslError;
use self::hack::PostgresSqlState;
macro_rules! make_errors(
($($code:pat => $error:ident),+) => (
// TODO: Get rid of this module when mozilla/rust#4375 is fixed
/// A module to get around issues with macro expansion
pub mod hack {
/// SQLSTATE error codes
#[deriving(ToStr, Eq)]
#[allow(missing_doc)]
pub enum PostgresSqlState {
$($error,)+
UnknownSqlState(~str)
}
/// SQLSTATE error codes
#[deriving(ToStr, Eq)]
#[allow(missing_doc)]
pub enum PostgresSqlState {
$($error,)+
UnknownSqlState(~str)
}
impl FromStr for PostgresSqlState {
fn from_str(s: &str) -> Option<PostgresSqlState> {
Some(match s {
$($code => $error,)+
state => UnknownSqlState(state.to_owned())
})
}
impl FromStr for PostgresSqlState {
fn from_str(s: &str) -> Option<PostgresSqlState> {
Some(match s {
$($code => $error,)+
state => UnknownSqlState(state.to_owned())
})
}
}
)

70
lib.rs
View File

@ -184,9 +184,7 @@ impl<'conn > Iterator<PostgresNotification> for
/// `next` may return `Some` notification after returning `None` if a new
/// notification was received.
fn next(&mut self) -> Option<PostgresNotification> {
do self.conn.conn.with_mut |conn| {
conn.notifications.pop_front()
}
self.conn.conn.with_mut(|conn| { conn.notifications.pop_front() })
}
}
@ -235,18 +233,18 @@ pub fn cancel_query(url: &str, ssl: &SslMode, data: PostgresCancelData)
fn open_socket(host: &str, port: Port)
-> Result<TcpStream, PostgresConnectError> {
let addrs = do io_error::cond.trap(|_| {}).inside {
let addrs = io_error::cond.trap(|_| {}).inside(|| {
net::get_host_addresses(host)
};
});
let addrs = match addrs {
Some(addrs) => addrs,
None => return Err(DnsError)
};
for addr in addrs.iter() {
let socket = do io_error::cond.trap(|_| {}).inside {
let socket = io_error::cond.trap(|_| {}).inside(|| {
TcpStream::connect(SocketAddr { ip: *addr, port: port })
};
});
match socket {
Some(socket) => return Ok(socket),
None => {}
@ -326,9 +324,9 @@ struct InnerPostgresConnection {
impl Drop for InnerPostgresConnection {
fn drop(&mut self) {
do io_error::cond.trap(|_| {}).inside {
io_error::cond.trap(|_| {}).inside(|| {
self.write_messages([Terminate]);
}
})
}
}
@ -566,11 +564,11 @@ impl PostgresConnection {
/// username if not specified.
pub fn try_connect(url: &str, ssl: &SslMode)
-> Result<PostgresConnection, PostgresConnectError> {
do InnerPostgresConnection::try_connect(url, ssl).map |conn| {
InnerPostgresConnection::try_connect(url, ssl).map(|conn| {
PostgresConnection {
conn: RefCell::new(conn)
}
}
})
}
/// A convenience wrapper around `try_connect`.
@ -611,9 +609,7 @@ impl PostgresConnection {
/// not outlive that connection.
pub fn try_prepare<'a>(&'a self, query: &str)
-> Result<NormalPostgresStatement<'a>, PostgresDbError> {
do self.conn.with_mut |conn| {
conn.try_prepare(query, self)
}
self.conn.with_mut(|conn| conn.try_prepare(query, self))
}
/// A convenience wrapper around `try_prepare`.
@ -653,9 +649,7 @@ impl PostgresConnection {
/// On success, returns the number of rows modified or 0 if not applicable.
pub fn try_update(&self, query: &str, params: &[&ToSql])
-> Result<uint, PostgresDbError> {
do self.try_prepare(query).and_then |stmt| {
stmt.try_update(params)
}
self.try_prepare(query).and_then(|stmt| stmt.try_update(params))
}
/// A convenience wrapper around `try_update`.
@ -676,13 +670,11 @@ impl PostgresConnection {
/// Used with the `cancel_query` function. The object returned can be used
/// to cancel any query executed by the connection it was created from.
pub fn cancel_data(&self) -> PostgresCancelData {
do self.conn.with |conn| {
conn.cancel_data
}
self.conn.with(|conn| conn.cancel_data)
}
fn quick_query(&self, query: &str) {
do self.conn.with_mut |conn| {
self.conn.with_mut(|conn| {
conn.write_messages([Query { query: query }]);
loop {
@ -694,25 +686,19 @@ impl PostgresConnection {
_ => {}
}
}
}
})
}
fn wait_for_ready(&self) {
do self.conn.with_mut |conn| {
conn.wait_for_ready()
}
self.conn.with_mut(|conn| conn.wait_for_ready())
}
fn read_message(&self) -> BackendMessage {
do self.conn.with_mut |conn| {
conn.read_message()
}
self.conn.with_mut(|conn| conn.read_message())
}
fn write_messages(&self, messages: &[FrontendMessage]) {
do self.conn.with_mut |conn| {
conn.write_messages(messages)
}
self.conn.with_mut(|conn| conn.write_messages(messages))
}
}
@ -736,7 +722,7 @@ pub struct PostgresTransaction<'conn> {
#[unsafe_destructor]
impl<'conn> Drop for PostgresTransaction<'conn> {
fn drop(&mut self) {
do io_error::cond.trap(|_| {}).inside {
io_error::cond.trap(|_| {}).inside(|| {
if task::failing() || !self.commit.with(|x| *x) {
if self.nested {
self.conn.quick_query("ROLLBACK TO sp");
@ -750,7 +736,7 @@ impl<'conn> Drop for PostgresTransaction<'conn> {
self.conn.quick_query("COMMIT");
}
}
}
})
}
}
@ -758,11 +744,11 @@ impl<'conn> PostgresTransaction<'conn> {
/// Like `PostgresConnection::try_prepare`.
pub fn try_prepare<'a>(&'a self, query: &str)
-> Result<TransactionalPostgresStatement<'a>, PostgresDbError> {
do self.conn.try_prepare(query).map |stmt| {
self.conn.try_prepare(query).map(|stmt| {
TransactionalPostgresStatement {
stmt: stmt
}
}
})
}
/// Like `PostgresConnection::prepare`.
@ -881,7 +867,7 @@ pub struct NormalPostgresStatement<'conn> {
#[unsafe_destructor]
impl<'conn> Drop for NormalPostgresStatement<'conn> {
fn drop(&mut self) {
do io_error::cond.trap(|_| {}).inside {
io_error::cond.trap(|_| {}).inside(|| {
self.conn.write_messages([
Close {
variant: 'S' as u8,
@ -894,7 +880,7 @@ impl<'conn> Drop for NormalPostgresStatement<'conn> {
_ => {}
}
}
}
})
}
}
@ -992,7 +978,7 @@ impl<'conn> PostgresStatement for NormalPostgresStatement<'conn> {
return Err(PostgresDbError::new(fields));
}
CommandComplete { tag } => {
let s = tag.split_iter(' ').last().unwrap();
let s = tag.split(' ').last().unwrap();
num = match FromStr::from_str(s) {
None => 0,
Some(n) => n
@ -1107,7 +1093,7 @@ pub struct PostgresResult<'stmt> {
#[unsafe_destructor]
impl<'stmt> Drop for PostgresResult<'stmt> {
fn drop(&mut self) {
do io_error::cond.trap(|_| {}).inside {
io_error::cond.trap(|_| {}).inside(|| {
self.stmt.conn.write_messages([
Close {
variant: 'P' as u8,
@ -1120,7 +1106,7 @@ impl<'stmt> Drop for PostgresResult<'stmt> {
_ => {}
}
}
}
})
}
}
@ -1161,12 +1147,12 @@ impl<'stmt> Iterator<PostgresRow<'stmt>> for PostgresResult<'stmt> {
self.execute();
}
do self.data.pop_front().map |row| {
self.data.pop_front().map(|row| {
PostgresRow {
stmt: self.stmt,
data: row
}
}
})
}
}

View File

@ -326,7 +326,7 @@ fn read_data_row(buf: &mut MemReader) -> BackendMessage {
let len = buf.read_be_i16() as uint;
let mut values = vec::with_capacity(len);
do len.times() {
for _ in range(0, len) {
let val = match buf.read_be_i32() {
-1 => None,
len => Some(buf.read_bytes(len as uint))
@ -354,7 +354,7 @@ fn read_parameter_description(buf: &mut MemReader) -> BackendMessage {
let len = buf.read_be_i16() as uint;
let mut types = vec::with_capacity(len);
do len.times() {
for _ in range(0, len) {
types.push(buf.read_be_i32());
}
@ -365,7 +365,7 @@ fn read_row_description(buf: &mut MemReader) -> BackendMessage {
let len = buf.read_be_i16() as uint;
let mut types = vec::with_capacity(len);
do len.times() {
for _ in range(0, len) {
types.push(RowDescriptionEntry {
name: buf.read_string(),
table_oid: buf.read_be_i32(),

View File

@ -80,13 +80,13 @@ impl PostgresConnectionPool {
/// If all connections are in use, blocks until one becomes available.
pub fn get_connection(&self) -> PooledPostgresConnection {
let conn = unsafe {
do self.pool.unsafe_access_cond |pool, cvar| {
self.pool.unsafe_access_cond(|pool, cvar| {
while pool.pool.is_empty() {
cvar.wait();
}
pool.pool.pop()
}
})
};
PooledPostgresConnection {
@ -109,9 +109,9 @@ pub struct PooledPostgresConnection {
impl Drop for PooledPostgresConnection {
fn drop(&mut self) {
unsafe {
do self.pool.pool.unsafe_access |pool| {
self.pool.pool.unsafe_access(|pool| {
pool.pool.push(self.conn.take_unwrap());
}
})
}
}
}

10
test.rs
View File

@ -26,11 +26,11 @@ use lib::error::{DbError,
DnsError,
MissingPassword,
Position,
PostgresDbError};
use lib::error::hack::{SyntaxError,
InvalidPassword,
QueryCanceled,
InvalidCatalogName};
PostgresDbError,
SyntaxError,
InvalidPassword,
QueryCanceled,
InvalidCatalogName};
use lib::types::{ToSql, FromSql, PgInt4, PgVarchar};
use lib::types::range::{Range, Inclusive, Exclusive, RangeBound};
use lib::pool::PostgresConnectionPool;

View File

@ -56,7 +56,7 @@ pub fn read_u64v_be(dst: &mut[u64], input: &[u8]) {
unsafe {
let mut x: *mut i64 = transmute(dst.unsafe_mut_ref(0));
let mut y: *i64 = transmute(input.unsafe_ref(0));
do dst.len().times() {
for _ in range(0, dst.len()) {
*x = to_be64(*y);
x = x.offset(1);
y = y.offset(1);
@ -72,7 +72,7 @@ pub fn read_u32v_be(dst: &mut[u32], input: &[u8]) {
unsafe {
let mut x: *mut i32 = transmute(dst.unsafe_mut_ref(0));
let mut y: *i32 = transmute(input.unsafe_ref(0));
do dst.len().times() {
for _ in range(0, dst.len()) {
*x = to_be32(*y);
x = x.offset(1);
y = y.offset(1);
@ -88,7 +88,7 @@ pub fn read_u32v_le(dst: &mut[u32], input: &[u8]) {
unsafe {
let mut x: *mut i32 = transmute(dst.unsafe_mut_ref(0));
let mut y: *i32 = transmute(input.unsafe_ref(0));
do dst.len().times() {
for _ in range(0, dst.len()) {
*x = to_le32(*y);
x = x.offset(1);
y = y.offset(1);
@ -178,7 +178,7 @@ pub fn add_bytes_to_bits_tuple
pub trait FixedBuffer {
/// Input a vector of bytes. If the buffer becomes full, process it with the provided
/// function and then clear the buffer.
fn input(&mut self, input: &[u8], func: &fn(&[u8]));
fn input(&mut self, input: &[u8], func: |&[u8]|);
/// Reset the buffer.
fn reset(&mut self);
@ -206,7 +206,7 @@ pub trait FixedBuffer {
macro_rules! impl_fixed_buffer( ($name:ident, $size:expr) => (
impl FixedBuffer for $name {
fn input(&mut self, input: &[u8], func: &fn(&[u8])) {
fn input(&mut self, input: &[u8], func: |&[u8]|) {
let mut i = 0;
// FIXME: #6304 - This local variable shouldn't be necessary.
@ -326,11 +326,11 @@ pub trait StandardPadding {
/// and is guaranteed to have exactly rem remaining bytes when it returns. If there are not at
/// least rem bytes available, the buffer will be zero padded, processed, cleared, and then
/// filled with zeros again until only rem bytes are remaining.
fn standard_padding(&mut self, rem: uint, func: &fn(&[u8]));
fn standard_padding(&mut self, rem: uint, func: |&[u8]|);
}
impl <T: FixedBuffer> StandardPadding for T {
fn standard_padding(&mut self, rem: uint, func: &fn(&[u8])) {
fn standard_padding(&mut self, rem: uint, func: |&[u8]|) {
let size = self.size();
self.next(1)[0] = 128;

View File

@ -299,9 +299,9 @@ mod bench {
pub fn md5_10(bh: & mut BenchHarness) {
let mut sh = Md5::new();
let bytes = [1u8, ..10];
do bh.iter {
bh.iter(|| {
sh.input(bytes);
}
});
bh.bytes = bytes.len() as u64;
}
@ -309,9 +309,9 @@ mod bench {
pub fn md5_1k(bh: & mut BenchHarness) {
let mut sh = Md5::new();
let bytes = [1u8, ..1024];
do bh.iter {
bh.iter(|| {
sh.input(bytes);
}
});
bh.bytes = bytes.len() as u64;
}
@ -319,9 +319,9 @@ mod bench {
pub fn md5_64k(bh: & mut BenchHarness) {
let mut sh = Md5::new();
let bytes = [1u8, ..65536];
do bh.iter {
bh.iter(|| {
sh.input(bytes);
}
});
bh.bytes = bytes.len() as u64;
}
}