diff --git a/decode/src/main.rs b/decode/src/main.rs index 0413fde..bfc7106 100644 --- a/decode/src/main.rs +++ b/decode/src/main.rs @@ -48,7 +48,7 @@ fn try() -> Result<()> { let mut r = framed::bytes::Config::default() .typed::() - .into_receiver(stdin()); + .to_receiver(stdin()); let mut csvw: Option> = match out_fmt { diff --git a/framed/src/bytes.rs b/framed/src/bytes.rs index 74d9f0d..0dcacb4 100644 --- a/framed/src/bytes.rs +++ b/framed/src/bytes.rs @@ -14,19 +14,19 @@ //! # use framed::bytes::*; //! # use std::io::Cursor; //! # -//! let config = Config::default(); +//! let mut config = Config::default(); //! //! let payload = [1, 2, 3]; //! //! let mut encoded = vec![]; //! { -//! let mut sender = config.clone().into_sender(&mut encoded); +//! let mut sender = config.clone().to_sender(&mut encoded); //! sender.send(&payload).expect("send ok"); //! } //! //! // `encoded` now contains the encoded frame. //! -//! let mut receiver = config.clone().into_receiver(Cursor::new(encoded)); +//! let mut receiver = config.clone().to_receiver(Cursor::new(encoded)); //! let decoded = receiver.recv().expect("recv ok"); //! //! assert_eq!(payload, *decoded); @@ -42,7 +42,7 @@ //! # use framed::*; //! # use framed::bytes::*; //! # -//! let mut codec = Config::default().into_codec(); +//! let mut codec = Config::default().to_codec(); //! //! // In a no_std crate without dynamic memory allocation we would typically //! // know the maximum payload length, which we can use for payload buffers. @@ -98,7 +98,7 @@ pub use ::checksum::Checksum; /// specific configuration. /// /// Construct an instance from a `Config` instance with the -/// `Config::into_codec` method. +/// `Config::to_codec` method. pub struct Codec { config: Config } @@ -114,33 +114,33 @@ pub struct Config { impl Config { /// Construct a `Codec` instance with this configuration. - pub fn into_codec(self) -> Codec { + pub fn to_codec(&mut self) -> Codec { Codec { - config: self, + config: self.clone(), } } #[cfg(feature = "use_std")] /// Construct a `Receiver` instance with this configuration. - pub fn into_receiver(self, r: R) -> Receiver { + pub fn to_receiver(&mut self, r: R) -> Receiver { Receiver:: { - codec: self.into_codec(), + codec: self.to_codec(), r: r, } } #[cfg(feature = "use_std")] /// Construct a `Sender` instance with this configuration. - pub fn into_sender(self, w: W) -> Sender { + pub fn to_sender(&mut self, w: W) -> Sender { Sender:: { - codec: self.into_codec(), + codec: self.to_codec(), w: w, } } /// Construct a `framed::typed::Config` instance to encode and decode a /// serializable type `T` with this byte encoding configuration. - pub fn typed(self) -> typed::Config { + pub fn typed(&mut self) -> typed::Config { typed::Config::::new(self) } @@ -528,7 +528,7 @@ mod tests { } fn codec() -> Codec { - Config::default().into_codec() + Config::default().to_codec() } // A test payload. @@ -710,9 +710,39 @@ mod tests { let decoded = c.decode_from_reader::>(&mut reader).unwrap(); assert_eq!(&*decoded, &PAYLOAD); } -} -// TODO: Some more roundtrip cases. + #[test] + #[cfg(feature = "use_std")] + fn roundtrip_default_config() { + roundtrip_case(&mut Config::default() + .to_codec(), + &PAYLOAD) + } + + #[test] + #[cfg(feature = "use_std")] + fn roundtrip_no_checksum() { + roundtrip_case(&mut Config::default() + .set_checksum(Checksum::None) + .to_codec(), + &PAYLOAD) + } + + #[test] + #[cfg(feature = "use_std")] + fn roundtrip_empty_payload() { + roundtrip_case(&mut Config::default() + .to_codec(), + &[]) + } + + #[cfg(feature = "use_std")] + fn roundtrip_case(c: &mut Codec, payload: &Payload) { + let encoded = c.encode_to_box(payload).unwrap(); + let decoded = c.decode_to_box(&*encoded).unwrap(); + assert_eq!(&*decoded, payload); + } +} #[cfg(all(test, feature = "use_std"))] mod rw_tests { @@ -778,7 +808,7 @@ mod rw_tests { #[test] fn partial_input() { let chan = Channel::new(); - let mut rx = config().into_receiver(chan.reader()); + let mut rx = config().to_receiver(chan.reader()); let mut tx_raw = chan.writer(); tx_raw.write(&[0x01]).unwrap(); match rx.recv() { @@ -794,8 +824,8 @@ mod rw_tests { fn pair() -> (Sender>, Receiver>) { let chan = Channel::new(); let c = config(); - let tx = c.clone().into_sender(Box::new(chan.writer()) as Box); - let rx = c.clone().into_receiver(Box::new(chan.reader()) as Box); + let tx = c.clone().to_sender(Box::new(chan.writer()) as Box); + let rx = c.clone().to_receiver(Box::new(chan.reader()) as Box); (tx, rx) } } diff --git a/framed/src/typed.rs b/framed/src/typed.rs index 99ef853..dc83fc3 100644 --- a/framed/src/typed.rs +++ b/framed/src/typed.rs @@ -34,17 +34,17 @@ //! } //! //! let payload = Test { a: 1, b: 2 }; -//! let config = framed::bytes::Config::default().typed::(); +//! let mut config = framed::bytes::Config::default().typed::(); //! //! let mut encoded = vec![]; //! { -//! let mut sender = config.clone().into_sender(&mut encoded); +//! let mut sender = config.clone().to_sender(&mut encoded); //! sender.send(&payload).expect("send ok"); //! } //! //! // `encoded` now contains the encoded value. //! -//! let mut receiver = config.clone().into_receiver(Cursor::new(encoded)); +//! let mut receiver = config.clone().to_receiver(Cursor::new(encoded)); //! let decoded = receiver.recv().expect("recv ok"); //! //! assert_eq!(payload, decoded); @@ -73,9 +73,9 @@ //! # } //! # //! # let payload = Test { a: 1, b: 2 }; -//! # let config = bytes::Config::default().typed::(); +//! # let mut config = bytes::Config::default().typed::(); //! # -//! let mut codec = config.into_codec(); +//! let mut codec = config.to_codec(); //! let mut ser_buf = [0u8; max_serialize_buf_len::()]; //! let mut encoded_buf = [0u8; max_encoded_len::()]; //! let encoded_len = codec.encode_to_slice( @@ -110,7 +110,7 @@ use std::io::{Read, Write}; /// with a specific configuration. /// /// Construct an instance from a `Config` instance with the -/// `Config::into_codec` method. +/// `Config::to_codec` method. pub struct Codec { bytes_codec: bytes::Codec, _phantom: PhantomData, @@ -135,35 +135,35 @@ impl Clone for Config { } impl Config { - pub(crate) fn new(bytes_config: bytes::Config) -> Config { + pub(crate) fn new(bytes_config: &bytes::Config) -> Config { Config:: { - bytes_config: bytes_config, + bytes_config: bytes_config.clone(), _phantom: PhantomData::::default(), } } /// Construct a `Codec` instance with this configuration. - pub fn into_codec(self) -> Codec { + pub fn to_codec(&mut self) -> Codec { Codec:: { - bytes_codec: self.bytes_config.into_codec(), + bytes_codec: self.bytes_config.to_codec(), _phantom: PhantomData::::default(), } } #[cfg(feature = "use_std")] /// Construct a `Receiver` instance with this configuration. - pub fn into_receiver(self, r: R) -> Receiver { + pub fn to_receiver(&mut self, r: R) -> Receiver { Receiver:: { - codec: self.into_codec(), + codec: self.to_codec(), r: r, } } #[cfg(feature = "use_std")] /// Construct a `Sender` instance with this configuration. - pub fn into_sender(self, w: W) -> Sender { + pub fn to_sender(&mut self, w: W) -> Sender { Sender:: { - codec: self.into_codec(), + codec: self.to_codec(), w: w, } } @@ -400,7 +400,7 @@ mod tests { } fn codec() -> Codec { - bytes::Config::default().typed::().into_codec() + bytes::Config::default().typed::().to_codec() } mod slice_tests { @@ -492,9 +492,9 @@ mod tests { let chan = Channel::new(); let conf = bytes::Config::default().typed::(); let tx = conf.clone() - .into_sender(Box::new(chan.writer()) as Box); + .to_sender(Box::new(chan.writer()) as Box); let rx = conf.clone() - .into_receiver(Box::new(chan.reader()) as Box); + .to_receiver(Box::new(chan.reader()) as Box); (tx, rx) } } diff --git a/test_type/src/main.rs b/test_type/src/main.rs index 8670a31..94108e3 100644 --- a/test_type/src/main.rs +++ b/test_type/src/main.rs @@ -15,6 +15,6 @@ fn main() { eprintln!("test_type/main.rs: Sending sample value: {:#?}", t); let mut s = framed::bytes::Config::default() .typed::() - .into_sender(stdout()); + .to_sender(stdout()); s.send(&t).unwrap(); }