de_singleton_inner() should no longer allocate a transform buffer when no transform is to be done.

Fortune for transfer's current commit: Blessing − 吉
basic
Avril 3 years ago
parent e900e6d398
commit b77d33fa8d
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -75,6 +75,17 @@ pub struct SendOpt
} }
ref_self!(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; pub type RecvOpt = SendOpt;
/// Default buffer size for encryption transform stream copying. /// Default buffer size for encryption transform stream copying.
@ -107,14 +118,19 @@ where F: AsyncRead + Unpin + ?Sized,
} }
async fn de_singleton_inner<T: DeserializeOwned, B, F>(buf: F, mut from: &[u8], how: &RecvOpt) -> Result<T, TransformErrorKind> async fn de_singleton_inner<T: DeserializeOwned, B, F>(buf: F, mut from: &[u8], how: &RecvOpt) -> Result<T, TransformErrorKind>
where B: AsRef<[u8]> + AsyncWrite + Unpin, where B: AsRef<[u8]> + AsyncWrite + Unpin + Default,
F: FnOnce(&[u8]) -> B F: FnOnce(&[u8]) -> B
{ {
// Decompressor // Decompressor
// The output is written to this (through writer) // The output is written to this (through writer)
let mut is_spec = false; //TODO: Determine this before allocating `buf`. let mut is_spec = false; // This is set later. The value will sometimes differ from `how.is_spec()` depending on combinations of options.
let mut buf = buf(from); // The `spec` output buffer. Used if there are transformations that need to be done to the data before deserialisation
//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 buf = if how.is_spec() {
buf(&from)
} else {
Default::default()
};
//let mut buf = Vec::with_capacity(from.len());
from = { from = {
let mut b; let mut b;
let writer: &mut (dyn AsyncWrite + Unpin) = let writer: &mut (dyn AsyncWrite + Unpin) =
@ -159,7 +175,6 @@ where B: AsRef<[u8]> + AsyncWrite + Unpin,
from from
} }
}; };
// Deserialise // Deserialise
let v = match how.format { let v = match how.format {
SerialFormat::Text => serde_json::from_slice(&from[..])?, SerialFormat::Text => serde_json::from_slice(&from[..])?,
@ -283,6 +298,21 @@ pub enum TransformErrorKind
#[derive(Debug)] #[derive(Debug)]
pub struct RecvError(Box<(TransformErrorKind, RecvOpt)>); 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 impl error::Error for RecvError
{ {
fn source(&self) -> Option<&(dyn error::Error + 'static)> { fn source(&self) -> Option<&(dyn error::Error + 'static)> {

Loading…
Cancel
Save