Added newtypes for the specific kinds of Reader and Writer used in the library

This commit is contained in:
Aaron Weiss 2014-10-08 13:10:55 -04:00
parent 567074a599
commit b0226099d7
3 changed files with 11 additions and 6 deletions

View file

@ -1,7 +1,7 @@
use std::io::{BufferedWriter, IoResult, TcpStream, Writer};
use data::Message;
use data::{IrcReader, IrcWriter, Message};
pub enum Connection<T, U> where T: Writer + Sized + 'static, U: Reader + Sized + Clone + 'static {
pub enum Connection<T, U> where T: IrcWriter, U: IrcReader {
Conn(T, U),
}
@ -12,7 +12,7 @@ impl Connection<BufferedWriter<TcpStream>, TcpStream> {
}
}
impl<T, U> Connection<T, U> where T: Writer + Sized + 'static, U: Reader + Sized + Clone + 'static {
impl<T, U> Connection<T, U> where T: IrcWriter, U: IrcReader {
fn send_internal(conn: &mut Connection<T, U>, msg: &str) -> IoResult<()> {
match conn {
&Conn(ref mut send, _) => {

View file

@ -2,6 +2,11 @@ use std::io::fs::File;
use std::io::{InvalidInput, IoError, IoResult};
use serialize::json::{decode};
pub trait IrcWriter: Writer + Sized + 'static {}
impl<T> IrcWriter for T where T: Writer + Sized + 'static {}
pub trait IrcReader: Reader + Sized + Clone + 'static {}
impl<T> IrcReader for T where T: Reader + Sized + Clone + 'static {}
pub struct Message<'a> {
pub source: Option<&'a str>,
pub command: &'a str,

View file

@ -8,19 +8,19 @@ use std::collections::HashMap;
use std::io::{BufferedReader, BufferedWriter, InvalidInput, IoError, IoResult, TcpStream};
use std::vec::Vec;
use conn::{Conn, Connection};
use data::{Config, Message};
use data::{Config, IrcReader, IrcWriter, Message};
pub mod conn;
pub mod data;
pub struct Bot<'a, T, U> where T: Writer + Sized + 'static, U: Reader + Sized + Clone + 'static {
pub struct Bot<'a, T, U> where T: IrcWriter, U: IrcReader {
pub conn: RefCell<Connection<T, U>>,
pub config: Config,
process: RefCell<|&Bot<T, U>, &str, &str, &[&str]|:'a -> IoResult<()>>,
pub chanlists: HashMap<String, Vec<String>>,
}
impl<'a, T, U> Bot<'a, T, U> where T: Writer + Sized + 'static, U: Buffer + Sized + Clone + 'static {
impl<'a, T, U> Bot<'a, T, U> where T: IrcWriter, U: IrcReader {
pub fn new(process: |&Bot<BufferedWriter<TcpStream>, TcpStream>, &str, &str, &[&str]|:'a -> IoResult<()>) -> IoResult<Bot<'a, BufferedWriter<TcpStream>, TcpStream>> {
let config = try!(Config::load());
let conn = try!(Connection::connect(config.server.as_slice(), config.port));