Add BlockingAdminServiceEventIterator
This change adds an iterator wrapper for the AdminServiceEventClient,
which wraps the next_event calls, returning None, for any errors.
This provides all of the conveniences of the Iterator trait, such as
skip and skip_while.
@@ -94,3 +94,40 @@ pub trait AdminServiceEventClient {
/// This should block until an event is available.
fn next_event(&self) -> Result<AdminServiceEvent, NextEventError>;
}
+
+impl AdminServiceEventClient for Box<dyn AdminServiceEventClient> {
+ fn try_next_event(&self) -> Result<Option<AdminServiceEvent>, NextEventError> {
+ (**self).try_next_event()
+ }
+
+ fn next_event(&self) -> Result<AdminServiceEvent, NextEventError> {
+ (**self).next_event()
+ }
+}
+
+pub struct BlockingAdminServiceEventIterator<T>
+where
+ T: AdminServiceEventClient,
+{
+ client: T,
+}
+
+impl<T> BlockingAdminServiceEventIterator<T>
+where
+ T: AdminServiceEventClient,
+{
+ pub fn new(client: T) -> BlockingAdminServiceEventIterator<T> {
+ Self { client }
+ }
+}
+
+impl<T> Iterator for BlockingAdminServiceEventIterator<T>
+where
+ T: AdminServiceEventClient,
+{
+ type Item = AdminServiceEvent;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ self.client.next_event().ok()
+ }
+}
