Skip to content

Commit 008f14b

Browse files
committed
Move notification stuff to its own module
1 parent ff4248c commit 008f14b

File tree

3 files changed

+86
-64
lines changed

3 files changed

+86
-64
lines changed

src/lib.rs

Lines changed: 7 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ use message::BackendMessage::*;
7878
use message::FrontendMessage::*;
7979
use message::{FrontendMessage, BackendMessage, RowDescriptionEntry};
8080
use message::{WriteMessage, ReadMessage};
81+
use notification::{Notifications, Notification};
8182
use rows::{Rows, LazyRows};
8283
use stmt::{Statement, Column};
8384
use types::{IsNull, Kind, Type, SessionInfo, Oid, Other};
@@ -97,6 +98,7 @@ pub mod io;
9798
pub mod rows;
9899
pub mod stmt;
99100
pub mod types;
101+
pub mod notification;
100102

101103
const TYPEINFO_QUERY: &'static str = "t";
102104

@@ -231,68 +233,6 @@ impl HandleNotice for LoggingNoticeHandler {
231233
}
232234
}
233235

234-
/// An asynchronous notification.
235-
#[derive(Clone, Debug)]
236-
pub struct Notification {
237-
/// The process ID of the notifying backend process.
238-
pub pid: u32,
239-
/// The name of the channel that the notify has been raised on.
240-
pub channel: String,
241-
/// The "payload" string passed from the notifying process.
242-
pub payload: String,
243-
}
244-
245-
/// An iterator over asynchronous notifications.
246-
pub struct Notifications<'conn> {
247-
conn: &'conn Connection
248-
}
249-
250-
impl<'a> fmt::Debug for Notifications<'a> {
251-
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
252-
DebugStruct::new(fmt, "Notifications")
253-
.field("pending", &self.conn.conn.borrow().notifications.len())
254-
.finish()
255-
}
256-
}
257-
258-
impl<'conn> Iterator for Notifications<'conn> {
259-
type Item = Notification;
260-
261-
/// Returns the oldest pending notification or `None` if there are none.
262-
///
263-
/// ## Note
264-
///
265-
/// `next` may return `Some` notification after returning `None` if a new
266-
/// notification was received.
267-
fn next(&mut self) -> Option<Notification> {
268-
self.conn.conn.borrow_mut().notifications.pop_front()
269-
}
270-
}
271-
272-
impl<'conn> Notifications<'conn> {
273-
/// Returns the oldest pending notification.
274-
///
275-
/// If no notifications are pending, blocks until one arrives.
276-
pub fn next_block(&mut self) -> Result<Notification> {
277-
if let Some(notification) = self.next() {
278-
return Ok(notification);
279-
}
280-
281-
let mut conn = self.conn.conn.borrow_mut();
282-
check_desync!(conn);
283-
match try!(conn.read_message_with_notification()) {
284-
NotificationResponse { pid, channel, payload } => {
285-
Ok(Notification {
286-
pid: pid,
287-
channel: channel,
288-
payload: payload
289-
})
290-
}
291-
_ => unreachable!()
292-
}
293-
}
294-
}
295-
296236
/// Contains information necessary to cancel queries for a session.
297237
#[derive(Copy, Clone, Debug)]
298238
pub struct CancelData {
@@ -947,7 +887,7 @@ impl Connection {
947887
///
948888
/// Use the `LISTEN` command to register this connection for notifications.
949889
pub fn notifications<'a>(&'a self) -> Notifications<'a> {
950-
Notifications { conn: self }
890+
Notifications::new(self)
951891
}
952892

953893
/// Creates a new prepared statement.
@@ -1444,3 +1384,7 @@ trait StatementInternals<'conn> {
14441384
trait ColumnNew {
14451385
fn new(name: String, type_: Type) -> Column;
14461386
}
1387+
1388+
trait NotificationsNew<'conn> {
1389+
fn new(conn: &'conn Connection) -> Notifications<'conn>;
1390+
}

src/notification.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//! Asynchronous notifications.
2+
3+
use debug_builders::DebugStruct;
4+
use std::fmt;
5+
6+
use {Result, Connection, NotificationsNew};
7+
use message::BackendMessage::NotificationResponse;
8+
9+
/// An asynchronous notification.
10+
#[derive(Clone, Debug)]
11+
pub struct Notification {
12+
/// The process ID of the notifying backend process.
13+
pub pid: u32,
14+
/// The name of the channel that the notify has been raised on.
15+
pub channel: String,
16+
/// The "payload" string passed from the notifying process.
17+
pub payload: String,
18+
}
19+
20+
/// An iterator over asynchronous notifications.
21+
pub struct Notifications<'conn> {
22+
conn: &'conn Connection
23+
}
24+
25+
impl<'a> fmt::Debug for Notifications<'a> {
26+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
27+
DebugStruct::new(fmt, "Notifications")
28+
.field("pending", &self.conn.conn.borrow().notifications.len())
29+
.finish()
30+
}
31+
}
32+
33+
impl<'conn> NotificationsNew<'conn> for Notifications<'conn> {
34+
fn new(conn: &'conn Connection) -> Notifications<'conn> {
35+
Notifications {
36+
conn: conn,
37+
}
38+
}
39+
}
40+
41+
impl<'conn> Iterator for Notifications<'conn> {
42+
type Item = Notification;
43+
44+
/// Returns the oldest pending notification or `None` if there are none.
45+
///
46+
/// ## Note
47+
///
48+
/// `next` may return `Some` notification after returning `None` if a new
49+
/// notification was received.
50+
fn next(&mut self) -> Option<Notification> {
51+
self.conn.conn.borrow_mut().notifications.pop_front()
52+
}
53+
}
54+
55+
impl<'conn> Notifications<'conn> {
56+
/// Returns the oldest pending notification.
57+
///
58+
/// If no notifications are pending, blocks until one arrives.
59+
pub fn next_block(&mut self) -> Result<Notification> {
60+
if let Some(notification) = self.next() {
61+
return Ok(notification);
62+
}
63+
64+
let mut conn = self.conn.conn.borrow_mut();
65+
check_desync!(conn);
66+
match try!(conn.read_message_with_notification()) {
67+
NotificationResponse { pid, channel, payload } => {
68+
Ok(Notification {
69+
pid: pid,
70+
channel: channel,
71+
payload: payload
72+
})
73+
}
74+
_ => unreachable!()
75+
}
76+
}
77+
}
78+

tests/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use std::io;
1111
use std::io::prelude::*;
1212

1313
use postgres::{HandleNotice,
14-
Notification,
1514
Connection,
1615
GenericConnection,
1716
SslMode,
@@ -27,6 +26,7 @@ use postgres::error::SqlState::{SyntaxError,
2726
CardinalityViolation};
2827
use postgres::error::ErrorPosition::Normal;
2928
use postgres::rows::RowIndex;
29+
use postgres::notification::Notification;
3030

3131
macro_rules! or_panic {
3232
($e:expr) => (

0 commit comments

Comments
 (0)