diff options
author | Jesse Luehrs <doy@tozt.net> | 2014-09-04 17:50:35 -0400 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2014-09-04 17:50:35 -0400 |
commit | 9db83fd0c6e0f712ff638aaf6c939c56d1401e74 (patch) | |
tree | 7ca548cfe3ca7c69dc020a5edfc1c06cacac50a3 | |
parent | 52997d0cd46d093a5be1129e22cba4e86ec98525 (diff) | |
download | rust-irc-9db83fd0c6e0f712ff638aaf6c939c56d1401e74.tar.gz rust-irc-9db83fd0c6e0f712ff638aaf6c939c56d1401e74.zip |
start adding callback support
the lifetime parameters on the callback closure parameters really should
be able to be inferred (since they aren't being used for anything at
all), but apparently rust can't handle that yet. hopefully they can go
away in the future.
-rw-r--r-- | src/client.rs | 56 | ||||
-rw-r--r-- | src/constants.rs | 2 | ||||
-rw-r--r-- | src/lib.rs | 2 |
3 files changed, 44 insertions, 16 deletions
diff --git a/src/client.rs b/src/client.rs index 4c063f6..f05b3b0 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,9 +1,18 @@ use std::{io, str}; +use std::collections::hashmap::HashMap; -use constants::{Nick, Pass, User}; +use constants::{MessageType, Nick, Pass, Ping, Pong, User}; use message::Message; -pub struct ClientBuilder { +#[deriving(PartialEq, Eq, Hash)] +pub enum CallbackEvent { + MessageEvent(MessageType), + // XXX timer? connect/disconnect? +} + +// XXX these lifetime parameters make no sense at all, but appear to be +// necessary for now... +pub struct ClientBuilder<'a, 'b> { nick: String, pass: Option<String>, realname: String, @@ -13,14 +22,28 @@ pub struct ClientBuilder { servername: String, port: u16, + + callbacks: HashMap<CallbackEvent, Vec<Box<Fn<(&'a mut Client<'a, 'b>, &'b Message), ()> + 'static>>>, } -pub struct Client { +pub struct Client<'a, 'b> { conn: io::BufferedStream<io::TcpStream>, + callbacks: HashMap<CallbackEvent, Vec<Box<Fn<(&'a mut Client<'a, 'b>, &'b Message), ()> + 'static>>>, } -impl ClientBuilder { - pub fn new (nick: &str, servername: &str) -> ClientBuilder { +impl<'a, 'b> ClientBuilder<'a, 'b> { + pub fn new (nick: &str, servername: &str) -> ClientBuilder<'a, 'b> { + let mut callbacks = HashMap::new(); + + callbacks.insert( + MessageEvent(Ping), + vec![ + box () (|&: client: &mut Client, m: &Message| { + client.write(Message::new(None, Pong, m.params().clone())); + }) as Box<Fn<(&mut Client, &Message), ()> + 'static> + ] + ); + ClientBuilder { nick: nick.to_string(), pass: None, @@ -31,35 +54,37 @@ impl ClientBuilder { servername: servername.to_string(), port: 6667, + + callbacks: callbacks, } } - pub fn set_pass (&mut self, pass: &str) -> &mut ClientBuilder { + pub fn set_pass (&mut self, pass: &str) -> &mut ClientBuilder<'a, 'b> { self.pass = Some(pass.to_string()); self } - pub fn set_username (&mut self, username: &str) -> &mut ClientBuilder { + pub fn set_username (&mut self, username: &str) -> &mut ClientBuilder<'a, 'b> { self.username = username.to_string(); self } - pub fn set_realname (&mut self, realname: &str) -> &mut ClientBuilder { + pub fn set_realname (&mut self, realname: &str) -> &mut ClientBuilder<'a, 'b> { self.realname = realname.to_string(); self } - pub fn set_hostname (&mut self, hostname: &str) -> &mut ClientBuilder { + pub fn set_hostname (&mut self, hostname: &str) -> &mut ClientBuilder<'a, 'b> { self.hostname = Some(hostname.to_string()); self } - pub fn set_port (&mut self, port: u16) -> &mut ClientBuilder { + pub fn set_port (&mut self, port: u16) -> &mut ClientBuilder<'a, 'b> { self.port = port; self } - pub fn connect (self) -> Client { + pub fn connect (self) -> Client<'a, 'b> { let nick = self.nick.clone(); let pass = self.pass.clone(); let hostname = match self.hostname { @@ -92,13 +117,16 @@ impl ClientBuilder { client } - pub fn connect_raw (self) -> Client { + pub fn connect_raw (self) -> Client<'a, 'b> { let mut stream = io::TcpStream::connect(self.servername.as_slice(), self.port); - Client { conn: io::BufferedStream::new(stream.unwrap()) } + Client { + conn: io::BufferedStream::new(stream.unwrap()), + callbacks: self.callbacks, + } } } -impl Client { +impl<'a, 'b> Client<'a, 'b> { pub fn read (&mut self) -> Message { // \n isn't valid inside a message, so this should be fine. if the \n // we find isn't preceded by a \r, this will be caught by the message diff --git a/src/constants.rs b/src/constants.rs index ff5e399..b3cb9ce 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -3,7 +3,7 @@ use std::fmt::{FormatError, Formatter, Show}; use std::from_str::FromStr; -#[deriving(PartialEq, Eq)] +#[deriving(PartialEq, Eq, Hash)] pub enum MessageType { Pass, Nick, @@ -1,4 +1,4 @@ -#![feature(phase, globs)] +#![feature(phase, globs, unboxed_closures)] #[phase(plugin)] extern crate regex_macros; extern crate regex; |