Skip to content

Commit 99d570f

Browse files
committed
Fixes for Rust updates
No more hacky macro module!
1 parent 48dfb4d commit 99d570f

7 files changed

Lines changed: 66 additions & 85 deletions

File tree

error.rs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,23 @@
33
use std::hashmap::HashMap;
44

55
use super::ssl::error::SslError;
6-
use self::hack::PostgresSqlState;
76

87
macro_rules! make_errors(
98
($($code:pat => $error:ident),+) => (
10-
// TODO: Get rid of this module when mozilla/rust#4375 is fixed
11-
/// A module to get around issues with macro expansion
12-
pub mod hack {
13-
/// SQLSTATE error codes
14-
#[deriving(ToStr, Eq)]
15-
#[allow(missing_doc)]
16-
pub enum PostgresSqlState {
17-
$($error,)+
18-
UnknownSqlState(~str)
19-
}
9+
/// SQLSTATE error codes
10+
#[deriving(ToStr, Eq)]
11+
#[allow(missing_doc)]
12+
pub enum PostgresSqlState {
13+
$($error,)+
14+
UnknownSqlState(~str)
15+
}
2016

21-
impl FromStr for PostgresSqlState {
22-
fn from_str(s: &str) -> Option<PostgresSqlState> {
23-
Some(match s {
24-
$($code => $error,)+
25-
state => UnknownSqlState(state.to_owned())
26-
})
27-
}
17+
impl FromStr for PostgresSqlState {
18+
fn from_str(s: &str) -> Option<PostgresSqlState> {
19+
Some(match s {
20+
$($code => $error,)+
21+
state => UnknownSqlState(state.to_owned())
22+
})
2823
}
2924
}
3025
)

lib.rs

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,7 @@ impl<'conn > Iterator<PostgresNotification> for
184184
/// `next` may return `Some` notification after returning `None` if a new
185185
/// notification was received.
186186
fn next(&mut self) -> Option<PostgresNotification> {
187-
do self.conn.conn.with_mut |conn| {
188-
conn.notifications.pop_front()
189-
}
187+
self.conn.conn.with_mut(|conn| { conn.notifications.pop_front() })
190188
}
191189
}
192190

@@ -235,18 +233,18 @@ pub fn cancel_query(url: &str, ssl: &SslMode, data: PostgresCancelData)
235233

236234
fn open_socket(host: &str, port: Port)
237235
-> Result<TcpStream, PostgresConnectError> {
238-
let addrs = do io_error::cond.trap(|_| {}).inside {
236+
let addrs = io_error::cond.trap(|_| {}).inside(|| {
239237
net::get_host_addresses(host)
240-
};
238+
});
241239
let addrs = match addrs {
242240
Some(addrs) => addrs,
243241
None => return Err(DnsError)
244242
};
245243

246244
for addr in addrs.iter() {
247-
let socket = do io_error::cond.trap(|_| {}).inside {
245+
let socket = io_error::cond.trap(|_| {}).inside(|| {
248246
TcpStream::connect(SocketAddr { ip: *addr, port: port })
249-
};
247+
});
250248
match socket {
251249
Some(socket) => return Ok(socket),
252250
None => {}
@@ -326,9 +324,9 @@ struct InnerPostgresConnection {
326324

327325
impl Drop for InnerPostgresConnection {
328326
fn drop(&mut self) {
329-
do io_error::cond.trap(|_| {}).inside {
327+
io_error::cond.trap(|_| {}).inside(|| {
330328
self.write_messages([Terminate]);
331-
}
329+
})
332330
}
333331
}
334332

@@ -566,11 +564,11 @@ impl PostgresConnection {
566564
/// username if not specified.
567565
pub fn try_connect(url: &str, ssl: &SslMode)
568566
-> Result<PostgresConnection, PostgresConnectError> {
569-
do InnerPostgresConnection::try_connect(url, ssl).map |conn| {
567+
InnerPostgresConnection::try_connect(url, ssl).map(|conn| {
570568
PostgresConnection {
571569
conn: RefCell::new(conn)
572570
}
573-
}
571+
})
574572
}
575573

576574
/// A convenience wrapper around `try_connect`.
@@ -611,9 +609,7 @@ impl PostgresConnection {
611609
/// not outlive that connection.
612610
pub fn try_prepare<'a>(&'a self, query: &str)
613611
-> Result<NormalPostgresStatement<'a>, PostgresDbError> {
614-
do self.conn.with_mut |conn| {
615-
conn.try_prepare(query, self)
616-
}
612+
self.conn.with_mut(|conn| conn.try_prepare(query, self))
617613
}
618614

619615
/// A convenience wrapper around `try_prepare`.
@@ -653,9 +649,7 @@ impl PostgresConnection {
653649
/// On success, returns the number of rows modified or 0 if not applicable.
654650
pub fn try_update(&self, query: &str, params: &[&ToSql])
655651
-> Result<uint, PostgresDbError> {
656-
do self.try_prepare(query).and_then |stmt| {
657-
stmt.try_update(params)
658-
}
652+
self.try_prepare(query).and_then(|stmt| stmt.try_update(params))
659653
}
660654

661655
/// A convenience wrapper around `try_update`.
@@ -676,13 +670,11 @@ impl PostgresConnection {
676670
/// Used with the `cancel_query` function. The object returned can be used
677671
/// to cancel any query executed by the connection it was created from.
678672
pub fn cancel_data(&self) -> PostgresCancelData {
679-
do self.conn.with |conn| {
680-
conn.cancel_data
681-
}
673+
self.conn.with(|conn| conn.cancel_data)
682674
}
683675

684676
fn quick_query(&self, query: &str) {
685-
do self.conn.with_mut |conn| {
677+
self.conn.with_mut(|conn| {
686678
conn.write_messages([Query { query: query }]);
687679

688680
loop {
@@ -694,25 +686,19 @@ impl PostgresConnection {
694686
_ => {}
695687
}
696688
}
697-
}
689+
})
698690
}
699691

700692
fn wait_for_ready(&self) {
701-
do self.conn.with_mut |conn| {
702-
conn.wait_for_ready()
703-
}
693+
self.conn.with_mut(|conn| conn.wait_for_ready())
704694
}
705695

706696
fn read_message(&self) -> BackendMessage {
707-
do self.conn.with_mut |conn| {
708-
conn.read_message()
709-
}
697+
self.conn.with_mut(|conn| conn.read_message())
710698
}
711699

712700
fn write_messages(&self, messages: &[FrontendMessage]) {
713-
do self.conn.with_mut |conn| {
714-
conn.write_messages(messages)
715-
}
701+
self.conn.with_mut(|conn| conn.write_messages(messages))
716702
}
717703
}
718704

@@ -736,7 +722,7 @@ pub struct PostgresTransaction<'conn> {
736722
#[unsafe_destructor]
737723
impl<'conn> Drop for PostgresTransaction<'conn> {
738724
fn drop(&mut self) {
739-
do io_error::cond.trap(|_| {}).inside {
725+
io_error::cond.trap(|_| {}).inside(|| {
740726
if task::failing() || !self.commit.with(|x| *x) {
741727
if self.nested {
742728
self.conn.quick_query("ROLLBACK TO sp");
@@ -750,19 +736,19 @@ impl<'conn> Drop for PostgresTransaction<'conn> {
750736
self.conn.quick_query("COMMIT");
751737
}
752738
}
753-
}
739+
})
754740
}
755741
}
756742

757743
impl<'conn> PostgresTransaction<'conn> {
758744
/// Like `PostgresConnection::try_prepare`.
759745
pub fn try_prepare<'a>(&'a self, query: &str)
760746
-> Result<TransactionalPostgresStatement<'a>, PostgresDbError> {
761-
do self.conn.try_prepare(query).map |stmt| {
747+
self.conn.try_prepare(query).map(|stmt| {
762748
TransactionalPostgresStatement {
763749
stmt: stmt
764750
}
765-
}
751+
})
766752
}
767753

768754
/// Like `PostgresConnection::prepare`.
@@ -881,7 +867,7 @@ pub struct NormalPostgresStatement<'conn> {
881867
#[unsafe_destructor]
882868
impl<'conn> Drop for NormalPostgresStatement<'conn> {
883869
fn drop(&mut self) {
884-
do io_error::cond.trap(|_| {}).inside {
870+
io_error::cond.trap(|_| {}).inside(|| {
885871
self.conn.write_messages([
886872
Close {
887873
variant: 'S' as u8,
@@ -894,7 +880,7 @@ impl<'conn> Drop for NormalPostgresStatement<'conn> {
894880
_ => {}
895881
}
896882
}
897-
}
883+
})
898884
}
899885
}
900886

@@ -992,7 +978,7 @@ impl<'conn> PostgresStatement for NormalPostgresStatement<'conn> {
992978
return Err(PostgresDbError::new(fields));
993979
}
994980
CommandComplete { tag } => {
995-
let s = tag.split_iter(' ').last().unwrap();
981+
let s = tag.split(' ').last().unwrap();
996982
num = match FromStr::from_str(s) {
997983
None => 0,
998984
Some(n) => n
@@ -1107,7 +1093,7 @@ pub struct PostgresResult<'stmt> {
11071093
#[unsafe_destructor]
11081094
impl<'stmt> Drop for PostgresResult<'stmt> {
11091095
fn drop(&mut self) {
1110-
do io_error::cond.trap(|_| {}).inside {
1096+
io_error::cond.trap(|_| {}).inside(|| {
11111097
self.stmt.conn.write_messages([
11121098
Close {
11131099
variant: 'P' as u8,
@@ -1120,7 +1106,7 @@ impl<'stmt> Drop for PostgresResult<'stmt> {
11201106
_ => {}
11211107
}
11221108
}
1123-
}
1109+
})
11241110
}
11251111
}
11261112

@@ -1161,12 +1147,12 @@ impl<'stmt> Iterator<PostgresRow<'stmt>> for PostgresResult<'stmt> {
11611147
self.execute();
11621148
}
11631149

1164-
do self.data.pop_front().map |row| {
1150+
self.data.pop_front().map(|row| {
11651151
PostgresRow {
11661152
stmt: self.stmt,
11671153
data: row
11681154
}
1169-
}
1155+
})
11701156
}
11711157
}
11721158

message.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ fn read_data_row(buf: &mut MemReader) -> BackendMessage {
326326
let len = buf.read_be_i16() as uint;
327327
let mut values = vec::with_capacity(len);
328328

329-
do len.times() {
329+
for _ in range(0, len) {
330330
let val = match buf.read_be_i32() {
331331
-1 => None,
332332
len => Some(buf.read_bytes(len as uint))
@@ -354,7 +354,7 @@ fn read_parameter_description(buf: &mut MemReader) -> BackendMessage {
354354
let len = buf.read_be_i16() as uint;
355355
let mut types = vec::with_capacity(len);
356356

357-
do len.times() {
357+
for _ in range(0, len) {
358358
types.push(buf.read_be_i32());
359359
}
360360

@@ -365,7 +365,7 @@ fn read_row_description(buf: &mut MemReader) -> BackendMessage {
365365
let len = buf.read_be_i16() as uint;
366366
let mut types = vec::with_capacity(len);
367367

368-
do len.times() {
368+
for _ in range(0, len) {
369369
types.push(RowDescriptionEntry {
370370
name: buf.read_string(),
371371
table_oid: buf.read_be_i32(),

pool.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,13 @@ impl PostgresConnectionPool {
8080
/// If all connections are in use, blocks until one becomes available.
8181
pub fn get_connection(&self) -> PooledPostgresConnection {
8282
let conn = unsafe {
83-
do self.pool.unsafe_access_cond |pool, cvar| {
83+
self.pool.unsafe_access_cond(|pool, cvar| {
8484
while pool.pool.is_empty() {
8585
cvar.wait();
8686
}
8787

8888
pool.pool.pop()
89-
}
89+
})
9090
};
9191

9292
PooledPostgresConnection {
@@ -109,9 +109,9 @@ pub struct PooledPostgresConnection {
109109
impl Drop for PooledPostgresConnection {
110110
fn drop(&mut self) {
111111
unsafe {
112-
do self.pool.pool.unsafe_access |pool| {
112+
self.pool.pool.unsafe_access(|pool| {
113113
pool.pool.push(self.conn.take_unwrap());
114-
}
114+
})
115115
}
116116
}
117117
}

test.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ use lib::error::{DbError,
2626
DnsError,
2727
MissingPassword,
2828
Position,
29-
PostgresDbError};
30-
use lib::error::hack::{SyntaxError,
31-
InvalidPassword,
32-
QueryCanceled,
33-
InvalidCatalogName};
29+
PostgresDbError,
30+
SyntaxError,
31+
InvalidPassword,
32+
QueryCanceled,
33+
InvalidCatalogName};
3434
use lib::types::{ToSql, FromSql, PgInt4, PgVarchar};
3535
use lib::types::range::{Range, Inclusive, Exclusive, RangeBound};
3636
use lib::pool::PostgresConnectionPool;

util/cryptoutil.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub fn read_u64v_be(dst: &mut[u64], input: &[u8]) {
5656
unsafe {
5757
let mut x: *mut i64 = transmute(dst.unsafe_mut_ref(0));
5858
let mut y: *i64 = transmute(input.unsafe_ref(0));
59-
do dst.len().times() {
59+
for _ in range(0, dst.len()) {
6060
*x = to_be64(*y);
6161
x = x.offset(1);
6262
y = y.offset(1);
@@ -72,7 +72,7 @@ pub fn read_u32v_be(dst: &mut[u32], input: &[u8]) {
7272
unsafe {
7373
let mut x: *mut i32 = transmute(dst.unsafe_mut_ref(0));
7474
let mut y: *i32 = transmute(input.unsafe_ref(0));
75-
do dst.len().times() {
75+
for _ in range(0, dst.len()) {
7676
*x = to_be32(*y);
7777
x = x.offset(1);
7878
y = y.offset(1);
@@ -88,7 +88,7 @@ pub fn read_u32v_le(dst: &mut[u32], input: &[u8]) {
8888
unsafe {
8989
let mut x: *mut i32 = transmute(dst.unsafe_mut_ref(0));
9090
let mut y: *i32 = transmute(input.unsafe_ref(0));
91-
do dst.len().times() {
91+
for _ in range(0, dst.len()) {
9292
*x = to_le32(*y);
9393
x = x.offset(1);
9494
y = y.offset(1);
@@ -178,7 +178,7 @@ pub fn add_bytes_to_bits_tuple
178178
pub trait FixedBuffer {
179179
/// Input a vector of bytes. If the buffer becomes full, process it with the provided
180180
/// function and then clear the buffer.
181-
fn input(&mut self, input: &[u8], func: &fn(&[u8]));
181+
fn input(&mut self, input: &[u8], func: |&[u8]|);
182182

183183
/// Reset the buffer.
184184
fn reset(&mut self);
@@ -206,7 +206,7 @@ pub trait FixedBuffer {
206206

207207
macro_rules! impl_fixed_buffer( ($name:ident, $size:expr) => (
208208
impl FixedBuffer for $name {
209-
fn input(&mut self, input: &[u8], func: &fn(&[u8])) {
209+
fn input(&mut self, input: &[u8], func: |&[u8]|) {
210210
let mut i = 0;
211211

212212
// FIXME: #6304 - This local variable shouldn't be necessary.
@@ -326,11 +326,11 @@ pub trait StandardPadding {
326326
/// and is guaranteed to have exactly rem remaining bytes when it returns. If there are not at
327327
/// least rem bytes available, the buffer will be zero padded, processed, cleared, and then
328328
/// filled with zeros again until only rem bytes are remaining.
329-
fn standard_padding(&mut self, rem: uint, func: &fn(&[u8]));
329+
fn standard_padding(&mut self, rem: uint, func: |&[u8]|);
330330
}
331331

332332
impl <T: FixedBuffer> StandardPadding for T {
333-
fn standard_padding(&mut self, rem: uint, func: &fn(&[u8])) {
333+
fn standard_padding(&mut self, rem: uint, func: |&[u8]|) {
334334
let size = self.size();
335335

336336
self.next(1)[0] = 128;

0 commit comments

Comments
 (0)