From 52d19c730bafe21cbd882553d282a5044d63de97 Mon Sep 17 00:00:00 2001 From: Avril Date: Thu, 5 Aug 2021 00:25:26 +0100 Subject: [PATCH] Start reworking SerializedMessage -> binary pipeline. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Moved from `io::Write` to `bytes::BufMut`, etc. TODO: Move from `io::Read` to `bytes::Buf` (SerializedMessage::::from_buffer(impl Buf)) Fortune for rsh's current commit: Future small blessing − 末小吉 --- Cargo.lock | 12 +++++- Cargo.toml | 1 + src/message.rs | 10 ++++- src/message/binary.rs | 92 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 src/message/binary.rs diff --git a/Cargo.lock b/Cargo.lock index 90ff8a8..ec8287e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -77,6 +77,15 @@ version = "0.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38" +[[package]] +name = "bytes" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b700ce4376041dcd0a327fd0097c41095743c4c8af8887265942faf1100bd040" +dependencies = [ + "serde", +] + [[package]] name = "cc" version = "1.0.69" @@ -780,6 +789,7 @@ dependencies = [ name = "rsh" version = "0.1.0" dependencies = [ + "bytes 1.0.1", "chacha20stream", "color-eyre", "cryptohelpers", @@ -950,7 +960,7 @@ version = "0.2.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6703a273949a90131b290be1fe7b039d0fc884aa1935860dfcbe056f28cd8092" dependencies = [ - "bytes", + "bytes 0.5.6", "fnv", "futures-core", "iovec", diff --git a/Cargo.toml b/Cargo.toml index f8edb7a..d3a4e5d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +bytes = { version = "1.0.1", features = ["serde"] } chacha20stream = { version = "1.0.3", features = ["async"] } color-eyre = "0.5.11" cryptohelpers = { version = "1.8.1" , features = ["serialise", "full"] } diff --git a/src/message.rs b/src/message.rs index 80a281e..4cf3f0c 100644 --- a/src/message.rs +++ b/src/message.rs @@ -236,6 +236,9 @@ impl Message } } +mod binary; +pub use binary::*; + impl SerializedMessage { /// Get the message header @@ -243,6 +246,9 @@ impl SerializedMessage { MessageHeader(&self.header, PhantomData) } + +} +/* /// Consume into an async writer pub async fn into_writer_async(self, mut writer: W) -> eyre::Result { @@ -328,6 +334,8 @@ impl SerializedMessage v } } + */ +/* impl SerializedMessage { /// Create from a reader. @@ -420,6 +428,6 @@ impl SerializedMessage Self::from_reader(&mut &bytes[..]) } } - +*/ #[cfg(test)] mod tests; diff --git a/src/message/binary.rs b/src/message/binary.rs new file mode 100644 index 0000000..7b5ea05 --- /dev/null +++ b/src/message/binary.rs @@ -0,0 +1,92 @@ +//! Creating binary from messages +//! +//! `SerializedMessage` to `Bytes`. +use super::*; +use bytes::{ + Bytes, + BytesMut, + BufMut, + Buf, +}; + +impl SerializedMessage +{ + /// Write this message to a buffer + /// + /// # Panics + /// If `buffer` cannot hold enough bytes. + pub fn into_buffer(self, mut buffer: impl BufMut) -> eyre::Result + { + let mut w=0; + macro_rules! write { + ($bytes:expr) => { + { + let slice: &[u8] = ($bytes).as_ref(); + buffer.put_slice(slice); + w+=slice.len(); + } + }; + (? $o:expr) => { + { + match $o { + Some(opt) => { + buffer.put_u8(1); + write!(opt); + }, + None => {buffer.put_u8(0);}, + } + w+=1; + } + }; + (: $ser:expr) => { + { + let mut v = StackVec::new(); + serde_cbor::to_writer(&mut v, $ser)?; + buffer.put_u64(v.len().try_into()?); + write!(&v[..]); + } + }; + } + + write!(: &self.header); + buffer.put_u64(self.data.len().try_into()?); + write!(self.data); + write!(self.hash); + write!(? self.enc_key); + write!(? self.sig); + + Ok(w) + } + + /// Write this message to a new `Bytes`. + pub fn into_bytes(self) -> eyre::Result + { + let mut output = BytesMut::with_capacity(4096); //TODO: Find a better default capacity for this. + self.into_buffer(&mut output)?; + Ok(output.freeze()) + } +} + +impl SerializedMessage +{ + /// Create from a buffer of bytes. + /// + /// # Panics + /// If `bytes` does not contain enough data to read. + pub fn from_buffer(bytes: impl Buf) -> eyre::Result + { + todo!() + } + + /// Create from a slice of bytes + #[inline] pub fn from_slice(bytes: impl AsRef<[u8]>) -> eyre::Result + { + Self::from_buffer(bytes.as_ref()) + } + + /// Create from a `Bytes` instance + #[inline(always)] pub fn from_bytes(bytes: Bytes) -> eyre::Result + { + Self::from_buffer(bytes) + } +}