1
Fork 0

Implement typed module. TODO: Calculate serialization buffer size.

This commit is contained in:
Alex Helfet 2017-12-24 21:39:43 +00:00
parent a8a00555b5
commit ec0567e0cc
2 changed files with 40 additions and 10 deletions

View file

@ -1,5 +1,8 @@
//! Representations of errors returned by this crate. //! Representations of errors returned by this crate.
#[cfg(feature = "typed")]
use ssmarshal;
#[cfg(feature = "use_std")] #[cfg(feature = "use_std")]
use std::io; use std::io;
@ -24,6 +27,10 @@ pub enum Error {
/// Forwarded io::Error. /// Forwarded io::Error.
#[cfg(feature = "use_std")] #[cfg(feature = "use_std")]
Io(io::Error), Io(io::Error),
/// Forwarded ssmarshal::Error.
#[cfg(feature = "typed")]
Ssmarshal(ssmarshal::Error),
} }
#[cfg(feature = "use_std")] #[cfg(feature = "use_std")]
@ -32,3 +39,10 @@ impl From<io::Error> for Error {
Error::Io(e) Error::Io(e)
} }
} }
#[cfg(feature = "typed")]
impl From<ssmarshal::Error> for Error {
fn from(e: ssmarshal::Error) -> Error {
Error::Ssmarshal(e)
}
}

View file

@ -3,18 +3,19 @@
use error::{Result}; use error::{Result};
use serde::Serialize; use serde::Serialize;
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
// use ssmarshal; use ssmarshal;
use std::io::{Read, Write}; 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. /// Sends encoded structs of type `T` over an inner `io::Write` instance.
pub struct Sender<W: Write, T: Serialize> { pub struct Sender<W: Write, T: Serialize + Sized> {
w: W, w: W,
_t: PhantomData<T>, _t: PhantomData<T>,
} }
impl<W: Write, T: Serialize> Sender<W, T> { impl<W: Write, T: Serialize + Sized> Sender<W, T> {
/// Construct a `Sender` that sends encoded structs over the supplied /// Construct a `Sender` that sends encoded structs over the supplied
/// `io::Write`. /// `io::Write`.
pub fn new(w: W) -> Sender<W, T> { pub fn new(w: W) -> Sender<W, T> {
@ -42,8 +43,18 @@ impl<W: Write, T: Serialize> Sender<W, T> {
/// transmitted call [`flush`](#method.flush). /// transmitted call [`flush`](#method.flush).
/// ///
/// See also: [`send`](#method.send) /// See also: [`send`](#method.send)
pub fn queue(&mut self, _v: &T) -> Result<usize> { pub fn queue(&mut self, v: &T) -> Result<usize> {
unimplemented!(); // TODO: Calculate buffer length with size_of::<T>().
// let mut ser_buf = [0u8; size_of::<T>()];
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 /// Encode the supplied payload as a frame, write it to the
@ -84,7 +95,9 @@ impl<R: Read, T: DeserializeOwned> Receiver<R, T> {
/// Receive an encoded frame from the inner `io::Read`, decode it /// Receive an encoded frame from the inner `io::Read`, decode it
/// and return the payload. /// and return the payload.
pub fn recv(&mut self) -> Result<T> { pub fn recv(&mut self) -> Result<T> {
unimplemented!() let payload = super::decode_from_reader::<R>(&mut self.r)?;
let (v, _len) = ssmarshal::deserialize(&*payload)?;
Ok(v)
} }
} }
@ -119,6 +132,7 @@ mod tests {
let v = val(); let v = val();
tx.send(&v).unwrap(); tx.send(&v).unwrap();
let r = rx.recv().unwrap(); let r = rx.recv().unwrap();
println!("r: {:#?}", r);
assert_eq!(v, r); assert_eq!(v, r);
} }
@ -132,7 +146,7 @@ mod tests {
} }
fn val() -> Test { fn val() -> Test {
Test { let v = Test {
i8: 1, i8: 1,
i16: 2, i16: 2,
i32: 3, i32: 3,
@ -147,7 +161,9 @@ mod tests {
none: None, none: None,
a: [1, 2, 3], a: [1, 2, 3],
} };
println!("Test value: {:#?}", v);
v
} }
fn pair() -> (Sender<Box<Write>, Test>, Receiver<Box<Read>, Test>) { fn pair() -> (Sender<Box<Write>, Test>, Receiver<Box<Read>, Test>) {