ServerIterator now iterates over IoResults.

This commit is contained in:
Aaron Weiss 2014-12-20 16:50:54 -05:00
parent fb743f148b
commit 70031aa8d8
5 changed files with 24 additions and 13 deletions

View file

@ -22,7 +22,7 @@ fn main() {
// We won't use a wrapper here because we don't need the added functionality.
spawn(move || {
for msg in server.iter() {
print!("{}", msg.into_string());
print!("{}", msg.unwrap().into_string());
}
});
}

View file

@ -19,6 +19,7 @@ fn main() {
let server = Wrapper::new(&irc_server);
server.identify().unwrap();
for message in server.iter() {
let message = message.unwrap();
print!("{}", message.into_string());
if message.command[] == "PRIVMSG" {
if let Some(msg) = message.suffix {

View file

@ -20,6 +20,7 @@ fn main() {
let server = Wrapper::new(&irc_server);
server.identify().unwrap();
for message in server.iter() {
let message = message.unwrap();
print!("{}", message.into_string());
if message.command[] == "PRIVMSG" {
if let Some(msg) = message.suffix {

View file

@ -22,7 +22,7 @@ fn main() {
let server = Wrapper::new(&*irc_server2);
server.identify().unwrap();
// Let's set up a loop that just prints the messages.
spawn(move || { irc_server.iter().map(|m| print!("{}", m.into_string())).count(); });
spawn(move || { irc_server.iter().map(|m| print!("{}", m.unwrap().into_string())).count(); });
loop {
server.send_privmsg("#vana", "TWEET TWEET").unwrap();
sleep(Duration::seconds(10))

View file

@ -1,7 +1,7 @@
//! Interface for working with IRC Servers
#![experimental]
use std::collections::HashMap;
use std::io::{BufferedReader, BufferedWriter, IoResult};
use std::io::{BufferedReader, BufferedWriter, IoError, IoErrorKind, IoResult};
use std::sync::{Mutex, RWLock};
use conn::{Connection, NetStream};
use data::{Command, Config, Message, Response, User};
@ -217,15 +217,24 @@ impl<'a, T: IrcReader, U: IrcWriter> ServerIterator<'a, T, U> {
}
}
impl<'a, T: IrcReader, U: IrcWriter> Iterator<Message> for ServerIterator<'a, T, U> {
fn next(&mut self) -> Option<Message> {
match self.get_next_line() {
Err(_) => None,
Ok(msg) => {
let message = from_str(msg[]);
self.server.handle_message(message.as_ref().unwrap());
message
impl<'a, T: IrcReader, U: IrcWriter> Iterator<IoResult<Message>> for ServerIterator<'a, T, U> {
fn next(&mut self) -> Option<IoResult<Message>> {
let res = self.get_next_line().and_then(|msg|
match from_str(msg[]) {
Some(msg) => {
self.server.handle_message(&msg);
Ok(msg)
},
None => Err(IoError {
kind: IoErrorKind::InvalidInput,
desc: "Failed to parse message.",
detail: Some(msg)
})
}
);
match res {
Err(ref err) if err.kind == IoErrorKind::EndOfFile => None,
_ => Some(res)
}
}
}
@ -265,7 +274,7 @@ mod test {
));
let mut messages = String::new();
for message in server.iter() {
messages.push_str(message.into_string()[]);
messages.push_str(message.unwrap().into_string()[]);
}
assert_eq!(messages[], exp);
}