@@ -78,6 +78,7 @@ use message::BackendMessage::*;
7878use message:: FrontendMessage :: * ;
7979use message:: { FrontendMessage , BackendMessage , RowDescriptionEntry } ;
8080use message:: { WriteMessage , ReadMessage } ;
81+ use notification:: { Notifications , Notification } ;
8182use rows:: { Rows , LazyRows } ;
8283use stmt:: { Statement , Column } ;
8384use types:: { IsNull , Kind , Type , SessionInfo , Oid , Other } ;
@@ -97,6 +98,7 @@ pub mod io;
9798pub mod rows;
9899pub mod stmt;
99100pub mod types;
101+ pub mod notification;
100102
101103const 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 ) ]
298238pub 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> {
14441384trait ColumnNew {
14451385 fn new ( name : String , type_ : Type ) -> Column ;
14461386}
1387+
1388+ trait NotificationsNew < ' conn > {
1389+ fn new ( conn : & ' conn Connection ) -> Notifications < ' conn > ;
1390+ }
0 commit comments