Make next_block_for return an option
This commit is contained in:
parent
6bd753888b
commit
d35124e214
28
src/lib.rs
28
src/lib.rs
@ -277,7 +277,7 @@ impl<'conn> Notifications<'conn> {
|
|||||||
/// Returns the oldest pending notification
|
/// Returns the oldest pending notification
|
||||||
///
|
///
|
||||||
/// If no notifications are pending, blocks for up to `timeout` time, after
|
/// 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
|
/// ## Example
|
||||||
///
|
///
|
||||||
@ -290,20 +290,20 @@ impl<'conn> Notifications<'conn> {
|
|||||||
///
|
///
|
||||||
/// # let conn = postgres::Connection::connect("", &postgres::SslMode::None).unwrap();
|
/// # let conn = postgres::Connection::connect("", &postgres::SslMode::None).unwrap();
|
||||||
/// match conn.notifications().next_block_for(Duration::seconds(2)) {
|
/// match conn.notifications().next_block_for(Duration::seconds(2)) {
|
||||||
/// Ok(notification) => println!("notification: {}", notification.payload),
|
/// Some(Ok(notification)) => println!("notification: {}", notification.payload),
|
||||||
/// Err(Error::IoError(IoError { kind: IoErrorKind::TimedOut, .. })) => {
|
/// Some(Err(e)) => println!("Error: {:?}", e),
|
||||||
/// println!("Wait for notification timed out");
|
/// None => println!("Wait for notification timed out"),
|
||||||
/// }
|
|
||||||
/// Err(e) => println!("Other error: {:?}", e),
|
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
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() {
|
if let Some(notification) = self.next() {
|
||||||
return Ok(notification);
|
return Some(Ok(notification));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut conn = self.conn.conn.borrow_mut();
|
let mut conn = self.conn.conn.borrow_mut();
|
||||||
check_desync!(conn);
|
if conn.desynchronized {
|
||||||
|
return Some(Err(Error::StreamDesynchronized));
|
||||||
|
}
|
||||||
|
|
||||||
let end = SteadyTime::now() + timeout;
|
let end = SteadyTime::now() + timeout;
|
||||||
loop {
|
loop {
|
||||||
@ -311,19 +311,19 @@ impl<'conn> Notifications<'conn> {
|
|||||||
conn.stream.set_read_timeout(Some(timeout));
|
conn.stream.set_read_timeout(Some(timeout));
|
||||||
match conn.read_one_message() {
|
match conn.read_one_message() {
|
||||||
Ok(Some(NotificationResponse { pid, channel, payload })) => {
|
Ok(Some(NotificationResponse { pid, channel, payload })) => {
|
||||||
return Ok(Notification {
|
return Some(Ok(Notification {
|
||||||
pid: pid,
|
pid: pid,
|
||||||
channel: channel,
|
channel: channel,
|
||||||
payload: payload
|
payload: payload
|
||||||
})
|
}))
|
||||||
}
|
}
|
||||||
Ok(Some(_)) => unreachable!(),
|
Ok(Some(_)) => unreachable!(),
|
||||||
Ok(None) => {}
|
Ok(None) => {}
|
||||||
Err(e @ IoError { kind: IoErrorKind::TimedOut, .. }) => {
|
Err(IoError { kind: IoErrorKind::TimedOut, .. }) => {
|
||||||
conn.desynchronized = false;
|
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))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,6 @@ extern crate openssl;
|
|||||||
|
|
||||||
use openssl::ssl::SslContext;
|
use openssl::ssl::SslContext;
|
||||||
use openssl::ssl::SslMethod;
|
use openssl::ssl::SslMethod;
|
||||||
use std::old_io::{IoError, IoErrorKind};
|
|
||||||
use std::old_io::timer;
|
use std::old_io::timer;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use std::thread::Thread;
|
use std::thread::Thread;
|
||||||
@ -649,7 +648,7 @@ fn test_notifications_next_block_for() {
|
|||||||
pid: 0,
|
pid: 0,
|
||||||
channel: "test_notifications_next_block_for".to_string(),
|
channel: "test_notifications_next_block_for".to_string(),
|
||||||
payload: "foo".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]
|
#[test]
|
||||||
@ -665,9 +664,9 @@ fn test_notifications_next_block_for_timeout() {
|
|||||||
|
|
||||||
let mut notifications = conn.notifications();
|
let mut notifications = conn.notifications();
|
||||||
match notifications.next_block_for(Duration::milliseconds(500)) {
|
match notifications.next_block_for(Duration::milliseconds(500)) {
|
||||||
Err(Error::IoError(IoError { kind: IoErrorKind::TimedOut, .. })) => {}
|
None => {}
|
||||||
Err(e) => panic!("Unexpected error {:?}", e),
|
Some(Err(e)) => panic!("Unexpected error {:?}", e),
|
||||||
Ok(_) => panic!("expected error"),
|
Some(Ok(_)) => panic!("expected error"),
|
||||||
}
|
}
|
||||||
|
|
||||||
or_panic!(conn.execute("SELECT 1", &[]));
|
or_panic!(conn.execute("SELECT 1", &[]));
|
||||||
|
Loading…
Reference in New Issue
Block a user