Skip to content

Commit d35124e

Browse files
committed
Make next_block_for return an option
1 parent 6bd7538 commit d35124e

2 files changed

Lines changed: 18 additions & 19 deletions

File tree

src/lib.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ impl<'conn> Notifications<'conn> {
277277
/// Returns the oldest pending notification
278278
///
279279
/// If no notifications are pending, blocks for up to `timeout` time, after
280-
/// which an `IoError` with the `TimedOut` kind is returned.
280+
/// which `None` is returned.
281281
///
282282
/// ## Example
283283
///
@@ -290,40 +290,40 @@ impl<'conn> Notifications<'conn> {
290290
///
291291
/// # let conn = postgres::Connection::connect("", &postgres::SslMode::None).unwrap();
292292
/// match conn.notifications().next_block_for(Duration::seconds(2)) {
293-
/// Ok(notification) => println!("notification: {}", notification.payload),
294-
/// Err(Error::IoError(IoError { kind: IoErrorKind::TimedOut, .. })) => {
295-
/// println!("Wait for notification timed out");
296-
/// }
297-
/// Err(e) => println!("Other error: {:?}", e),
293+
/// Some(Ok(notification)) => println!("notification: {}", notification.payload),
294+
/// Some(Err(e)) => println!("Error: {:?}", e),
295+
/// None => println!("Wait for notification timed out"),
298296
/// }
299297
/// ```
300-
pub fn next_block_for(&mut self, timeout: Duration) -> Result<Notification> {
298+
pub fn next_block_for(&mut self, timeout: Duration) -> Option<Result<Notification>> {
301299
if let Some(notification) = self.next() {
302-
return Ok(notification);
300+
return Some(Ok(notification));
303301
}
304302

305303
let mut conn = self.conn.conn.borrow_mut();
306-
check_desync!(conn);
304+
if conn.desynchronized {
305+
return Some(Err(Error::StreamDesynchronized));
306+
}
307307

308308
let end = SteadyTime::now() + timeout;
309309
loop {
310310
let timeout = max(Duration::zero(), end - SteadyTime::now()).num_milliseconds() as u64;
311311
conn.stream.set_read_timeout(Some(timeout));
312312
match conn.read_one_message() {
313313
Ok(Some(NotificationResponse { pid, channel, payload })) => {
314-
return Ok(Notification {
314+
return Some(Ok(Notification {
315315
pid: pid,
316316
channel: channel,
317317
payload: payload
318-
})
318+
}))
319319
}
320320
Ok(Some(_)) => unreachable!(),
321321
Ok(None) => {}
322-
Err(e @ IoError { kind: IoErrorKind::TimedOut, .. }) => {
322+
Err(IoError { kind: IoErrorKind::TimedOut, .. }) => {
323323
conn.desynchronized = false;
324-
return Err(Error::IoError(e));
324+
return None;
325325
}
326-
Err(e) => return Err(Error::IoError(e)),
326+
Err(e) => return Some(Err(Error::IoError(e))),
327327
}
328328
}
329329
}

tests/test.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ extern crate openssl;
77

88
use openssl::ssl::SslContext;
99
use openssl::ssl::SslMethod;
10-
use std::old_io::{IoError, IoErrorKind};
1110
use std::old_io::timer;
1211
use std::time::Duration;
1312
use std::thread::Thread;
@@ -649,7 +648,7 @@ fn test_notifications_next_block_for() {
649648
pid: 0,
650649
channel: "test_notifications_next_block_for".to_string(),
651650
payload: "foo".to_string()
652-
}, or_panic!(notifications.next_block_for(Duration::seconds(2))));
651+
}, or_panic!(notifications.next_block_for(Duration::seconds(2)).unwrap()));
653652
}
654653

655654
#[test]
@@ -665,9 +664,9 @@ fn test_notifications_next_block_for_timeout() {
665664

666665
let mut notifications = conn.notifications();
667666
match notifications.next_block_for(Duration::milliseconds(500)) {
668-
Err(Error::IoError(IoError { kind: IoErrorKind::TimedOut, .. })) => {}
669-
Err(e) => panic!("Unexpected error {:?}", e),
670-
Ok(_) => panic!("expected error"),
667+
None => {}
668+
Some(Err(e)) => panic!("Unexpected error {:?}", e),
669+
Some(Ok(_)) => panic!("expected error"),
671670
}
672671

673672
or_panic!(conn.execute("SELECT 1", &[]));

0 commit comments

Comments
 (0)