@ -17,18 +17,49 @@ const DEFER_DROP_SIZE_FLOOR: usize = 1024 * 1024; // 1 MB
enum MaybeCompressor < ' a , T >
enum MaybeCompressor < ' a , T >
{
{
Compressing ( Compressor < & ' a mut T > ) ,
Compressing ( Compressor < & ' a mut T > ) ,
Decompressing ( Decompressor < & ' a mut T > ) ,
Raw ( & ' a mut T ) ,
Raw ( & ' a mut T ) ,
}
}
/// Compress or decompress?
#[ derive(Debug, Clone, PartialEq, Eq, Hash, Copy, PartialOrd, Ord) ]
enum CompKind
{
Compress ,
Decompress
}
impl Default for CompKind
{
#[ inline ]
fn default ( ) -> Self
{
Self ::Compress
}
}
impl < ' a , T > MaybeCompressor < ' a , T >
{
/// What kind is this compressor set to
pub fn kind ( & self ) -> Option < CompKind >
{
Some ( match self {
Self ::Raw ( _ ) = > return None ,
Self ::Compressing ( _ ) = > CompKind ::Compress ,
Self ::Decompressing ( _ ) = > CompKind ::Decompress ,
} )
}
}
impl < ' a , T > MaybeCompressor < ' a , T >
impl < ' a , T > MaybeCompressor < ' a , T >
where T : AsyncWrite + Unpin + ' a
where T : AsyncWrite + Unpin + ' a
{
{
pub fn new ( raw : & ' a mut T , compress : bool ) -> Self
pub fn new ( raw : & ' a mut T , compress : Option < CompKind > ) -> Self
{
{
if compress {
match compress {
Self ::Compressing ( Compressor ::new ( raw ) )
Some ( CompKind ::Compress ) = > Self ::Compressing ( Compressor ::new ( raw ) ) ,
} else {
Some ( CompKind ::Decompress ) = > Self ::Decompressing ( Decompressor ::new ( raw ) ) ,
Self ::Raw ( raw )
None = > Self ::Raw ( raw ) ,
}
}
}
}
}
}
@ -39,6 +70,7 @@ where T: AsyncWrite + Unpin + 'a
fn deref_mut ( & mut self ) -> & mut Self ::Target {
fn deref_mut ( & mut self ) -> & mut Self ::Target {
match self {
match self {
Self ::Compressing ( t ) = > t ,
Self ::Compressing ( t ) = > t ,
Self ::Decompressing ( t ) = > t ,
Self ::Raw ( o ) = > o ,
Self ::Raw ( o ) = > o ,
}
}
}
}
@ -51,6 +83,7 @@ where T: AsyncWrite + Unpin + 'a
fn deref ( & self ) -> & Self ::Target {
fn deref ( & self ) -> & Self ::Target {
match self {
match self {
Self ::Compressing ( t ) = > t ,
Self ::Compressing ( t ) = > t ,
Self ::Decompressing ( t ) = > t ,
Self ::Raw ( o ) = > o ,
Self ::Raw ( o ) = > o ,
}
}
}
}
@ -73,7 +106,7 @@ where W: AsyncWrite + Unpin
. with_section ( sect_type_name . clone ( ) ) ? ;
. with_section ( sect_type_name . clone ( ) ) ? ;
{
{
let mut stream = MaybeCompressor ::new ( & mut to , compress );
let mut stream = MaybeCompressor ::new ( & mut to , compress .then ( | | CompKind ::Compress ) );
cfg_eprintln ! ( Verbose ; config ::get_global ( ) , "Writing {} bytes of type {:?} to stream of type {:?}" , vec . len ( ) , std ::any ::type_name ::< T > ( ) , std ::any ::type_name ::< W > ( ) ) ;
cfg_eprintln ! ( Verbose ; config ::get_global ( ) , "Writing {} bytes of type {:?} to stream of type {:?}" , vec . len ( ) , std ::any ::type_name ::< T > ( ) , std ::any ::type_name ::< W > ( ) ) ;