Skip to content

Commit 5bb1ec2

Browse files
committed
Remove read_message macros
Workaround for rust-lang/rust#9049 but probably the right thing to do in any case.
1 parent 7971e86 commit 5bb1ec2

1 file changed

Lines changed: 57 additions & 77 deletions

File tree

src/lib.rs

Lines changed: 57 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -24,45 +24,6 @@ use types::{PostgresType, ToSql, FromSql};
2424
mod message;
2525
mod types;
2626

27-
macro_rules! match_read_message(
28-
($conn:expr, { $($($p:pat)|+ => $e:expr),+ }) => (
29-
match {
30-
let ref conn = $conn;
31-
let resp;
32-
loop {
33-
let msg = conn.read_message();
34-
info2!("{}", msg.to_str());
35-
match msg {
36-
NoticeResponse { fields } =>
37-
handle_notice_response(fields),
38-
ParameterStatus { parameter, value } =>
39-
info!("Parameter %s = %s", parameter, value),
40-
msg => {
41-
resp = msg;
42-
break;
43-
}
44-
}
45-
}
46-
resp
47-
} {
48-
$(
49-
$($p)|+ => $e
50-
),+
51-
}
52-
)
53-
)
54-
55-
macro_rules! match_read_message_or_fail(
56-
($conn:expr, { $($($p:pat)|+ => $e:expr),+ }) => (
57-
match_read_message!($conn, {
58-
$(
59-
$($p)|+ => $e
60-
),+ ,
61-
resp => fail2!("Bad response: {}", resp.to_str())
62-
})
63-
)
64-
)
65-
6627
fn handle_notice_response(fields: ~[(u8, ~str)]) {
6728
let err = PostgresDbError::new(fields);
6829
info2!("{}: {}", err.severity, err.message);
@@ -179,7 +140,6 @@ impl PostgresConnection {
179140
};
180141

181142
let conn = PostgresConnection {
182-
// Need to figure out what to do about unwrap here
183143
stream: Cell::new(stream),
184144
next_stmt_id: Cell::new(0)
185145
};
@@ -201,10 +161,11 @@ impl PostgresConnection {
201161
}
202162

203163
loop {
204-
match_read_message_or_fail!(conn, {
164+
match conn.read_message() {
205165
BackendKeyData {_} => (),
206-
ReadyForQuery {_} => break
207-
})
166+
ReadyForQuery {_} => break,
167+
_ => fail!()
168+
}
208169
}
209170

210171
Ok(conn)
@@ -250,21 +211,31 @@ impl PostgresConnection {
250211
}
251212

252213
fn read_message(&self) -> BackendMessage {
253-
do self.stream.with_mut_ref |s| {
254-
s.read_message()
214+
loop {
215+
let msg = do self.stream.with_mut_ref |s| {
216+
s.read_message()
217+
};
218+
219+
match msg {
220+
NoticeResponse { fields } =>
221+
handle_notice_response(fields),
222+
ParameterStatus { parameter, value } =>
223+
info!("Parameter %s = %s", parameter, value),
224+
msg => return msg
225+
}
255226
}
256227
}
257228

258229
fn handle_auth(&self, user: UserInfo) -> Option<PostgresConnectError> {
259-
match_read_message_or_fail!(self, {
230+
match self.read_message() {
260231
AuthenticationOk => return None,
261232
AuthenticationCleartextPassword => {
262233
let pass = match user.pass {
263234
Some(pass) => pass,
264235
None => return Some(MissingPassword)
265236
};
266237
self.write_message(&PasswordMessage { password: pass });
267-
},
238+
}
268239
AuthenticationMD5Password { salt } => {
269240
let UserInfo { user, pass } = user;
270241
let pass = match pass {
@@ -283,13 +254,15 @@ impl PostgresConnection {
283254
password: output.as_slice()
284255
});
285256
}
286-
})
257+
_ => fail!()
258+
}
287259

288-
match_read_message_or_fail!(self, {
260+
match self.read_message() {
289261
AuthenticationOk => None,
290262
ErrorResponse { fields } =>
291-
Some(DbError(PostgresDbError::new(fields)))
292-
})
263+
Some(DbError(PostgresDbError::new(fields))),
264+
_ => fail!()
265+
}
293266
}
294267

295268
pub fn prepare<'a>(&'a self, query: &str) -> NormalPostgresStatement<'a> {
@@ -319,21 +292,23 @@ impl PostgresConnection {
319292
},
320293
&Sync]);
321294

322-
match_read_message_or_fail!(self, {
295+
match self.read_message() {
323296
ParseComplete => (),
324297
ErrorResponse { fields } => {
325298
self.wait_for_ready();
326299
return Err(PostgresDbError::new(fields));
327300
}
328-
})
301+
_ => fail!()
302+
}
329303

