From 03eaddb7eb6145a27dfbcdfe754045fdffe21739 Mon Sep 17 00:00:00 2001 From: Avril Date: Sat, 25 Sep 2021 23:12:24 +0100 Subject: [PATCH] Start: de_singleton_inner() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fortune for transfer's current commit: Small curse − 小凶 --- src/enc.rs | 66 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 51 insertions(+), 15 deletions(-) diff --git a/src/enc.rs b/src/enc.rs index a8d240c..f78a0ff 100644 --- a/src/enc.rs +++ b/src/enc.rs @@ -13,7 +13,7 @@ use tokio::io::{ }; use serde::{ Serialize, - Deserialize + de::DeserializeOwned }; #[derive(Debug, Clone, PartialEq, Eq, Hash, Copy, PartialOrd, Ord)] @@ -75,6 +75,19 @@ pub struct SendOpt } ref_self!(SendOpt); +#[derive(Debug, Clone, PartialEq, Eq, Hash, Copy, Default)] +pub struct RecvOpt +{ + pub comp: Option, + pub handle_encrypted: Option, + pub format: SerialFormat, + /// Max size of the (decompressed) input buffer. + /// 0 for unlimited. + //TODO: Do we need this? + pub max_size: usize, +} +ref_self!(RecvOpt); + /// Default buffer size for encryption transform stream copying. pub const DEFAULT_BUFSIZE: usize = 4096; @@ -104,7 +117,13 @@ where F: AsyncRead + Unpin + ?Sized, Ok((written, read)) } -async fn ser_singleton_inner(to: F, value: &T, how: impl AsRef) -> Result<(V, usize), SendErrorKind> +async fn de_singleton_inner(from: S, how: impl AsRef) -> Result +where S: AsRef<[u8]> //TODO: Should we use bytes::Buf or something instead? +{ + todo!("Deserialise from `from` using `how`.") +} + +async fn ser_singleton_inner(to: F, value: &T, how: impl AsRef) -> Result<(V, usize), TransformErrorKind> where F: FnOnce(&Vec) -> V { let how = how.as_ref(); @@ -153,13 +172,25 @@ where F: FnOnce(&Vec) -> V /// Serialise a single object to a stream with the method described by `how`. #[inline] pub async fn write_singleton(to: &mut S, value: &T, how: &SendOpt) -> Result { - ser_singleton_inner(|_| to, value, &how).await - .map_err(|k| SendError(Box::new((k, how.to_owned())))) - .map(|(_, n)| n) + let (cont, v) = ser_singleton_inner(|n| Vec::with_capacity(n.len()), value, &how).await + .map_err(|k| SendError(Box::new((k, how.to_owned()))))?; + + + let n = async move { + to.write_all(&(v as u64).to_be_bytes()[..]).await?; + to.write_all(&cont).await + .map(|_| std::mem::size_of::() + cont.len()) + } + .await + .map_err(|k| SendError(Box::new((k.into(), how.to_owned()))))?; + + + Ok(n) } +/// Kind of error for a send (serialise) or receive (deserialise) operation #[derive(Debug)] -pub enum SendErrorKind +pub enum TransformErrorKind { /// Invalid serialised format Format, @@ -174,14 +205,19 @@ pub enum SendErrorKind /// An error when sending / serialising an object. #[derive(Debug)] -pub struct SendError(Box<(SendErrorKind, SendOpt)>); +pub struct RecvError(Box<(TransformErrorKind, RecvOpt)>); + + +/// An error when sending / serialising an object. +#[derive(Debug)] +pub struct SendError(Box<(TransformErrorKind, SendOpt)>); impl error::Error for SendError { fn source(&self) -> Option<&(dyn error::Error + 'static)> { Some(match &self.0.0 { - SendErrorKind::IO(io) => io, + TransformErrorKind::IO(io) => io, _ => return None, }) } @@ -193,15 +229,15 @@ impl fmt::Display for SendError { write!(f, "error when serialising object with params {:?}: ", self.0.1)?; match self.0.0 { - SendErrorKind::Format => write!(f, "failed to serialise object to data"), - SendErrorKind::Compress => write!(f, "failed to compress data"), - SendErrorKind::Encrypt => write!(f, "failed to encrypt data"), - SendErrorKind::IO(_) => write!(f, "i/o failure"), + TransformErrorKind::Format => write!(f, "failed to serialise object to data"), + TransformErrorKind::Compress => write!(f, "failed to compress data"), + TransformErrorKind::Encrypt => write!(f, "failed to encrypt data"), + TransformErrorKind::IO(_) => write!(f, "i/o failure"), } } } -impl From for SendErrorKind +impl From for TransformErrorKind { fn from(from: io::Error) -> Self { @@ -210,7 +246,7 @@ impl From for SendErrorKind } -impl From for SendErrorKind +impl From for TransformErrorKind { #[inline] fn from(_: serde_cbor::Error) -> Self { @@ -218,7 +254,7 @@ impl From for SendErrorKind } } -impl From for SendErrorKind +impl From for TransformErrorKind { #[inline] fn from(_: serde_json::Error) -> Self {