From b77d33fa8d48e8e7046d1366f70e4788e55a9c63 Mon Sep 17 00:00:00 2001 From: Avril Date: Sun, 26 Sep 2021 21:37:49 +0100 Subject: [PATCH] de_singleton_inner() should no longer allocate a transform buffer when no transform is to be done. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fortune for transfer's current commit: Blessing − 吉 --- src/enc.rs | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/src/enc.rs b/src/enc.rs index b6e6e7f..6c5377a 100644 --- a/src/enc.rs +++ b/src/enc.rs @@ -75,6 +75,17 @@ pub struct SendOpt } ref_self!(SendOpt); +impl SendOpt +{ + /// Does the binary data of this format require special handling? + /// + /// True if encryption and/or compression are specified. + fn is_spec(&self) -> bool + { + self.comp.is_some() || self.encrypt.is_some() + } +} + pub type RecvOpt = SendOpt; /// Default buffer size for encryption transform stream copying. @@ -107,14 +118,19 @@ where F: AsyncRead + Unpin + ?Sized, } async fn de_singleton_inner(buf: F, mut from: &[u8], how: &RecvOpt) -> Result -where B: AsRef<[u8]> + AsyncWrite + Unpin, +where B: AsRef<[u8]> + AsyncWrite + Unpin + Default, F: FnOnce(&[u8]) -> B { // Decompressor // The output is written to this (through writer) - let mut is_spec = false; //TODO: Determine this before allocating `buf`. - let mut buf = buf(from); - //let mut buf = Vec::with_capacity(from.len()); // The `spec` output buffer. Used if there are transformations that need to be done to the data before deserialisation + let mut is_spec = false; // This is set later. The value will sometimes differ from `how.is_spec()` depending on combinations of options. + // The `spec` output buffer. Used if there are transformations that need to be done to the data before deserialisation + let mut buf = if how.is_spec() { + buf(&from) + } else { + Default::default() + }; + //let mut buf = Vec::with_capacity(from.len()); from = { let mut b; let writer: &mut (dyn AsyncWrite + Unpin) = @@ -159,7 +175,6 @@ where B: AsRef<[u8]> + AsyncWrite + Unpin, from } }; - // Deserialise let v = match how.format { SerialFormat::Text => serde_json::from_slice(&from[..])?, @@ -283,6 +298,21 @@ pub enum TransformErrorKind #[derive(Debug)] pub struct RecvError(Box<(TransformErrorKind, RecvOpt)>); +impl RecvError +{ + #[inline] pub fn kind(&self) -> &TransformErrorKind + { + &self.0.0 + } +} +impl SendError +{ + #[inline] pub fn kind(&self) -> &TransformErrorKind + { + &self.0.0 + } +} + impl error::Error for RecvError { fn source(&self) -> Option<&(dyn error::Error + 'static)> {