330-
let param_types = match_read_message_or_fail!(self, {
304+
let param_types = match self.read_message() {
331305
ParameterDescription { types } =>
332306
types.iter().map(|ty| { PostgresType::from_oid(*ty) })
333-
.collect()
334-
});
307+
.collect(),
308+
_ => fail!()
309+
};
335310

336-
let result_desc = match_read_message_or_fail!(self, {
311+
let result_desc = match self.read_message() {
337312
RowDescription { descriptions } => {
338313
let mut res: ~[ResultDescription] = descriptions
339314
.move_rev_iter().map(|desc| {
@@ -342,8 +317,9 @@ impl PostgresConnection {
342317
res.reverse();
343318
res
344319
},
345-
NoData => ~[]
346-
});
320+
NoData => ~[],
321+
_ => fail!()
322+
};
347323

348324
self.wait_for_ready();
349325

@@ -394,19 +370,20 @@ impl PostgresConnection {
394370
self.write_message(&Query { query: query });
395371

396372
loop {
397-
match_read_message!(self, {
373+
match self.read_message() {
398374
ReadyForQuery {_} => break,
399375
ErrorResponse { fields } =>
400376
fail2!("Error: {}", PostgresDbError::new(fields).to_str()),
401377
_ => ()
402-
})
378+
}
403379
}
404380
}
405381

406382
fn wait_for_ready(&self) {
407-
match_read_message_or_fail!(self, {
408-
ReadyForQuery {_} => ()
409-
})
383+
match self.read_message() {
384+
ReadyForQuery {_} => (),
385+
_ => fail!()
386+
}
410387
}
411388
}
412389

@@ -521,10 +498,10 @@ impl<'self> Drop for NormalPostgresStatement<'self> {
521498
},
522499
&Sync]);
523500
loop {
524-
match_read_message!(self.conn, {
501+
match self.conn.read_message() {
525502
ReadyForQuery {_} => break,
526503
_ => ()
527-
})
504+
}
528505
}
529506
}
530507
}
@@ -559,13 +536,14 @@ impl<'self> NormalPostgresStatement<'self> {
559536
},
560537
&Sync]);
561538

562-
match_read_message_or_fail!(self.conn, {
539+
match self.conn.read_message() {
563540
BindComplete => None,
564541
ErrorResponse { fields } => {
565542
self.conn.wait_for_ready();
566543
Some(PostgresDbError::new(fields))
567544
}
568-
})
545+
_ => fail!()
546+
}
569547
}
570548

571549
fn lazy_query<'a>(&'a self, row_limit: uint, params: &[&ToSql])
@@ -629,26 +607,27 @@ impl<'self> PostgresStatement for NormalPostgresStatement<'self> {
629607

630608
let num;
631609
loop {
632-
match_read_message_or_fail!(self.conn, {
610+
match self.conn.read_message() {
633611
CommandComplete { tag } => {
634612
let s = tag.split_iter(' ').last().unwrap();
635613
num = match FromStr::from_str(s) {
636614
None => 0,
637615
Some(n) => n
638616
};
639617
break;
640-
},
618+
}
641619
DataRow {_} => (),
642620
EmptyQueryResponse => {
643621
num = 0;
644622
break;
645-
},
623+
}
646624
NoticeResponse {_} => (),
647625
ErrorResponse { fields } => {
648626
self.conn.wait_for_ready();
649627
return Err(PostgresDbError::new(fields));
650628
}
651-
})
629+
_ => fail!()
630+
}
652631
}
653632
self.conn.wait_for_ready();
654633

@@ -738,10 +717,10 @@ impl<'self> Drop for PostgresResult<'self> {
738717
},
739718
&Sync]);
740719
loop {
741-
match_read_message!(self.stmt.conn, {
720+
match self.stmt.conn.read_message() {
742721
ReadyForQuery {_} => break,
743722
_ => ()
744-
})
723+
}
745724
}
746725
}
747726
}
@@ -750,7 +729,7 @@ impl<'self> Drop for PostgresResult<'self> {
750729
impl<'self> PostgresResult<'self> {
751730
fn read_rows(&mut self) {
752731
loop {
753-
match_read_message_or_fail!(self.stmt.conn, {
732+
match self.stmt.conn.read_message() {
754733
EmptyQueryResponse |
755734
CommandComplete {_} => {
756735
self.more_rows = false;
@@ -760,8 +739,9 @@ impl<'self> PostgresResult<'self> {
760739
self.more_rows = true;
761740
break;
762741
},
763-
DataRow { row } => self.data.push_back(row)
764-
})
742+
DataRow { row } => self.data.push_back(row),
743+
_ => fail!()
744+
}
765745
}
766746
self.stmt.conn.wait_for_ready();
767747
}

0 commit comments

Comments
 (0)