Extended for_each_incoming to all IRC streams, and added the error to

the return type.
This commit is contained in:
Aaron Weiss 2017-06-25 04:58:08 -04:00
parent cfafb39ed9
commit b95ed76088
No known key found for this signature in database
GPG key ID: 0237035D9BF03AE2

View file

@ -25,6 +25,23 @@ use proto::Command::{JOIN, NICK, NICKSERV, PART, PRIVMSG, ChannelMODE, QUIT};
pub mod utils; pub mod utils;
/// Trait extending all IRC streams with `for_each_incoming` convenience function.
pub trait EachIncomingExt: Stream<Item=Message, Error=error::Error> {
/// Blocks on the stream, running the given function on each incoming message as they arrive.
fn for_each_incoming<F>(self, mut f: F) -> error::Result<()>
where
F: FnMut(Message) -> (),
Self: Sized,
{
self.for_each(|msg| {
f(msg);
Ok(())
}).wait()
}
}
impl<T> EachIncomingExt for T where T: Stream<Item=Message, Error=error::Error> {}
/// An interface for interacting with an IRC server. /// An interface for interacting with an IRC server.
pub trait Server { pub trait Server {
/// Gets the configuration being used with this Server. /// Gets the configuration being used with this Server.
@ -40,14 +57,11 @@ pub trait Server {
fn stream(&self) -> ServerStream; fn stream(&self) -> ServerStream;
/// Blocks on the stream, running the given function on each incoming message as they arrive. /// Blocks on the stream, running the given function on each incoming message as they arrive.
fn for_each_incoming<F>(&self, mut f: F) -> () fn for_each_incoming<F>(&self, f: F) -> error::Result<()>
where where
F: FnMut(Message) -> (), F: FnMut(Message) -> (),
{ {
self.stream().for_each(|msg| { self.stream().for_each_incoming(f)
f(msg);
Ok(())
}).wait().unwrap()
} }
/// Gets a list of currently joined channels. This will be none if tracking is not supported /// Gets a list of currently joined channels. This will be none if tracking is not supported