#![crate_name = "superchan"]
#![feature(unsafe_destructor)]
#![allow(dead_code)]
#![unstable = "waiting for the serialization dust to settle"]
extern crate "rustc-serialize" as rustc_serialize;
extern crate bincode;
use bincode::{encode, EncodingError, decode, DecodingError, SizeLimit};
use rustc_serialize::{Decodable, Encodable};
use std::sync::mpsc;
use std::error::{Error, FromError};
use std::io::{IoError, Reader, Writer};
use std::sync::Future;
pub mod tcp;
#[unstable = "waiting for the serialization dust to settle"]
pub trait Sender<T> where T: Encodable + Send {
fn send(&mut self, t: T) -> Future<Result<(), SenderError<T>>>;
}
#[stable]
pub enum SenderError<T> {
#[stable] Mpsc(mpsc::SendError<T>),
#[stable] Io(IoError),
#[stable] Encoding(EncodingError),
}
impl<T> Error for SenderError<T> where T: Send {
fn description(&self) -> &str {
match *self {
SenderError::Mpsc(_) => "mpsc error",
SenderError::Io(_) => "io error",
SenderError::Encoding(_) => "encoding error",
}
}
fn cause(&self) -> Option<&Error> {
match *self {
SenderError::Mpsc(_) => None,
SenderError::Io(ref err) => Some(err as &Error),
SenderError::Encoding(ref err) => Some(err as &Error),
}
}
}
impl<T> FromError<mpsc::SendError<T>> for SenderError<T> {
fn from_error(err: mpsc::SendError<T>) -> SenderError<T> {
SenderError::Mpsc(err)
}
}
impl<T> FromError<IoError> for SenderError<T> {
fn from_error(err: IoError) -> SenderError<T> {
SenderError::Io(err)
}
}
impl<T> FromError<EncodingError> for SenderError<T> {
fn from_error(err: EncodingError) -> SenderError<T> {
SenderError::Encoding(err)
}
}
type SendRequest<T> = (T, mpsc::Sender<Result<(), SenderError<T>>>);
#[unstable = "waiting for the serialization dust to settle"]
pub trait Receiver<S> where S: Decodable + Send {
fn try_recv(&mut self) -> Result<S, ReceiverError<S>>;
fn recv(&mut self) -> S {
match self.try_recv() {
Ok(val) => val,
Err(e) => panic!("{:?}", e.description()),
}
}
}
#[stable]
pub enum ReceiverError<S> {
#[stable] Io(IoError),
#[stable] Decoding(DecodingError),
}
impl<S> Error for ReceiverError<S> where S: Send {
fn description(&self) -> &str {
match *self {
ReceiverError::Io(_) => "io error",
ReceiverError::Decoding(_) => "decoding error",
}
}
fn cause(&self) -> Option<&Error> {
match *self {
ReceiverError::Io(ref err) => Some(err as &Error),
ReceiverError::Decoding(ref err) => Some(err as &Error),
}
}
}
impl<S> FromError<IoError> for ReceiverError<S> {
fn from_error(err: IoError) -> ReceiverError<S> {
ReceiverError::Io(err)
}
}
impl<S> FromError<DecodingError> for ReceiverError<S> {
fn from_error(err: DecodingError) -> ReceiverError<S> {
ReceiverError::Decoding(err)
}
}
fn read_item<S, R>(r: &mut R, size: usize) -> Result<S, ReceiverError<S>> where S: Decodable, R: Reader {
let data = try!(r.read_exact(size));
Ok(try!(decode::<S>(data.as_slice())))
}
fn write_item<T, W>(w: &mut W, val: &T) -> Result<(), SenderError<T>> where T: Encodable, W: Writer {
let e = try!(encode(val, SizeLimit::Infinite));
try!(w.write_le_uint(e.len()));
try!(w.write(e.as_slice()));
try!(w.flush());
Ok(())
}