Make next_block_for return an option

This commit is contained in:
Steven Fackler 2015-01-24 11:50:37 -08:00
parent 6bd753888b
commit d35124e214
2 changed files with 18 additions and 19 deletions

View File

@ -277,7 +277,7 @@ impl<'conn> Notifications<'conn> {
/// Returns the oldest pending notification
///
/// If no notifications are pending, blocks for up to `timeout` time, after
/// which an `IoError` with the `TimedOut` kind is returned.
/// which `None` is returned.
///
/// ## Example
///
@ -290,20 +290,20 @@ impl<'conn> Notifications<'conn> {
///
/// # let conn = postgres::Connection::connect("", &postgres::SslMode::None).unwrap();
/// match conn.notifications().next_block_for(Duration::seconds(2)) {
/// Ok(notification) => println!("notification: {}", notification.payload),
/// Err(Error::IoError(IoError { kind: IoErrorKind::TimedOut, .. })) => {
/// println!("Wait for notification timed out");
/// }
/// Err(e) => println!("Other error: {:?}", e),
/// Some(Ok(notification)) => println!("notification: {}", notification.payload),
/// Some(Err(e)) => println!("Error: {:?}", e),
/// None => println!("Wait for notification timed out"),
/// }
/// ```
pub fn next_block_for(&mut self, timeout: Duration) -> Result<Notification> {
pub fn next_block_for(&mut self, timeout: Duration) -> Option<Result<Notification>> {
if let Some(notification) = self.next() {
return Ok(notification);
return Some(Ok(notification));
}
let mut conn = self.conn.conn.borrow_mut();
check_desync!(conn);
if conn.desynchronized {
return Some(Err(Error::StreamDesynchronized));
}
let end = SteadyTime::now() + timeout;
loop {
@ -311,19 +311,19 @@ impl<'conn> Notifications<'conn> {
conn.stream.set_read_timeout(Some(timeout));
match conn.read_one_message() {
Ok(Some(NotificationResponse { pid, channel, payload })) => {
return Ok(Notification {
return Some(Ok(Notification {
pid: pid,
channel: channel,
payload: payload
})
}))
}
Ok(Some(_)) => unreachable!(),
Ok(None) => {}
Err(e @ IoError { kind: IoErrorKind::TimedOut, .. }) => {
Err(IoError { kind: IoErrorKind::TimedOut, .. }) => {
conn.desynchronized = false;
return Err(Error::IoError(e));
return None;
}
Err(e) => return Err(Error::IoError(e)),
Err(e) => return Some(Err(Error::IoError(e))),
}
}
}

View File

@ -7,7 +7,6 @@ extern crate openssl;
use openssl::ssl::SslContext;
use openssl::ssl::SslMethod;
use std::old_io::{IoError, IoErrorKind};
use std::old_io::timer;
use std::time::Duration;
use std::thread::Thread;
@ -649,7 +648,7 @@ fn test_notifications_next_block_for() {
pid: 0,
channel: "test_notifications_next_block_for".to_string(),
payload: "foo".to_string()
}, or_panic!(notifications.next_block_for(Duration::seconds(2))));
}, or_panic!(notifications.next_block_for(Duration::seconds(2)).unwrap()));
}
#[test]
@ -665,9 +664,9 @@ fn test_notifications_next_block_for_timeout() {
let mut notifications = conn.notifications();
match notifications.next_block_for(Duration::milliseconds(500)) {
Err(Error::IoError(IoError { kind: IoErrorKind::TimedOut, .. })) => {}
Err(e) => panic!("Unexpected error {:?}", e),
Ok(_) => panic!("expected error"),
None => {}
Some(Err(e)) => panic!("Unexpected error {:?}", e),
Some(Ok(_)) => panic!("expected error"),
}
or_panic!(conn.execute("SELECT 1", &[]));