1
1
//! Asynchronous notifications.
2
2
3
+ use fallible_iterator:: { FallibleIterator , IntoFallibleIterator } ;
3
4
use std:: fmt;
4
5
use std:: time:: Duration ;
5
6
@@ -42,7 +43,7 @@ impl<'conn> Notifications<'conn> {
42
43
self . len ( ) == 0
43
44
}
44
45
45
- /// Returns an iterator over pending notifications.
46
+ /// Returns a fallible iterator over pending notifications.
46
47
///
47
48
/// # Note
48
49
///
@@ -52,16 +53,16 @@ impl<'conn> Notifications<'conn> {
52
53
Iter { conn : self . conn }
53
54
}
54
55
55
- /// Returns an iterator over notifications that blocks until one is
56
+ /// Returns a fallible iterator over notifications that blocks until one is
56
57
/// received if none are pending.
57
58
///
58
59
/// The iterator will never return `None`.
59
60
pub fn blocking_iter < ' a > ( & ' a self ) -> BlockingIter < ' a > {
60
61
BlockingIter { conn : self . conn }
61
62
}
62
63
63
- /// Returns an iterator over notifications that blocks for a limited time
64
- /// waiting to receive one if none are pending.
64
+ /// Returns a fallible iterator over notifications that blocks for a limited
65
+ /// time waiting to receive one if none are pending.
65
66
///
66
67
/// # Note
67
68
///
@@ -75,11 +76,12 @@ impl<'conn> Notifications<'conn> {
75
76
}
76
77
}
77
78
78
- impl < ' a , ' conn > IntoIterator for & ' a Notifications < ' conn > {
79
- type Item = Result < Notification > ;
79
+ impl < ' a , ' conn > IntoFallibleIterator for & ' a Notifications < ' conn > {
80
+ type Item = Notification ;
81
+ type Error = Error ;
80
82
type IntoIter = Iter < ' a > ;
81
83
82
- fn into_iter ( self ) -> Iter < ' a > {
84
+ fn into_fallible_iterator ( self ) -> Iter < ' a > {
83
85
self . iter ( )
84
86
}
85
87
}
@@ -90,35 +92,36 @@ impl<'conn> NotificationsNew<'conn> for Notifications<'conn> {
90
92
}
91
93
}
92
94
93
- /// An iterator over pending notifications.
95
+ /// A fallible iterator over pending notifications.
94
96
pub struct Iter < ' a > {
95
97
conn : & ' a Connection ,
96
98
}
97
99
98
- impl < ' a > Iterator for Iter < ' a > {
99
- type Item = Result < Notification > ;
100
+ impl < ' a > FallibleIterator for Iter < ' a > {
101
+ type Item = Notification ;
102
+ type Error = Error ;
100
103
101
- fn next ( & mut self ) -> Option < Result < Notification > > {
104
+ fn next ( & mut self ) -> Result < Option < Notification > > {
102
105
let mut conn = self . conn . conn . borrow_mut ( ) ;
103
106
104
107
if let Some ( notification) = conn. notifications . pop_front ( ) {
105
- return Some ( Ok ( notification) ) ;
108
+ return Ok ( Some ( notification) ) ;
106
109
}
107
110
108
111
if conn. is_desynchronized ( ) {
109
- return Some ( Err ( Error :: Io ( desynchronized ( ) ) ) ) ;
112
+ return Err ( Error :: Io ( desynchronized ( ) ) ) ;
110
113
}
111
114
112
115
match conn. read_message_with_notification_nonblocking ( ) {
113
116
Ok ( Some ( Backend :: NotificationResponse { process_id, channel, payload } ) ) => {
114
- Some ( Ok ( Notification {
117
+ Ok ( Some ( Notification {
115
118
process_id : process_id,
116
119
channel : channel,
117
120
payload : payload,
118
121
} ) )
119
122
}
120
- Ok ( None ) => None ,
121
- Err ( err) => Some ( Err ( Error :: Io ( err) ) ) ,
123
+ Ok ( None ) => Ok ( None ) ,
124
+ Err ( err) => Err ( Error :: Io ( err) ) ,
122
125
_ => unreachable ! ( ) ,
123
126
}
124
127
}
@@ -133,36 +136,33 @@ pub struct BlockingIter<'a> {
133
136
conn : & ' a Connection ,
134
137
}
135
138
136
- impl < ' a > Iterator for BlockingIter < ' a > {
137
- type Item = Result < Notification > ;
139
+ impl < ' a > FallibleIterator for BlockingIter < ' a > {
140
+ type Item = Notification ;
141
+ type Error = Error ;
138
142
139
- fn next ( & mut self ) -> Option < Result < Notification > > {
143
+ fn next ( & mut self ) -> Result < Option < Notification > > {
140
144
let mut conn = self . conn . conn . borrow_mut ( ) ;
141
145
142
146
if let Some ( notification) = conn. notifications . pop_front ( ) {
143
- return Some ( Ok ( notification) ) ;
147
+ return Ok ( Some ( notification) ) ;
144
148
}
145
149
146
150
if conn. is_desynchronized ( ) {
147
- return Some ( Err ( Error :: Io ( desynchronized ( ) ) ) ) ;
151
+ return Err ( Error :: Io ( desynchronized ( ) ) ) ;
148
152
}
149
153
150
154
match conn. read_message_with_notification ( ) {
151
155
Ok ( Backend :: NotificationResponse { process_id, channel, payload } ) => {
152
- Some ( Ok ( Notification {
156
+ Ok ( Some ( Notification {
153
157
process_id : process_id,
154
158
channel : channel,
155
159
payload : payload,
156
160
} ) )
157
161
}
158
- Err ( err) => Some ( Err ( Error :: Io ( err) ) ) ,
162
+ Err ( err) => Err ( Error :: Io ( err) ) ,
159
163
_ => unreachable ! ( ) ,
160
164
}
161
165
}
162
-
163
- fn size_hint ( & self ) -> ( usize , Option < usize > ) {
164
- ( usize:: max_value ( ) , None )
165
- }
166
166
}
167
167
168
168
/// An iterator over notifications which will block for a period of time if
@@ -172,30 +172,31 @@ pub struct TimeoutIter<'a> {
172
172
timeout : Duration ,
173
173
}
174
174
175
- impl < ' a > Iterator for TimeoutIter < ' a > {
176
- type Item = Result < Notification > ;
175
+ impl < ' a > FallibleIterator for TimeoutIter < ' a > {
176
+ type Item = Notification ;
177
+ type Error = Error ;
177
178
178
- fn next ( & mut self ) -> Option < Result < Notification > > {
179
+ fn next ( & mut self ) -> Result < Option < Notification > > {
179
180
let mut conn = self . conn . conn . borrow_mut ( ) ;
180
181
181
182
if let Some ( notification) = conn. notifications . pop_front ( ) {
182
- return Some ( Ok ( notification) ) ;
183
+ return Ok ( Some ( notification) ) ;
183
184
}
184
185
185
186
if conn. is_desynchronized ( ) {
186
- return Some ( Err ( Error :: Io ( desynchronized ( ) ) ) ) ;
187
+ return Err ( Error :: Io ( desynchronized ( ) ) ) ;
187
188
}
188
189
189
190
match conn. read_message_with_notification_timeout ( self . timeout ) {
190
191
Ok ( Some ( Backend :: NotificationResponse { process_id, channel, payload } ) ) => {
191
- Some ( Ok ( Notification {
192
+ Ok ( Some ( Notification {
192
193
process_id : process_id,
193
194
channel : channel,
194
195
payload : payload,
195
196
} ) )
196
197
}
197
- Ok ( None ) => None ,
198
- Err ( err) => Some ( Err ( Error :: Io ( err) ) ) ,
198
+ Ok ( None ) => Ok ( None ) ,
199
+ Err ( err) => Err ( Error :: Io ( err) ) ,
199
200
_ => unreachable ! ( ) ,
200
201
}
201
202
}
0 commit comments