Start reworking SerializedMessage -> binary pipeline.

Moved from `io::Write` to `bytes::BufMut`, etc.

TODO: Move from `io::Read` to `bytes::Buf` (SerializedMessage::<impl MessageValue>::from_buffer(impl Buf))

Fortune for rsh's current commit: Future small blessing − 末小吉
message-write-to-buf
Avril 3 years ago
parent 6afb148068
commit 52d19c730b
Signed by: flanchan
GPG Key ID: 284488987C31F630

12
Cargo.lock generated

@ -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",

@ -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"] }

@ -236,6 +236,9 @@ impl<V: ?Sized + MessageValue> Message<V>
}
}
mod binary;
pub use binary::*;
impl<V: ?Sized> SerializedMessage<V>
{
/// Get the message header
@ -243,6 +246,9 @@ impl<V: ?Sized> SerializedMessage<V>
{
MessageHeader(&self.header, PhantomData)
}
}
/*
/// Consume into an async writer
pub async fn into_writer_async<W:AsyncWrite+Unpin>(self, mut writer: W) -> eyre::Result<usize>
{
@ -328,6 +334,8 @@ impl<V: ?Sized> SerializedMessage<V>
v
}
}
*/
/*
impl<V: ?Sized + MessageValue> SerializedMessage<V>
{
/// Create from a reader.
@ -420,6 +428,6 @@ impl<V: ?Sized + MessageValue> SerializedMessage<V>
Self::from_reader(&mut &bytes[..])
}
}
*/
#[cfg(test)]
mod tests;

@ -0,0 +1,92 @@
//! Creating binary from messages
//!
//! `SerializedMessage` to `Bytes`.
use super::*;
use bytes::{
Bytes,
BytesMut,
BufMut,
Buf,
};
impl<V: ?Sized> SerializedMessage<V>
{
/// Write this message to a buffer
///
/// # Panics
/// If `buffer` cannot hold enough bytes.
pub fn into_buffer(self, mut buffer: impl BufMut) -> eyre::Result<usize>
{
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<Bytes>
{
let mut output = BytesMut::with_capacity(4096); //TODO: Find a better default capacity for this.
self.into_buffer(&mut output)?;
Ok(output.freeze())
}
}
impl<V: ?Sized + MessageValue> SerializedMessage<V>
{
/// 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<Self>
{
todo!()
}
/// Create from a slice of bytes
#[inline] pub fn from_slice(bytes: impl AsRef<[u8]>) -> eyre::Result<Self>
{
Self::from_buffer(bytes.as_ref())
}
/// Create from a `Bytes` instance
#[inline(always)] pub fn from_bytes(bytes: Bytes) -> eyre::Result<Self>
{
Self::from_buffer(bytes)
}
}
Loading…
Cancel
Save