From d35124e214705beb58fd205c9a784402301c9b3d Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sat, 24 Jan 2015 11:50:37 -0800 Subject: [PATCH] Make next_block_for return an option --- src/lib.rs | 28 ++++++++++++++-------------- tests/test.rs | 9 ++++----- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4fe4eacc..3547d533 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 { + pub fn next_block_for(&mut self, timeout: Duration) -> Option> { 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))), } } } diff --git a/tests/test.rs b/tests/test.rs index 7ad280ea..f43e96c5 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -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", &[]));