From 9db83fd0c6e0f712ff638aaf6c939c56d1401e74 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 4 Sep 2014 17:50:35 -0400 Subject: 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. --- src/client.rs | 56 ++++++++++++++++++++++++++++++++++++++++++-------------- src/constants.rs | 2 +- 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, realname: String, @@ -13,14 +22,28 @@ pub struct ClientBuilder { servername: String, port: u16, + + callbacks: HashMap, &'b Message), ()> + 'static>>>, } -pub struct Client { +pub struct Client<'a, 'b> { conn: io::BufferedStream, + callbacks: HashMap, &'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 + '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, diff --git a/src/lib.rs b/src/lib.rs index 847f013..f7c1784 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -#![feature(phase, globs)] +#![feature(phase, globs, unboxed_closures)] #[phase(plugin)] extern crate regex_macros; extern crate regex; -- cgit v1.2.3-54-g00ecf