diff --git a/src/error.rs b/src/error.rs index b1487b2..ddd22ec 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,5 +1,8 @@ //! Representations of errors returned by this crate. +#[cfg(feature = "typed")] +use ssmarshal; + #[cfg(feature = "use_std")] use std::io; @@ -24,6 +27,10 @@ pub enum Error { /// Forwarded io::Error. #[cfg(feature = "use_std")] Io(io::Error), + + /// Forwarded ssmarshal::Error. + #[cfg(feature = "typed")] + Ssmarshal(ssmarshal::Error), } #[cfg(feature = "use_std")] @@ -32,3 +39,10 @@ impl From for Error { Error::Io(e) } } + +#[cfg(feature = "typed")] +impl From for Error { + fn from(e: ssmarshal::Error) -> Error { + Error::Ssmarshal(e) + } +} diff --git a/src/typed.rs b/src/typed.rs index 656c0de..7d0a634 100644 --- a/src/typed.rs +++ b/src/typed.rs @@ -3,18 +3,19 @@ use error::{Result}; use serde::Serialize; use serde::de::DeserializeOwned; -// use ssmarshal; +use ssmarshal; use std::io::{Read, Write}; -use std::marker::PhantomData; - +use std::marker::{PhantomData, Sized}; +#[allow(unused_imports)] +use std::mem::size_of; /// Sends encoded structs of type `T` over an inner `io::Write` instance. -pub struct Sender { +pub struct Sender { w: W, _t: PhantomData, } -impl Sender { +impl Sender { /// Construct a `Sender` that sends encoded structs over the supplied /// `io::Write`. pub fn new(w: W) -> Sender { @@ -42,8 +43,18 @@ impl Sender { /// transmitted call [`flush`](#method.flush). /// /// See also: [`send`](#method.send) - pub fn queue(&mut self, _v: &T) -> Result { - unimplemented!(); + pub fn queue(&mut self, v: &T) -> Result { + // TODO: Calculate buffer length with size_of::(). + // let mut ser_buf = [0u8; size_of::()]; + + let mut ser_buf = [0u8; 1024]; + let ser_len = ssmarshal::serialize(&mut ser_buf, v)?; + let ser = &ser_buf[0..ser_len]; + #[cfg(feature = "trace")] { + println!("framed: Serialized = {:?}", ser); + } + + super::encode_to_writer(&ser, &mut self.w) } /// Encode the supplied payload as a frame, write it to the @@ -84,7 +95,9 @@ impl Receiver { /// Receive an encoded frame from the inner `io::Read`, decode it /// and return the payload. pub fn recv(&mut self) -> Result { - unimplemented!() + let payload = super::decode_from_reader::(&mut self.r)?; + let (v, _len) = ssmarshal::deserialize(&*payload)?; + Ok(v) } } @@ -119,6 +132,7 @@ mod tests { let v = val(); tx.send(&v).unwrap(); let r = rx.recv().unwrap(); + println!("r: {:#?}", r); assert_eq!(v, r); } @@ -132,7 +146,7 @@ mod tests { } fn val() -> Test { - Test { + let v = Test { i8: 1, i16: 2, i32: 3, @@ -147,7 +161,9 @@ mod tests { none: None, a: [1, 2, 3], - } + }; + println!("Test value: {:#?}", v); + v } fn pair() -> (Sender, Test>, Receiver, Test>) {